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/StmtCXX.h"
22 #include "clang/AST/StmtOpenMP.h"
23 #include "clang/AST/StmtVisitor.h"
24 #include "clang/AST/TypeOrdering.h"
25 #include "clang/Basic/OpenMPKinds.h"
26 #include "clang/Sema/Initialization.h"
27 #include "clang/Sema/Lookup.h"
28 #include "clang/Sema/Scope.h"
29 #include "clang/Sema/ScopeInfo.h"
30 #include "clang/Sema/SemaInternal.h"
31 #include "llvm/ADT/IndexedMap.h"
32 #include "llvm/ADT/PointerEmbeddedInt.h"
33 #include "llvm/Frontend/OpenMP/OMPConstants.h"
34 using namespace clang;
35 using namespace llvm::omp;
36 
37 //===----------------------------------------------------------------------===//
38 // Stack of data-sharing attributes for variables
39 //===----------------------------------------------------------------------===//
40 
41 static const Expr *checkMapClauseExpressionBase(
42     Sema &SemaRef, Expr *E,
43     OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
44     OpenMPClauseKind CKind, bool NoDiagnose);
45 
46 namespace {
47 /// Default data sharing attributes, which can be applied to directive.
48 enum DefaultDataSharingAttributes {
49   DSA_unspecified = 0, /// Data sharing attribute not specified.
50   DSA_none = 1 << 0,   /// Default data sharing attribute 'none'.
51   DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'.
52 };
53 
54 /// Stack for tracking declarations used in OpenMP directives and
55 /// clauses and their data-sharing attributes.
56 class DSAStackTy {
57 public:
58   struct DSAVarData {
59     OpenMPDirectiveKind DKind = OMPD_unknown;
60     OpenMPClauseKind CKind = OMPC_unknown;
61     const Expr *RefExpr = nullptr;
62     DeclRefExpr *PrivateCopy = nullptr;
63     SourceLocation ImplicitDSALoc;
64     DSAVarData() = default;
65     DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
66                const Expr *RefExpr, DeclRefExpr *PrivateCopy,
67                SourceLocation ImplicitDSALoc)
68         : DKind(DKind), CKind(CKind), RefExpr(RefExpr),
69           PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc) {}
70   };
71   using OperatorOffsetTy =
72       llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>;
73   using DoacrossDependMapTy =
74       llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>;
75 
76 private:
77   struct DSAInfo {
78     OpenMPClauseKind Attributes = OMPC_unknown;
79     /// Pointer to a reference expression and a flag which shows that the
80     /// variable is marked as lastprivate(true) or not (false).
81     llvm::PointerIntPair<const Expr *, 1, bool> RefExpr;
82     DeclRefExpr *PrivateCopy = nullptr;
83   };
84   using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>;
85   using AlignedMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>;
86   using LCDeclInfo = std::pair<unsigned, VarDecl *>;
87   using LoopControlVariablesMapTy =
88       llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>;
89   /// Struct that associates a component with the clause kind where they are
90   /// found.
91   struct MappedExprComponentTy {
92     OMPClauseMappableExprCommon::MappableExprComponentLists Components;
93     OpenMPClauseKind Kind = OMPC_unknown;
94   };
95   using MappedExprComponentsTy =
96       llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>;
97   using CriticalsWithHintsTy =
98       llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>;
99   struct ReductionData {
100     using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>;
101     SourceRange ReductionRange;
102     llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp;
103     ReductionData() = default;
104     void set(BinaryOperatorKind BO, SourceRange RR) {
105       ReductionRange = RR;
106       ReductionOp = BO;
107     }
108     void set(const Expr *RefExpr, SourceRange RR) {
109       ReductionRange = RR;
110       ReductionOp = RefExpr;
111     }
112   };
113   using DeclReductionMapTy =
114       llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>;
115   struct DefaultmapInfo {
116     OpenMPDefaultmapClauseModifier ImplicitBehavior =
117         OMPC_DEFAULTMAP_MODIFIER_unknown;
118     SourceLocation SLoc;
119     DefaultmapInfo() = default;
120     DefaultmapInfo(OpenMPDefaultmapClauseModifier M, SourceLocation Loc)
121         : ImplicitBehavior(M), SLoc(Loc) {}
122   };
123 
124   struct SharingMapTy {
125     DeclSAMapTy SharingMap;
126     DeclReductionMapTy ReductionMap;
127     AlignedMapTy AlignedMap;
128     MappedExprComponentsTy MappedExprComponents;
129     LoopControlVariablesMapTy LCVMap;
130     DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
131     SourceLocation DefaultAttrLoc;
132     DefaultmapInfo DefaultmapMap[OMPC_DEFAULTMAP_unknown];
133     OpenMPDirectiveKind Directive = OMPD_unknown;
134     DeclarationNameInfo DirectiveName;
135     Scope *CurScope = nullptr;
136     SourceLocation ConstructLoc;
137     /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
138     /// get the data (loop counters etc.) about enclosing loop-based construct.
139     /// This data is required during codegen.
140     DoacrossDependMapTy DoacrossDepends;
141     /// First argument (Expr *) contains optional argument of the
142     /// 'ordered' clause, the second one is true if the regions has 'ordered'
143     /// clause, false otherwise.
144     llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion;
145     unsigned AssociatedLoops = 1;
146     bool HasMutipleLoops = false;
147     const Decl *PossiblyLoopCounter = nullptr;
148     bool NowaitRegion = false;
149     bool CancelRegion = false;
150     bool LoopStart = false;
151     bool BodyComplete = false;
152     SourceLocation InnerTeamsRegionLoc;
153     /// Reference to the taskgroup task_reduction reference expression.
154     Expr *TaskgroupReductionRef = nullptr;
155     llvm::DenseSet<QualType> MappedClassesQualTypes;
156     /// List of globals marked as declare target link in this target region
157     /// (isOpenMPTargetExecutionDirective(Directive) == true).
158     llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls;
159     SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
160                  Scope *CurScope, SourceLocation Loc)
161         : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
162           ConstructLoc(Loc) {}
163     SharingMapTy() = default;
164   };
165 
166   using StackTy = SmallVector<SharingMapTy, 4>;
167 
168   /// Stack of used declaration and their data-sharing attributes.
169   DeclSAMapTy Threadprivates;
170   const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
171   SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack;
172   /// true, if check for DSA must be from parent directive, false, if
173   /// from current directive.
174   OpenMPClauseKind ClauseKindMode = OMPC_unknown;
175   Sema &SemaRef;
176   bool ForceCapturing = false;
177   /// true if all the variables in the target executable directives must be
178   /// captured by reference.
179   bool ForceCaptureByReferenceInTargetExecutable = false;
180   CriticalsWithHintsTy Criticals;
181   unsigned IgnoredStackElements = 0;
182 
183   /// Iterators over the stack iterate in order from innermost to outermost
184   /// directive.
185   using const_iterator = StackTy::const_reverse_iterator;
186   const_iterator begin() const {
187     return Stack.empty() ? const_iterator()
188                          : Stack.back().first.rbegin() + IgnoredStackElements;
189   }
190   const_iterator end() const {
191     return Stack.empty() ? const_iterator() : Stack.back().first.rend();
192   }
193   using iterator = StackTy::reverse_iterator;
194   iterator begin() {
195     return Stack.empty() ? iterator()
196                          : Stack.back().first.rbegin() + IgnoredStackElements;
197   }
198   iterator end() {
199     return Stack.empty() ? iterator() : Stack.back().first.rend();
200   }
201 
202   // Convenience operations to get at the elements of the stack.
203 
204   bool isStackEmpty() const {
205     return Stack.empty() ||
206            Stack.back().second != CurrentNonCapturingFunctionScope ||
207            Stack.back().first.size() <= IgnoredStackElements;
208   }
209   size_t getStackSize() const {
210     return isStackEmpty() ? 0
211                           : Stack.back().first.size() - IgnoredStackElements;
212   }
213 
214   SharingMapTy *getTopOfStackOrNull() {
215     size_t Size = getStackSize();
216     if (Size == 0)
217       return nullptr;
218     return &Stack.back().first[Size - 1];
219   }
220   const SharingMapTy *getTopOfStackOrNull() const {
221     return const_cast<DSAStackTy&>(*this).getTopOfStackOrNull();
222   }
223   SharingMapTy &getTopOfStack() {
224     assert(!isStackEmpty() && "no current directive");
225     return *getTopOfStackOrNull();
226   }
227   const SharingMapTy &getTopOfStack() const {
228     return const_cast<DSAStackTy&>(*this).getTopOfStack();
229   }
230 
231   SharingMapTy *getSecondOnStackOrNull() {
232     size_t Size = getStackSize();
233     if (Size <= 1)
234       return nullptr;
235     return &Stack.back().first[Size - 2];
236   }
237   const SharingMapTy *getSecondOnStackOrNull() const {
238     return const_cast<DSAStackTy&>(*this).getSecondOnStackOrNull();
239   }
240 
241   /// Get the stack element at a certain level (previously returned by
242   /// \c getNestingLevel).
243   ///
244   /// Note that nesting levels count from outermost to innermost, and this is
245   /// the reverse of our iteration order where new inner levels are pushed at
246   /// the front of the stack.
247   SharingMapTy &getStackElemAtLevel(unsigned Level) {
248     assert(Level < getStackSize() && "no such stack element");
249     return Stack.back().first[Level];
250   }
251   const SharingMapTy &getStackElemAtLevel(unsigned Level) const {
252     return const_cast<DSAStackTy&>(*this).getStackElemAtLevel(Level);
253   }
254 
255   DSAVarData getDSA(const_iterator &Iter, ValueDecl *D) const;
256 
257   /// Checks if the variable is a local for OpenMP region.
258   bool isOpenMPLocal(VarDecl *D, const_iterator Iter) const;
259 
260   /// Vector of previously declared requires directives
261   SmallVector<const OMPRequiresDecl *, 2> RequiresDecls;
262   /// omp_allocator_handle_t type.
263   QualType OMPAllocatorHandleT;
264   /// Expression for the predefined allocators.
265   Expr *OMPPredefinedAllocators[OMPAllocateDeclAttr::OMPUserDefinedMemAlloc] = {
266       nullptr};
267   /// Vector of previously encountered target directives
268   SmallVector<SourceLocation, 2> TargetLocations;
269 
270 public:
271   explicit DSAStackTy(Sema &S) : SemaRef(S) {}
272 
273   /// Sets omp_allocator_handle_t type.
274   void setOMPAllocatorHandleT(QualType Ty) { OMPAllocatorHandleT = Ty; }
275   /// Gets omp_allocator_handle_t type.
276   QualType getOMPAllocatorHandleT() const { return OMPAllocatorHandleT; }
277   /// Sets the given default allocator.
278   void setAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
279                     Expr *Allocator) {
280     OMPPredefinedAllocators[AllocatorKind] = Allocator;
281   }
282   /// Returns the specified default allocator.
283   Expr *getAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind) const {
284     return OMPPredefinedAllocators[AllocatorKind];
285   }
286 
287   bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
288   OpenMPClauseKind getClauseParsingMode() const {
289     assert(isClauseParsingMode() && "Must be in clause parsing mode.");
290     return ClauseKindMode;
291   }
292   void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
293 
294   bool isBodyComplete() const {
295     const SharingMapTy *Top = getTopOfStackOrNull();
296     return Top && Top->BodyComplete;
297   }
298   void setBodyComplete() {
299     getTopOfStack().BodyComplete = true;
300   }
301 
302   bool isForceVarCapturing() const { return ForceCapturing; }
303   void setForceVarCapturing(bool V) { ForceCapturing = V; }
304 
305   void setForceCaptureByReferenceInTargetExecutable(bool V) {
306     ForceCaptureByReferenceInTargetExecutable = V;
307   }
308   bool isForceCaptureByReferenceInTargetExecutable() const {
309     return ForceCaptureByReferenceInTargetExecutable;
310   }
311 
312   void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
313             Scope *CurScope, SourceLocation Loc) {
314     assert(!IgnoredStackElements &&
315            "cannot change stack while ignoring elements");
316     if (Stack.empty() ||
317         Stack.back().second != CurrentNonCapturingFunctionScope)
318       Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope);
319     Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc);
320     Stack.back().first.back().DefaultAttrLoc = Loc;
321   }
322 
323   void pop() {
324     assert(!IgnoredStackElements &&
325            "cannot change stack while ignoring elements");
326     assert(!Stack.back().first.empty() &&
327            "Data-sharing attributes stack is empty!");
328     Stack.back().first.pop_back();
329   }
330 
331   /// RAII object to temporarily leave the scope of a directive when we want to
332   /// logically operate in its parent.
333   class ParentDirectiveScope {
334     DSAStackTy &Self;
335     bool Active;
336   public:
337     ParentDirectiveScope(DSAStackTy &Self, bool Activate)
338         : Self(Self), Active(false) {
339       if (Activate)
340         enable();
341     }
342     ~ParentDirectiveScope() { disable(); }
343     void disable() {
344       if (Active) {
345         --Self.IgnoredStackElements;
346         Active = false;
347       }
348     }
349     void enable() {
350       if (!Active) {
351         ++Self.IgnoredStackElements;
352         Active = true;
353       }
354     }
355   };
356 
357   /// Marks that we're started loop parsing.
358   void loopInit() {
359     assert(isOpenMPLoopDirective(getCurrentDirective()) &&
360            "Expected loop-based directive.");
361     getTopOfStack().LoopStart = true;
362   }
363   /// Start capturing of the variables in the loop context.
364   void loopStart() {
365     assert(isOpenMPLoopDirective(getCurrentDirective()) &&
366            "Expected loop-based directive.");
367     getTopOfStack().LoopStart = false;
368   }
369   /// true, if variables are captured, false otherwise.
370   bool isLoopStarted() const {
371     assert(isOpenMPLoopDirective(getCurrentDirective()) &&
372            "Expected loop-based directive.");
373     return !getTopOfStack().LoopStart;
374   }
375   /// Marks (or clears) declaration as possibly loop counter.
376   void resetPossibleLoopCounter(const Decl *D = nullptr) {
377     getTopOfStack().PossiblyLoopCounter =
378         D ? D->getCanonicalDecl() : D;
379   }
380   /// Gets the possible loop counter decl.
381   const Decl *getPossiblyLoopCunter() const {
382     return getTopOfStack().PossiblyLoopCounter;
383   }
384   /// Start new OpenMP region stack in new non-capturing function.
385   void pushFunction() {
386     assert(!IgnoredStackElements &&
387            "cannot change stack while ignoring elements");
388     const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
389     assert(!isa<CapturingScopeInfo>(CurFnScope));
390     CurrentNonCapturingFunctionScope = CurFnScope;
391   }
392   /// Pop region stack for non-capturing function.
393   void popFunction(const FunctionScopeInfo *OldFSI) {
394     assert(!IgnoredStackElements &&
395            "cannot change stack while ignoring elements");
396     if (!Stack.empty() && Stack.back().second == OldFSI) {
397       assert(Stack.back().first.empty());
398       Stack.pop_back();
399     }
400     CurrentNonCapturingFunctionScope = nullptr;
401     for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) {
402       if (!isa<CapturingScopeInfo>(FSI)) {
403         CurrentNonCapturingFunctionScope = FSI;
404         break;
405       }
406     }
407   }
408 
409   void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) {
410     Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint);
411   }
412   const std::pair<const OMPCriticalDirective *, llvm::APSInt>
413   getCriticalWithHint(const DeclarationNameInfo &Name) const {
414     auto I = Criticals.find(Name.getAsString());
415     if (I != Criticals.end())
416       return I->second;
417     return std::make_pair(nullptr, llvm::APSInt());
418   }
419   /// If 'aligned' declaration for given variable \a D was not seen yet,
420   /// add it and return NULL; otherwise return previous occurrence's expression
421   /// for diagnostics.
422   const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE);
423 
424   /// Register specified variable as loop control variable.
425   void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture);
426   /// Check if the specified variable is a loop control variable for
427   /// current region.
428   /// \return The index of the loop control variable in the list of associated
429   /// for-loops (from outer to inner).
430   const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const;
431   /// Check if the specified variable is a loop control variable for
432   /// parent region.
433   /// \return The index of the loop control variable in the list of associated
434   /// for-loops (from outer to inner).
435   const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const;
436   /// Get the loop control variable for the I-th loop (or nullptr) in
437   /// parent directive.
438   const ValueDecl *getParentLoopControlVariable(unsigned I) const;
439 
440   /// Adds explicit data sharing attribute to the specified declaration.
441   void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
442               DeclRefExpr *PrivateCopy = nullptr);
443 
444   /// Adds additional information for the reduction items with the reduction id
445   /// represented as an operator.
446   void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
447                                  BinaryOperatorKind BOK);
448   /// Adds additional information for the reduction items with the reduction id
449   /// represented as reduction identifier.
450   void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
451                                  const Expr *ReductionRef);
452   /// Returns the location and reduction operation from the innermost parent
453   /// region for the given \p D.
454   const DSAVarData
455   getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
456                                    BinaryOperatorKind &BOK,
457                                    Expr *&TaskgroupDescriptor) const;
458   /// Returns the location and reduction operation from the innermost parent
459   /// region for the given \p D.
460   const DSAVarData
461   getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
462                                    const Expr *&ReductionRef,
463                                    Expr *&TaskgroupDescriptor) const;
464   /// Return reduction reference expression for the current taskgroup.
465   Expr *getTaskgroupReductionRef() const {
466     assert(getTopOfStack().Directive == OMPD_taskgroup &&
467            "taskgroup reference expression requested for non taskgroup "
468            "directive.");
469     return getTopOfStack().TaskgroupReductionRef;
470   }
471   /// Checks if the given \p VD declaration is actually a taskgroup reduction
472   /// descriptor variable at the \p Level of OpenMP regions.
473   bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const {
474     return getStackElemAtLevel(Level).TaskgroupReductionRef &&
475            cast<DeclRefExpr>(getStackElemAtLevel(Level).TaskgroupReductionRef)
476                    ->getDecl() == VD;
477   }
478 
479   /// Returns data sharing attributes from top of the stack for the
480   /// specified declaration.
481   const DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
482   /// Returns data-sharing attributes for the specified declaration.
483   const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const;
484   /// Checks if the specified variables has data-sharing attributes which
485   /// match specified \a CPred predicate in any directive which matches \a DPred
486   /// predicate.
487   const DSAVarData
488   hasDSA(ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
489          const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
490          bool FromParent) const;
491   /// Checks if the specified variables has data-sharing attributes which
492   /// match specified \a CPred predicate in any innermost directive which
493   /// matches \a DPred predicate.
494   const DSAVarData
495   hasInnermostDSA(ValueDecl *D,
496                   const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
497                   const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
498                   bool FromParent) const;
499   /// Checks if the specified variables has explicit data-sharing
500   /// attributes which match specified \a CPred predicate at the specified
501   /// OpenMP region.
502   bool hasExplicitDSA(const ValueDecl *D,
503                       const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
504                       unsigned Level, bool NotLastprivate = false) const;
505 
506   /// Returns true if the directive at level \Level matches in the
507   /// specified \a DPred predicate.
508   bool hasExplicitDirective(
509       const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
510       unsigned Level) const;
511 
512   /// Finds a directive which matches specified \a DPred predicate.
513   bool hasDirective(
514       const llvm::function_ref<bool(
515           OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)>
516           DPred,
517       bool FromParent) const;
518 
519   /// Returns currently analyzed directive.
520   OpenMPDirectiveKind getCurrentDirective() const {
521     const SharingMapTy *Top = getTopOfStackOrNull();
522     return Top ? Top->Directive : OMPD_unknown;
523   }
524   /// Returns directive kind at specified level.
525   OpenMPDirectiveKind getDirective(unsigned Level) const {
526     assert(!isStackEmpty() && "No directive at specified level.");
527     return getStackElemAtLevel(Level).Directive;
528   }
529   /// Returns the capture region at the specified level.
530   OpenMPDirectiveKind getCaptureRegion(unsigned Level,
531                                        unsigned OpenMPCaptureLevel) const {
532     SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
533     getOpenMPCaptureRegions(CaptureRegions, getDirective(Level));
534     return CaptureRegions[OpenMPCaptureLevel];
535   }
536   /// Returns parent directive.
537   OpenMPDirectiveKind getParentDirective() const {
538     const SharingMapTy *Parent = getSecondOnStackOrNull();
539     return Parent ? Parent->Directive : OMPD_unknown;
540   }
541 
542   /// Add requires decl to internal vector
543   void addRequiresDecl(OMPRequiresDecl *RD) {
544     RequiresDecls.push_back(RD);
545   }
546 
547   /// Checks if the defined 'requires' directive has specified type of clause.
548   template <typename ClauseType>
549   bool hasRequiresDeclWithClause() {
550     return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
551       return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
552         return isa<ClauseType>(C);
553       });
554     });
555   }
556 
557   /// Checks for a duplicate clause amongst previously declared requires
558   /// directives
559   bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const {
560     bool IsDuplicate = false;
561     for (OMPClause *CNew : ClauseList) {
562       for (const OMPRequiresDecl *D : RequiresDecls) {
563         for (const OMPClause *CPrev : D->clauselists()) {
564           if (CNew->getClauseKind() == CPrev->getClauseKind()) {
565             SemaRef.Diag(CNew->getBeginLoc(),
566                          diag::err_omp_requires_clause_redeclaration)
567                 << getOpenMPClauseName(CNew->getClauseKind());
568             SemaRef.Diag(CPrev->getBeginLoc(),
569                          diag::note_omp_requires_previous_clause)
570                 << getOpenMPClauseName(CPrev->getClauseKind());
571             IsDuplicate = true;
572           }
573         }
574       }
575     }
576     return IsDuplicate;
577   }
578 
579   /// Add location of previously encountered target to internal vector
580   void addTargetDirLocation(SourceLocation LocStart) {
581     TargetLocations.push_back(LocStart);
582   }
583 
584   // Return previously encountered target region locations.
585   ArrayRef<SourceLocation> getEncounteredTargetLocs() const {
586     return TargetLocations;
587   }
588 
589   /// Set default data sharing attribute to none.
590   void setDefaultDSANone(SourceLocation Loc) {
591     getTopOfStack().DefaultAttr = DSA_none;
592     getTopOfStack().DefaultAttrLoc = Loc;
593   }
594   /// Set default data sharing attribute to shared.
595   void setDefaultDSAShared(SourceLocation Loc) {
596     getTopOfStack().DefaultAttr = DSA_shared;
597     getTopOfStack().DefaultAttrLoc = Loc;
598   }
599   /// Set default data mapping attribute to Modifier:Kind
600   void setDefaultDMAAttr(OpenMPDefaultmapClauseModifier M,
601                          OpenMPDefaultmapClauseKind Kind,
602                          SourceLocation Loc) {
603     DefaultmapInfo &DMI = getTopOfStack().DefaultmapMap[Kind];
604     DMI.ImplicitBehavior = M;
605     DMI.SLoc = Loc;
606   }
607   /// Check whether the implicit-behavior has been set in defaultmap
608   bool checkDefaultmapCategory(OpenMPDefaultmapClauseKind VariableCategory) {
609     return getTopOfStack().DefaultmapMap[VariableCategory].ImplicitBehavior !=
610            OMPC_DEFAULTMAP_MODIFIER_unknown;
611   }
612 
613   DefaultDataSharingAttributes getDefaultDSA() const {
614     return isStackEmpty() ? DSA_unspecified
615                           : getTopOfStack().DefaultAttr;
616   }
617   SourceLocation getDefaultDSALocation() const {
618     return isStackEmpty() ? SourceLocation()
619                           : getTopOfStack().DefaultAttrLoc;
620   }
621   OpenMPDefaultmapClauseModifier
622   getDefaultmapModifier(OpenMPDefaultmapClauseKind Kind) const {
623     return isStackEmpty()
624                ? OMPC_DEFAULTMAP_MODIFIER_unknown
625                : getTopOfStack().DefaultmapMap[Kind].ImplicitBehavior;
626   }
627   OpenMPDefaultmapClauseModifier
628   getDefaultmapModifierAtLevel(unsigned Level,
629                                OpenMPDefaultmapClauseKind Kind) const {
630     return getStackElemAtLevel(Level).DefaultmapMap[Kind].ImplicitBehavior;
631   }
632   bool isDefaultmapCapturedByRef(unsigned Level,
633                                  OpenMPDefaultmapClauseKind Kind) const {
634     OpenMPDefaultmapClauseModifier M =
635         getDefaultmapModifierAtLevel(Level, Kind);
636     if (Kind == OMPC_DEFAULTMAP_scalar || Kind == OMPC_DEFAULTMAP_pointer) {
637       return (M == OMPC_DEFAULTMAP_MODIFIER_alloc) ||
638              (M == OMPC_DEFAULTMAP_MODIFIER_to) ||
639              (M == OMPC_DEFAULTMAP_MODIFIER_from) ||
640              (M == OMPC_DEFAULTMAP_MODIFIER_tofrom);
641     }
642     return true;
643   }
644   static bool mustBeFirstprivateBase(OpenMPDefaultmapClauseModifier M,
645                                      OpenMPDefaultmapClauseKind Kind) {
646     switch (Kind) {
647     case OMPC_DEFAULTMAP_scalar:
648     case OMPC_DEFAULTMAP_pointer:
649       return (M == OMPC_DEFAULTMAP_MODIFIER_unknown) ||
650              (M == OMPC_DEFAULTMAP_MODIFIER_firstprivate) ||
651              (M == OMPC_DEFAULTMAP_MODIFIER_default);
652     case OMPC_DEFAULTMAP_aggregate:
653       return M == OMPC_DEFAULTMAP_MODIFIER_firstprivate;
654     default:
655       break;
656     }
657     llvm_unreachable("Unexpected OpenMPDefaultmapClauseKind enum");
658   }
659   bool mustBeFirstprivateAtLevel(unsigned Level,
660                                  OpenMPDefaultmapClauseKind Kind) const {
661     OpenMPDefaultmapClauseModifier M =
662         getDefaultmapModifierAtLevel(Level, Kind);
663     return mustBeFirstprivateBase(M, Kind);
664   }
665   bool mustBeFirstprivate(OpenMPDefaultmapClauseKind Kind) const {
666     OpenMPDefaultmapClauseModifier M = getDefaultmapModifier(Kind);
667     return mustBeFirstprivateBase(M, Kind);
668   }
669 
670   /// Checks if the specified variable is a threadprivate.
671   bool isThreadPrivate(VarDecl *D) {
672     const DSAVarData DVar = getTopDSA(D, false);
673     return isOpenMPThreadPrivate(DVar.CKind);
674   }
675 
676   /// Marks current region as ordered (it has an 'ordered' clause).
677   void setOrderedRegion(bool IsOrdered, const Expr *Param,
678                         OMPOrderedClause *Clause) {
679     if (IsOrdered)
680       getTopOfStack().OrderedRegion.emplace(Param, Clause);
681     else
682       getTopOfStack().OrderedRegion.reset();
683   }
684   /// Returns true, if region is ordered (has associated 'ordered' clause),
685   /// false - otherwise.
686   bool isOrderedRegion() const {
687     if (const SharingMapTy *Top = getTopOfStackOrNull())
688       return Top->OrderedRegion.hasValue();
689     return false;
690   }
691   /// Returns optional parameter for the ordered region.
692   std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const {
693     if (const SharingMapTy *Top = getTopOfStackOrNull())
694       if (Top->OrderedRegion.hasValue())
695         return Top->OrderedRegion.getValue();
696     return std::make_pair(nullptr, nullptr);
697   }
698   /// Returns true, if parent region is ordered (has associated
699   /// 'ordered' clause), false - otherwise.
700   bool isParentOrderedRegion() const {
701     if (const SharingMapTy *Parent = getSecondOnStackOrNull())
702       return Parent->OrderedRegion.hasValue();
703     return false;
704   }
705   /// Returns optional parameter for the ordered region.
706   std::pair<const Expr *, OMPOrderedClause *>
707   getParentOrderedRegionParam() const {
708     if (const SharingMapTy *Parent = getSecondOnStackOrNull())
709       if (Parent->OrderedRegion.hasValue())
710         return Parent->OrderedRegion.getValue();
711     return std::make_pair(nullptr, nullptr);
712   }
713   /// Marks current region as nowait (it has a 'nowait' clause).
714   void setNowaitRegion(bool IsNowait = true) {
715     getTopOfStack().NowaitRegion = IsNowait;
716   }
717   /// Returns true, if parent region is nowait (has associated
718   /// 'nowait' clause), false - otherwise.
719   bool isParentNowaitRegion() const {
720     if (const SharingMapTy *Parent = getSecondOnStackOrNull())
721       return Parent->NowaitRegion;
722     return false;
723   }
724   /// Marks parent region as cancel region.
725   void setParentCancelRegion(bool Cancel = true) {
726     if (SharingMapTy *Parent = getSecondOnStackOrNull())
727       Parent->CancelRegion |= Cancel;
728   }
729   /// Return true if current region has inner cancel construct.
730   bool isCancelRegion() const {
731     const SharingMapTy *Top = getTopOfStackOrNull();
732     return Top ? Top->CancelRegion : false;
733   }
734 
735   /// Set collapse value for the region.
736   void setAssociatedLoops(unsigned Val) {
737     getTopOfStack().AssociatedLoops = Val;
738     if (Val > 1)
739       getTopOfStack().HasMutipleLoops = true;
740   }
741   /// Return collapse value for region.
742   unsigned getAssociatedLoops() const {
743     const SharingMapTy *Top = getTopOfStackOrNull();
744     return Top ? Top->AssociatedLoops : 0;
745   }
746   /// Returns true if the construct is associated with multiple loops.
747   bool hasMutipleLoops() const {
748     const SharingMapTy *Top = getTopOfStackOrNull();
749     return Top ? Top->HasMutipleLoops : false;
750   }
751 
752   /// Marks current target region as one with closely nested teams
753   /// region.
754   void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
755     if (SharingMapTy *Parent = getSecondOnStackOrNull())
756       Parent->InnerTeamsRegionLoc = TeamsRegionLoc;
757   }
758   /// Returns true, if current region has closely nested teams region.
759   bool hasInnerTeamsRegion() const {
760     return getInnerTeamsRegionLoc().isValid();
761   }
762   /// Returns location of the nested teams region (if any).
763   SourceLocation getInnerTeamsRegionLoc() const {
764     const SharingMapTy *Top = getTopOfStackOrNull();
765     return Top ? Top->InnerTeamsRegionLoc : SourceLocation();
766   }
767 
768   Scope *getCurScope() const {
769     const SharingMapTy *Top = getTopOfStackOrNull();
770     return Top ? Top->CurScope : nullptr;
771   }
772   SourceLocation getConstructLoc() const {
773     const SharingMapTy *Top = getTopOfStackOrNull();
774     return Top ? Top->ConstructLoc : SourceLocation();
775   }
776 
777   /// Do the check specified in \a Check to all component lists and return true
778   /// if any issue is found.
779   bool checkMappableExprComponentListsForDecl(
780       const ValueDecl *VD, bool CurrentRegionOnly,
781       const llvm::function_ref<
782           bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
783                OpenMPClauseKind)>
784           Check) const {
785     if (isStackEmpty())
786       return false;
787     auto SI = begin();
788     auto SE = end();
789 
790     if (SI == SE)
791       return false;
792 
793     if (CurrentRegionOnly)
794       SE = std::next(SI);
795     else
796       std::advance(SI, 1);
797 
798     for (; SI != SE; ++SI) {
799       auto MI = SI->MappedExprComponents.find(VD);
800       if (MI != SI->MappedExprComponents.end())
801         for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
802              MI->second.Components)
803           if (Check(L, MI->second.Kind))
804             return true;
805     }
806     return false;
807   }
808 
809   /// Do the check specified in \a Check to all component lists at a given level
810   /// and return true if any issue is found.
811   bool checkMappableExprComponentListsForDeclAtLevel(
812       const ValueDecl *VD, unsigned Level,
813       const llvm::function_ref<
814           bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
815                OpenMPClauseKind)>
816           Check) const {
817     if (getStackSize() <= Level)
818       return false;
819 
820     const SharingMapTy &StackElem = getStackElemAtLevel(Level);
821     auto MI = StackElem.MappedExprComponents.find(VD);
822     if (MI != StackElem.MappedExprComponents.end())
823       for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
824            MI->second.Components)
825         if (Check(L, MI->second.Kind))
826           return true;
827     return false;
828   }
829 
830   /// Create a new mappable expression component list associated with a given
831   /// declaration and initialize it with the provided list of components.
832   void addMappableExpressionComponents(
833       const ValueDecl *VD,
834       OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
835       OpenMPClauseKind WhereFoundClauseKind) {
836     MappedExprComponentTy &MEC = getTopOfStack().MappedExprComponents[VD];
837     // Create new entry and append the new components there.
838     MEC.Components.resize(MEC.Components.size() + 1);
839     MEC.Components.back().append(Components.begin(), Components.end());
840     MEC.Kind = WhereFoundClauseKind;
841   }
842 
843   unsigned getNestingLevel() const {
844     assert(!isStackEmpty());
845     return getStackSize() - 1;
846   }
847   void addDoacrossDependClause(OMPDependClause *C,
848                                const OperatorOffsetTy &OpsOffs) {
849     SharingMapTy *Parent = getSecondOnStackOrNull();
850     assert(Parent && isOpenMPWorksharingDirective(Parent->Directive));
851     Parent->DoacrossDepends.try_emplace(C, OpsOffs);
852   }
853   llvm::iterator_range<DoacrossDependMapTy::const_iterator>
854   getDoacrossDependClauses() const {
855     const SharingMapTy &StackElem = getTopOfStack();
856     if (isOpenMPWorksharingDirective(StackElem.Directive)) {
857       const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends;
858       return llvm::make_range(Ref.begin(), Ref.end());
859     }
860     return llvm::make_range(StackElem.DoacrossDepends.end(),
861                             StackElem.DoacrossDepends.end());
862   }
863 
864   // Store types of classes which have been explicitly mapped
865   void addMappedClassesQualTypes(QualType QT) {
866     SharingMapTy &StackElem = getTopOfStack();
867     StackElem.MappedClassesQualTypes.insert(QT);
868   }
869 
870   // Return set of mapped classes types
871   bool isClassPreviouslyMapped(QualType QT) const {
872     const SharingMapTy &StackElem = getTopOfStack();
873     return StackElem.MappedClassesQualTypes.count(QT) != 0;
874   }
875 
876   /// Adds global declare target to the parent target region.
877   void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) {
878     assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
879                E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link &&
880            "Expected declare target link global.");
881     for (auto &Elem : *this) {
882       if (isOpenMPTargetExecutionDirective(Elem.Directive)) {
883         Elem.DeclareTargetLinkVarDecls.push_back(E);
884         return;
885       }
886     }
887   }
888 
889   /// Returns the list of globals with declare target link if current directive
890   /// is target.
891   ArrayRef<DeclRefExpr *> getLinkGlobals() const {
892     assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) &&
893            "Expected target executable directive.");
894     return getTopOfStack().DeclareTargetLinkVarDecls;
895   }
896 };
897 
898 bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) {
899   return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind);
900 }
901 
902 bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) {
903   return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(DKind) ||
904          DKind == OMPD_unknown;
905 }
906 
907 } // namespace
908 
909 static const Expr *getExprAsWritten(const Expr *E) {
910   if (const auto *FE = dyn_cast<FullExpr>(E))
911     E = FE->getSubExpr();
912 
913   if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
914     E = MTE->getSubExpr();
915 
916   while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
917     E = Binder->getSubExpr();
918 
919   if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
920     E = ICE->getSubExprAsWritten();
921   return E->IgnoreParens();
922 }
923 
924 static Expr *getExprAsWritten(Expr *E) {
925   return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E)));
926 }
927 
928 static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
929   if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
930     if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
931       D = ME->getMemberDecl();
932   const auto *VD = dyn_cast<VarDecl>(D);
933   const auto *FD = dyn_cast<FieldDecl>(D);
934   if (VD != nullptr) {
935     VD = VD->getCanonicalDecl();
936     D = VD;
937   } else {
938     assert(FD);
939     FD = FD->getCanonicalDecl();
940     D = FD;
941   }
942   return D;
943 }
944 
945 static ValueDecl *getCanonicalDecl(ValueDecl *D) {
946   return const_cast<ValueDecl *>(
947       getCanonicalDecl(const_cast<const ValueDecl *>(D)));
948 }
949 
950 DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter,
951                                           ValueDecl *D) const {
952   D = getCanonicalDecl(D);
953   auto *VD = dyn_cast<VarDecl>(D);
954   const auto *FD = dyn_cast<FieldDecl>(D);
955   DSAVarData DVar;
956   if (Iter == end()) {
957     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
958     // in a region but not in construct]
959     //  File-scope or namespace-scope variables referenced in called routines
960     //  in the region are shared unless they appear in a threadprivate
961     //  directive.
962     if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD))
963       DVar.CKind = OMPC_shared;
964 
965     // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
966     // in a region but not in construct]
967     //  Variables with static storage duration that are declared in called
968     //  routines in the region are shared.
969     if (VD && VD->hasGlobalStorage())
970       DVar.CKind = OMPC_shared;
971 
972     // Non-static data members are shared by default.
973     if (FD)
974       DVar.CKind = OMPC_shared;
975 
976     return DVar;
977   }
978 
979   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
980   // in a Construct, C/C++, predetermined, p.1]
981   // Variables with automatic storage duration that are declared in a scope
982   // inside the construct are private.
983   if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
984       (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
985     DVar.CKind = OMPC_private;
986     return DVar;
987   }
988 
989   DVar.DKind = Iter->Directive;
990   // Explicitly specified attributes and local variables with predetermined
991   // attributes.
992   if (Iter->SharingMap.count(D)) {
993     const DSAInfo &Data = Iter->SharingMap.lookup(D);
994     DVar.RefExpr = Data.RefExpr.getPointer();
995     DVar.PrivateCopy = Data.PrivateCopy;
996     DVar.CKind = Data.Attributes;
997     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
998     return DVar;
999   }
1000 
1001   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1002   // in a Construct, C/C++, implicitly determined, p.1]
1003   //  In a parallel or task construct, the data-sharing attributes of these
1004   //  variables are determined by the default clause, if present.
1005   switch (Iter->DefaultAttr) {
1006   case DSA_shared:
1007     DVar.CKind = OMPC_shared;
1008     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1009     return DVar;
1010   case DSA_none:
1011     return DVar;
1012   case DSA_unspecified:
1013     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1014     // in a Construct, implicitly determined, p.2]
1015     //  In a parallel construct, if no default clause is present, these
1016     //  variables are shared.
1017     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1018     if ((isOpenMPParallelDirective(DVar.DKind) &&
1019          !isOpenMPTaskLoopDirective(DVar.DKind)) ||
1020         isOpenMPTeamsDirective(DVar.DKind)) {
1021       DVar.CKind = OMPC_shared;
1022       return DVar;
1023     }
1024 
1025     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1026     // in a Construct, implicitly determined, p.4]
1027     //  In a task construct, if no default clause is present, a variable that in
1028     //  the enclosing context is determined to be shared by all implicit tasks
1029     //  bound to the current team is shared.
1030     if (isOpenMPTaskingDirective(DVar.DKind)) {
1031       DSAVarData DVarTemp;
1032       const_iterator I = Iter, E = end();
1033       do {
1034         ++I;
1035         // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
1036         // Referenced in a Construct, implicitly determined, p.6]
1037         //  In a task construct, if no default clause is present, a variable
1038         //  whose data-sharing attribute is not determined by the rules above is
1039         //  firstprivate.
1040         DVarTemp = getDSA(I, D);
1041         if (DVarTemp.CKind != OMPC_shared) {
1042           DVar.RefExpr = nullptr;
1043           DVar.CKind = OMPC_firstprivate;
1044           return DVar;
1045         }
1046       } while (I != E && !isImplicitTaskingRegion(I->Directive));
1047       DVar.CKind =
1048           (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
1049       return DVar;
1050     }
1051   }
1052   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1053   // in a Construct, implicitly determined, p.3]
1054   //  For constructs other than task, if no default clause is present, these
1055   //  variables inherit their data-sharing attributes from the enclosing
1056   //  context.
1057   return getDSA(++Iter, D);
1058 }
1059 
1060 const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
1061                                          const Expr *NewDE) {
1062   assert(!isStackEmpty() && "Data sharing attributes stack is empty");
1063   D = getCanonicalDecl(D);
1064   SharingMapTy &StackElem = getTopOfStack();
1065   auto It = StackElem.AlignedMap.find(D);
1066   if (It == StackElem.AlignedMap.end()) {
1067     assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
1068     StackElem.AlignedMap[D] = NewDE;
1069     return nullptr;
1070   }
1071   assert(It->second && "Unexpected nullptr expr in the aligned map");
1072   return It->second;
1073 }
1074 
1075 void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
1076   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1077   D = getCanonicalDecl(D);
1078   SharingMapTy &StackElem = getTopOfStack();
1079   StackElem.LCVMap.try_emplace(
1080       D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
1081 }
1082 
1083 const DSAStackTy::LCDeclInfo
1084 DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
1085   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1086   D = getCanonicalDecl(D);
1087   const SharingMapTy &StackElem = getTopOfStack();
1088   auto It = StackElem.LCVMap.find(D);
1089   if (It != StackElem.LCVMap.end())
1090     return It->second;
1091   return {0, nullptr};
1092 }
1093 
1094 const DSAStackTy::LCDeclInfo
1095 DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
1096   const SharingMapTy *Parent = getSecondOnStackOrNull();
1097   assert(Parent && "Data-sharing attributes stack is empty");
1098   D = getCanonicalDecl(D);
1099   auto It = Parent->LCVMap.find(D);
1100   if (It != Parent->LCVMap.end())
1101     return It->second;
1102   return {0, nullptr};
1103 }
1104 
1105 const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
1106   const SharingMapTy *Parent = getSecondOnStackOrNull();
1107   assert(Parent && "Data-sharing attributes stack is empty");
1108   if (Parent->LCVMap.size() < I)
1109     return nullptr;
1110   for (const auto &Pair : Parent->LCVMap)
1111     if (Pair.second.first == I)
1112       return Pair.first;
1113   return nullptr;
1114 }
1115 
1116 void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
1117                         DeclRefExpr *PrivateCopy) {
1118   D = getCanonicalDecl(D);
1119   if (A == OMPC_threadprivate) {
1120     DSAInfo &Data = Threadprivates[D];
1121     Data.Attributes = A;
1122     Data.RefExpr.setPointer(E);
1123     Data.PrivateCopy = nullptr;
1124   } else {
1125     DSAInfo &Data = getTopOfStack().SharingMap[D];
1126     assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
1127            (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
1128            (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
1129            (isLoopControlVariable(D).first && A == OMPC_private));
1130     if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
1131       Data.RefExpr.setInt(/*IntVal=*/true);
1132       return;
1133     }
1134     const bool IsLastprivate =
1135         A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
1136     Data.Attributes = A;
1137     Data.RefExpr.setPointerAndInt(E, IsLastprivate);
1138     Data.PrivateCopy = PrivateCopy;
1139     if (PrivateCopy) {
1140       DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()];
1141       Data.Attributes = A;
1142       Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
1143       Data.PrivateCopy = nullptr;
1144     }
1145   }
1146 }
1147 
1148 /// Build a variable declaration for OpenMP loop iteration variable.
1149 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
1150                              StringRef Name, const AttrVec *Attrs = nullptr,
1151                              DeclRefExpr *OrigRef = nullptr) {
1152   DeclContext *DC = SemaRef.CurContext;
1153   IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
1154   TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
1155   auto *Decl =
1156       VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
1157   if (Attrs) {
1158     for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
1159          I != E; ++I)
1160       Decl->addAttr(*I);
1161   }
1162   Decl->setImplicit();
1163   if (OrigRef) {
1164     Decl->addAttr(
1165         OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef));
1166   }
1167   return Decl;
1168 }
1169 
1170 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
1171                                      SourceLocation Loc,
1172                                      bool RefersToCapture = false) {
1173   D->setReferenced();
1174   D->markUsed(S.Context);
1175   return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
1176                              SourceLocation(), D, RefersToCapture, Loc, Ty,
1177                              VK_LValue);
1178 }
1179 
1180 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1181                                            BinaryOperatorKind BOK) {
1182   D = getCanonicalDecl(D);
1183   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1184   assert(
1185       getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
1186       "Additional reduction info may be specified only for reduction items.");
1187   ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
1188   assert(ReductionData.ReductionRange.isInvalid() &&
1189          getTopOfStack().Directive == OMPD_taskgroup &&
1190          "Additional reduction info may be specified only once for reduction "
1191          "items.");
1192   ReductionData.set(BOK, SR);
1193   Expr *&TaskgroupReductionRef =
1194       getTopOfStack().TaskgroupReductionRef;
1195   if (!TaskgroupReductionRef) {
1196     VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1197                                SemaRef.Context.VoidPtrTy, ".task_red.");
1198     TaskgroupReductionRef =
1199         buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
1200   }
1201 }
1202 
1203 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1204                                            const Expr *ReductionRef) {
1205   D = getCanonicalDecl(D);
1206   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1207   assert(
1208       getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
1209       "Additional reduction info may be specified only for reduction items.");
1210   ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
1211   assert(ReductionData.ReductionRange.isInvalid() &&
1212          getTopOfStack().Directive == OMPD_taskgroup &&
1213          "Additional reduction info may be specified only once for reduction "
1214          "items.");
1215   ReductionData.set(ReductionRef, SR);
1216   Expr *&TaskgroupReductionRef =
1217       getTopOfStack().TaskgroupReductionRef;
1218   if (!TaskgroupReductionRef) {
1219     VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1220                                SemaRef.Context.VoidPtrTy, ".task_red.");
1221     TaskgroupReductionRef =
1222         buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
1223   }
1224 }
1225 
1226 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1227     const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
1228     Expr *&TaskgroupDescriptor) const {
1229   D = getCanonicalDecl(D);
1230   assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1231   for (const_iterator I = begin() + 1, E = end(); I != E; ++I) {
1232     const DSAInfo &Data = I->SharingMap.lookup(D);
1233     if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
1234       continue;
1235     const ReductionData &ReductionData = I->ReductionMap.lookup(D);
1236     if (!ReductionData.ReductionOp ||
1237         ReductionData.ReductionOp.is<const Expr *>())
1238       return DSAVarData();
1239     SR = ReductionData.ReductionRange;
1240     BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
1241     assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1242                                        "expression for the descriptor is not "
1243                                        "set.");
1244     TaskgroupDescriptor = I->TaskgroupReductionRef;
1245     return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1246                       Data.PrivateCopy, I->DefaultAttrLoc);
1247   }
1248   return DSAVarData();
1249 }
1250 
1251 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1252     const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
1253     Expr *&TaskgroupDescriptor) const {
1254   D = getCanonicalDecl(D);
1255   assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1256   for (const_iterator I = begin() + 1, E = end(); I != E; ++I) {
1257     const DSAInfo &Data = I->SharingMap.lookup(D);
1258     if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
1259       continue;
1260     const ReductionData &ReductionData = I->ReductionMap.lookup(D);
1261     if (!ReductionData.ReductionOp ||
1262         !ReductionData.ReductionOp.is<const Expr *>())
1263       return DSAVarData();
1264     SR = ReductionData.ReductionRange;
1265     ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
1266     assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1267                                        "expression for the descriptor is not "
1268                                        "set.");
1269     TaskgroupDescriptor = I->TaskgroupReductionRef;
1270     return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1271                       Data.PrivateCopy, I->DefaultAttrLoc);
1272   }
1273   return DSAVarData();
1274 }
1275 
1276 bool DSAStackTy::isOpenMPLocal(VarDecl *D, const_iterator I) const {
1277   D = D->getCanonicalDecl();
1278   for (const_iterator E = end(); I != E; ++I) {
1279     if (isImplicitOrExplicitTaskingRegion(I->Directive) ||
1280         isOpenMPTargetExecutionDirective(I->Directive)) {
1281       Scope *TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
1282       Scope *CurScope = getCurScope();
1283       while (CurScope && CurScope != TopScope && !CurScope->isDeclScope(D))
1284         CurScope = CurScope->getParent();
1285       return CurScope != TopScope;
1286     }
1287   }
1288   return false;
1289 }
1290 
1291 static bool isConstNotMutableType(Sema &SemaRef, QualType Type,
1292                                   bool AcceptIfMutable = true,
1293                                   bool *IsClassType = nullptr) {
1294   ASTContext &Context = SemaRef.getASTContext();
1295   Type = Type.getNonReferenceType().getCanonicalType();
1296   bool IsConstant = Type.isConstant(Context);
1297   Type = Context.getBaseElementType(Type);
1298   const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus
1299                                 ? Type->getAsCXXRecordDecl()
1300                                 : nullptr;
1301   if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
1302     if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
1303       RD = CTD->getTemplatedDecl();
1304   if (IsClassType)
1305     *IsClassType = RD;
1306   return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD &&
1307                          RD->hasDefinition() && RD->hasMutableFields());
1308 }
1309 
1310 static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D,
1311                                       QualType Type, OpenMPClauseKind CKind,
1312                                       SourceLocation ELoc,
1313                                       bool AcceptIfMutable = true,
1314                                       bool ListItemNotVar = false) {
1315   ASTContext &Context = SemaRef.getASTContext();
1316   bool IsClassType;
1317   if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) {
1318     unsigned Diag = ListItemNotVar
1319                         ? diag::err_omp_const_list_item
1320                         : IsClassType ? diag::err_omp_const_not_mutable_variable
1321                                       : diag::err_omp_const_variable;
1322     SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind);
1323     if (!ListItemNotVar && D) {
1324       const VarDecl *VD = dyn_cast<VarDecl>(D);
1325       bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
1326                                VarDecl::DeclarationOnly;
1327       SemaRef.Diag(D->getLocation(),
1328                    IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1329           << D;
1330     }
1331     return true;
1332   }
1333   return false;
1334 }
1335 
1336 const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1337                                                    bool FromParent) {
1338   D = getCanonicalDecl(D);
1339   DSAVarData DVar;
1340 
1341   auto *VD = dyn_cast<VarDecl>(D);
1342   auto TI = Threadprivates.find(D);
1343   if (TI != Threadprivates.end()) {
1344     DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
1345     DVar.CKind = OMPC_threadprivate;
1346     return DVar;
1347   }
1348   if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
1349     DVar.RefExpr = buildDeclRefExpr(
1350         SemaRef, VD, D->getType().getNonReferenceType(),
1351         VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
1352     DVar.CKind = OMPC_threadprivate;
1353     addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1354     return DVar;
1355   }
1356   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1357   // in a Construct, C/C++, predetermined, p.1]
1358   //  Variables appearing in threadprivate directives are threadprivate.
1359   if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
1360        !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1361          SemaRef.getLangOpts().OpenMPUseTLS &&
1362          SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
1363       (VD && VD->getStorageClass() == SC_Register &&
1364        VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
1365     DVar.RefExpr = buildDeclRefExpr(
1366         SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation());
1367     DVar.CKind = OMPC_threadprivate;
1368     addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1369     return DVar;
1370   }
1371   if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
1372       VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
1373       !isLoopControlVariable(D).first) {
1374     const_iterator IterTarget =
1375         std::find_if(begin(), end(), [](const SharingMapTy &Data) {
1376           return isOpenMPTargetExecutionDirective(Data.Directive);
1377         });
1378     if (IterTarget != end()) {
1379       const_iterator ParentIterTarget = IterTarget + 1;
1380       for (const_iterator Iter = begin();
1381            Iter != ParentIterTarget; ++Iter) {
1382         if (isOpenMPLocal(VD, Iter)) {
1383           DVar.RefExpr =
1384               buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1385                                D->getLocation());
1386           DVar.CKind = OMPC_threadprivate;
1387           return DVar;
1388         }
1389       }
1390       if (!isClauseParsingMode() || IterTarget != begin()) {
1391         auto DSAIter = IterTarget->SharingMap.find(D);
1392         if (DSAIter != IterTarget->SharingMap.end() &&
1393             isOpenMPPrivate(DSAIter->getSecond().Attributes)) {
1394           DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1395           DVar.CKind = OMPC_threadprivate;
1396           return DVar;
1397         }
1398         const_iterator End = end();
1399         if (!SemaRef.isOpenMPCapturedByRef(
1400                 D, std::distance(ParentIterTarget, End),
1401                 /*OpenMPCaptureLevel=*/0)) {
1402           DVar.RefExpr =
1403               buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1404                                IterTarget->ConstructLoc);
1405           DVar.CKind = OMPC_threadprivate;
1406           return DVar;
1407         }
1408       }
1409     }
1410   }
1411 
1412   if (isStackEmpty())
1413     // Not in OpenMP execution region and top scope was already checked.
1414     return DVar;
1415 
1416   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1417   // in a Construct, C/C++, predetermined, p.4]
1418   //  Static data members are shared.
1419   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1420   // in a Construct, C/C++, predetermined, p.7]
1421   //  Variables with static storage duration that are declared in a scope
1422   //  inside the construct are shared.
1423   if (VD && VD->isStaticDataMember()) {
1424     // Check for explicitly specified attributes.
1425     const_iterator I = begin();
1426     const_iterator EndI = end();
1427     if (FromParent && I != EndI)
1428       ++I;
1429     auto It = I->SharingMap.find(D);
1430     if (It != I->SharingMap.end()) {
1431       const DSAInfo &Data = It->getSecond();
1432       DVar.RefExpr = Data.RefExpr.getPointer();
1433       DVar.PrivateCopy = Data.PrivateCopy;
1434       DVar.CKind = Data.Attributes;
1435       DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1436       DVar.DKind = I->Directive;
1437       return DVar;
1438     }
1439 
1440     DVar.CKind = OMPC_shared;
1441     return DVar;
1442   }
1443 
1444   auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; };
1445   // The predetermined shared attribute for const-qualified types having no
1446   // mutable members was removed after OpenMP 3.1.
1447   if (SemaRef.LangOpts.OpenMP <= 31) {
1448     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1449     // in a Construct, C/C++, predetermined, p.6]
1450     //  Variables with const qualified type having no mutable member are
1451     //  shared.
1452     if (isConstNotMutableType(SemaRef, D->getType())) {
1453       // Variables with const-qualified type having no mutable member may be
1454       // listed in a firstprivate clause, even if they are static data members.
1455       DSAVarData DVarTemp = hasInnermostDSA(
1456           D,
1457           [](OpenMPClauseKind C) {
1458             return C == OMPC_firstprivate || C == OMPC_shared;
1459           },
1460           MatchesAlways, FromParent);
1461       if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
1462         return DVarTemp;
1463 
1464       DVar.CKind = OMPC_shared;
1465       return DVar;
1466     }
1467   }
1468 
1469   // Explicitly specified attributes and local variables with predetermined
1470   // attributes.
1471   const_iterator I = begin();
1472   const_iterator EndI = end();
1473   if (FromParent && I != EndI)
1474     ++I;
1475   auto It = I->SharingMap.find(D);
1476   if (It != I->SharingMap.end()) {
1477     const DSAInfo &Data = It->getSecond();
1478     DVar.RefExpr = Data.RefExpr.getPointer();
1479     DVar.PrivateCopy = Data.PrivateCopy;
1480     DVar.CKind = Data.Attributes;
1481     DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1482     DVar.DKind = I->Directive;
1483   }
1484 
1485   return DVar;
1486 }
1487 
1488 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1489                                                         bool FromParent) const {
1490   if (isStackEmpty()) {
1491     const_iterator I;
1492     return getDSA(I, D);
1493   }
1494   D = getCanonicalDecl(D);
1495   const_iterator StartI = begin();
1496   const_iterator EndI = end();
1497   if (FromParent && StartI != EndI)
1498     ++StartI;
1499   return getDSA(StartI, D);
1500 }
1501 
1502 const DSAStackTy::DSAVarData
1503 DSAStackTy::hasDSA(ValueDecl *D,
1504                    const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1505                    const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1506                    bool FromParent) const {
1507   if (isStackEmpty())
1508     return {};
1509   D = getCanonicalDecl(D);
1510   const_iterator I = begin();
1511   const_iterator EndI = end();
1512   if (FromParent && I != EndI)
1513     ++I;
1514   for (; I != EndI; ++I) {
1515     if (!DPred(I->Directive) &&
1516         !isImplicitOrExplicitTaskingRegion(I->Directive))
1517       continue;
1518     const_iterator NewI = I;
1519     DSAVarData DVar = getDSA(NewI, D);
1520     if (I == NewI && CPred(DVar.CKind))
1521       return DVar;
1522   }
1523   return {};
1524 }
1525 
1526 const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
1527     ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1528     const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1529     bool FromParent) const {
1530   if (isStackEmpty())
1531     return {};
1532   D = getCanonicalDecl(D);
1533   const_iterator StartI = begin();
1534   const_iterator EndI = end();
1535   if (FromParent && StartI != EndI)
1536     ++StartI;
1537   if (StartI == EndI || !DPred(StartI->Directive))
1538     return {};
1539   const_iterator NewI = StartI;
1540   DSAVarData DVar = getDSA(NewI, D);
1541   return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData();
1542 }
1543 
1544 bool DSAStackTy::hasExplicitDSA(
1545     const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1546     unsigned Level, bool NotLastprivate) const {
1547   if (getStackSize() <= Level)
1548     return false;
1549   D = getCanonicalDecl(D);
1550   const SharingMapTy &StackElem = getStackElemAtLevel(Level);
1551   auto I = StackElem.SharingMap.find(D);
1552   if (I != StackElem.SharingMap.end() &&
1553       I->getSecond().RefExpr.getPointer() &&
1554       CPred(I->getSecond().Attributes) &&
1555       (!NotLastprivate || !I->getSecond().RefExpr.getInt()))
1556     return true;
1557   // Check predetermined rules for the loop control variables.
1558   auto LI = StackElem.LCVMap.find(D);
1559   if (LI != StackElem.LCVMap.end())
1560     return CPred(OMPC_private);
1561   return false;
1562 }
1563 
1564 bool DSAStackTy::hasExplicitDirective(
1565     const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1566     unsigned Level) const {
1567   if (getStackSize() <= Level)
1568     return false;
1569   const SharingMapTy &StackElem = getStackElemAtLevel(Level);
1570   return DPred(StackElem.Directive);
1571 }
1572 
1573 bool DSAStackTy::hasDirective(
1574     const llvm::function_ref<bool(OpenMPDirectiveKind,
1575                                   const DeclarationNameInfo &, SourceLocation)>
1576         DPred,
1577     bool FromParent) const {
1578   // We look only in the enclosing region.
1579   size_t Skip = FromParent ? 2 : 1;
1580   for (const_iterator I = begin() + std::min(Skip, getStackSize()), E = end();
1581        I != E; ++I) {
1582     if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1583       return true;
1584   }
1585   return false;
1586 }
1587 
1588 void Sema::InitDataSharingAttributesStack() {
1589   VarDataSharingAttributesStack = new DSAStackTy(*this);
1590 }
1591 
1592 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1593 
1594 void Sema::pushOpenMPFunctionRegion() {
1595   DSAStack->pushFunction();
1596 }
1597 
1598 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1599   DSAStack->popFunction(OldFSI);
1600 }
1601 
1602 static bool isOpenMPDeviceDelayedContext(Sema &S) {
1603   assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice &&
1604          "Expected OpenMP device compilation.");
1605   return !S.isInOpenMPTargetExecutionDirective() &&
1606          !S.isInOpenMPDeclareTargetContext();
1607 }
1608 
1609 namespace {
1610 /// Status of the function emission on the host/device.
1611 enum class FunctionEmissionStatus {
1612   Emitted,
1613   Discarded,
1614   Unknown,
1615 };
1616 } // anonymous namespace
1617 
1618 Sema::DeviceDiagBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc,
1619                                                      unsigned DiagID) {
1620   assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice &&
1621          "Expected OpenMP device compilation.");
1622   FunctionEmissionStatus FES = getEmissionStatus(getCurFunctionDecl());
1623   DeviceDiagBuilder::Kind Kind = DeviceDiagBuilder::K_Nop;
1624   switch (FES) {
1625   case FunctionEmissionStatus::Emitted:
1626     Kind = DeviceDiagBuilder::K_Immediate;
1627     break;
1628   case FunctionEmissionStatus::Unknown:
1629     Kind = isOpenMPDeviceDelayedContext(*this) ? DeviceDiagBuilder::K_Deferred
1630                                                : DeviceDiagBuilder::K_Immediate;
1631     break;
1632   case FunctionEmissionStatus::TemplateDiscarded:
1633   case FunctionEmissionStatus::OMPDiscarded:
1634     Kind = DeviceDiagBuilder::K_Nop;
1635     break;
1636   case FunctionEmissionStatus::CUDADiscarded:
1637     llvm_unreachable("CUDADiscarded unexpected in OpenMP device compilation");
1638     break;
1639   }
1640 
1641   return DeviceDiagBuilder(Kind, Loc, DiagID, getCurFunctionDecl(), *this);
1642 }
1643 
1644 Sema::DeviceDiagBuilder Sema::diagIfOpenMPHostCode(SourceLocation Loc,
1645                                                    unsigned DiagID) {
1646   assert(LangOpts.OpenMP && !LangOpts.OpenMPIsDevice &&
1647          "Expected OpenMP host compilation.");
1648   FunctionEmissionStatus FES = getEmissionStatus(getCurFunctionDecl());
1649   DeviceDiagBuilder::Kind Kind = DeviceDiagBuilder::K_Nop;
1650   switch (FES) {
1651   case FunctionEmissionStatus::Emitted:
1652     Kind = DeviceDiagBuilder::K_Immediate;
1653     break;
1654   case FunctionEmissionStatus::Unknown:
1655     Kind = DeviceDiagBuilder::K_Deferred;
1656     break;
1657   case FunctionEmissionStatus::TemplateDiscarded:
1658   case FunctionEmissionStatus::OMPDiscarded:
1659   case FunctionEmissionStatus::CUDADiscarded:
1660     Kind = DeviceDiagBuilder::K_Nop;
1661     break;
1662   }
1663 
1664   return DeviceDiagBuilder(Kind, Loc, DiagID, getCurFunctionDecl(), *this);
1665 }
1666 
1667 void Sema::checkOpenMPDeviceFunction(SourceLocation Loc, FunctionDecl *Callee,
1668                                      bool CheckForDelayedContext) {
1669   assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice &&
1670          "Expected OpenMP device compilation.");
1671   assert(Callee && "Callee may not be null.");
1672   Callee = Callee->getMostRecentDecl();
1673   FunctionDecl *Caller = getCurFunctionDecl();
1674 
1675   // host only function are not available on the device.
1676   if (Caller) {
1677     FunctionEmissionStatus CallerS = getEmissionStatus(Caller);
1678     FunctionEmissionStatus CalleeS = getEmissionStatus(Callee);
1679     assert(CallerS != FunctionEmissionStatus::CUDADiscarded &&
1680            CalleeS != FunctionEmissionStatus::CUDADiscarded &&
1681            "CUDADiscarded unexpected in OpenMP device function check");
1682     if ((CallerS == FunctionEmissionStatus::Emitted ||
1683          (!isOpenMPDeviceDelayedContext(*this) &&
1684           CallerS == FunctionEmissionStatus::Unknown)) &&
1685         CalleeS == FunctionEmissionStatus::OMPDiscarded) {
1686       StringRef HostDevTy = getOpenMPSimpleClauseTypeName(
1687           OMPC_device_type, OMPC_DEVICE_TYPE_host);
1688       Diag(Loc, diag::err_omp_wrong_device_function_call) << HostDevTy << 0;
1689       Diag(Callee->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(),
1690            diag::note_omp_marked_device_type_here)
1691           << HostDevTy;
1692       return;
1693     }
1694   }
1695   // If the caller is known-emitted, mark the callee as known-emitted.
1696   // Otherwise, mark the call in our call graph so we can traverse it later.
1697   if ((CheckForDelayedContext && !isOpenMPDeviceDelayedContext(*this)) ||
1698       (!Caller && !CheckForDelayedContext) ||
1699       (Caller && getEmissionStatus(Caller) == FunctionEmissionStatus::Emitted))
1700     markKnownEmitted(*this, Caller, Callee, Loc,
1701                      [CheckForDelayedContext](Sema &S, FunctionDecl *FD) {
1702                        return CheckForDelayedContext &&
1703                               S.getEmissionStatus(FD) ==
1704                                   FunctionEmissionStatus::Emitted;
1705                      });
1706   else if (Caller)
1707     DeviceCallGraph[Caller].insert({Callee, Loc});
1708 }
1709 
1710 void Sema::checkOpenMPHostFunction(SourceLocation Loc, FunctionDecl *Callee,
1711                                    bool CheckCaller) {
1712   assert(LangOpts.OpenMP && !LangOpts.OpenMPIsDevice &&
1713          "Expected OpenMP host compilation.");
1714   assert(Callee && "Callee may not be null.");
1715   Callee = Callee->getMostRecentDecl();
1716   FunctionDecl *Caller = getCurFunctionDecl();
1717 
1718   // device only function are not available on the host.
1719   if (Caller) {
1720     FunctionEmissionStatus CallerS = getEmissionStatus(Caller);
1721     FunctionEmissionStatus CalleeS = getEmissionStatus(Callee);
1722     assert(
1723         (LangOpts.CUDA || (CallerS != FunctionEmissionStatus::CUDADiscarded &&
1724                            CalleeS != FunctionEmissionStatus::CUDADiscarded)) &&
1725         "CUDADiscarded unexpected in OpenMP host function check");
1726     if (CallerS == FunctionEmissionStatus::Emitted &&
1727         CalleeS == FunctionEmissionStatus::OMPDiscarded) {
1728       StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName(
1729           OMPC_device_type, OMPC_DEVICE_TYPE_nohost);
1730       Diag(Loc, diag::err_omp_wrong_device_function_call) << NoHostDevTy << 1;
1731       Diag(Callee->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(),
1732            diag::note_omp_marked_device_type_here)
1733           << NoHostDevTy;
1734       return;
1735     }
1736   }
1737   // If the caller is known-emitted, mark the callee as known-emitted.
1738   // Otherwise, mark the call in our call graph so we can traverse it later.
1739   if (!shouldIgnoreInHostDeviceCheck(Callee)) {
1740     if ((!CheckCaller && !Caller) ||
1741         (Caller &&
1742          getEmissionStatus(Caller) == FunctionEmissionStatus::Emitted))
1743       markKnownEmitted(
1744           *this, Caller, Callee, Loc, [CheckCaller](Sema &S, FunctionDecl *FD) {
1745             return CheckCaller &&
1746                    S.getEmissionStatus(FD) == FunctionEmissionStatus::Emitted;
1747           });
1748     else if (Caller)
1749       DeviceCallGraph[Caller].insert({Callee, Loc});
1750   }
1751 }
1752 
1753 void Sema::checkOpenMPDeviceExpr(const Expr *E) {
1754   assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice &&
1755          "OpenMP device compilation mode is expected.");
1756   QualType Ty = E->getType();
1757   if ((Ty->isFloat16Type() && !Context.getTargetInfo().hasFloat16Type()) ||
1758       ((Ty->isFloat128Type() ||
1759         (Ty->isRealFloatingType() && Context.getTypeSize(Ty) == 128)) &&
1760        !Context.getTargetInfo().hasFloat128Type()) ||
1761       (Ty->isIntegerType() && Context.getTypeSize(Ty) == 128 &&
1762        !Context.getTargetInfo().hasInt128Type()))
1763     targetDiag(E->getExprLoc(), diag::err_omp_unsupported_type)
1764         << static_cast<unsigned>(Context.getTypeSize(Ty)) << Ty
1765         << Context.getTargetInfo().getTriple().str() << E->getSourceRange();
1766 }
1767 
1768 static OpenMPDefaultmapClauseKind
1769 getVariableCategoryFromDecl(const LangOptions &LO, const ValueDecl *VD) {
1770   if (LO.OpenMP <= 45) {
1771     if (VD->getType().getNonReferenceType()->isScalarType())
1772       return OMPC_DEFAULTMAP_scalar;
1773     return OMPC_DEFAULTMAP_aggregate;
1774   }
1775   if (VD->getType().getNonReferenceType()->isAnyPointerType())
1776     return OMPC_DEFAULTMAP_pointer;
1777   if (VD->getType().getNonReferenceType()->isScalarType())
1778     return OMPC_DEFAULTMAP_scalar;
1779   return OMPC_DEFAULTMAP_aggregate;
1780 }
1781 
1782 bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level,
1783                                  unsigned OpenMPCaptureLevel) const {
1784   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1785 
1786   ASTContext &Ctx = getASTContext();
1787   bool IsByRef = true;
1788 
1789   // Find the directive that is associated with the provided scope.
1790   D = cast<ValueDecl>(D->getCanonicalDecl());
1791   QualType Ty = D->getType();
1792 
1793   bool IsVariableUsedInMapClause = false;
1794   if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
1795     // This table summarizes how a given variable should be passed to the device
1796     // given its type and the clauses where it appears. This table is based on
1797     // the description in OpenMP 4.5 [2.10.4, target Construct] and
1798     // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
1799     //
1800     // =========================================================================
1801     // | type |  defaultmap   | pvt | first | is_device_ptr |    map   | res.  |
1802     // |      |(tofrom:scalar)|     |  pvt  |               |          |       |
1803     // =========================================================================
1804     // | scl  |               |     |       |       -       |          | bycopy|
1805     // | scl  |               |  -  |   x   |       -       |     -    | bycopy|
1806     // | scl  |               |  x  |   -   |       -       |     -    | null  |
1807     // | scl  |       x       |     |       |       -       |          | byref |
1808     // | scl  |       x       |  -  |   x   |       -       |     -    | bycopy|
1809     // | scl  |       x       |  x  |   -   |       -       |     -    | null  |
1810     // | scl  |               |  -  |   -   |       -       |     x    | byref |
1811     // | scl  |       x       |  -  |   -   |       -       |     x    | byref |
1812     //
1813     // | agg  |      n.a.     |     |       |       -       |          | byref |
1814     // | agg  |      n.a.     |  -  |   x   |       -       |     -    | byref |
1815     // | agg  |      n.a.     |  x  |   -   |       -       |     -    | null  |
1816     // | agg  |      n.a.     |  -  |   -   |       -       |     x    | byref |
1817     // | agg  |      n.a.     |  -  |   -   |       -       |    x[]   | byref |
1818     //
1819     // | ptr  |      n.a.     |     |       |       -       |          | bycopy|
1820     // | ptr  |      n.a.     |  -  |   x   |       -       |     -    | bycopy|
1821     // | ptr  |      n.a.     |  x  |   -   |       -       |     -    | null  |
1822     // | ptr  |      n.a.     |  -  |   -   |       -       |     x    | byref |
1823     // | ptr  |      n.a.     |  -  |   -   |       -       |    x[]   | bycopy|
1824     // | ptr  |      n.a.     |  -  |   -   |       x       |          | bycopy|
1825     // | ptr  |      n.a.     |  -  |   -   |       x       |     x    | bycopy|
1826     // | ptr  |      n.a.     |  -  |   -   |       x       |    x[]   | bycopy|
1827     // =========================================================================
1828     // Legend:
1829     //  scl - scalar
1830     //  ptr - pointer
1831     //  agg - aggregate
1832     //  x - applies
1833     //  - - invalid in this combination
1834     //  [] - mapped with an array section
1835     //  byref - should be mapped by reference
1836     //  byval - should be mapped by value
1837     //  null - initialize a local variable to null on the device
1838     //
1839     // Observations:
1840     //  - All scalar declarations that show up in a map clause have to be passed
1841     //    by reference, because they may have been mapped in the enclosing data
1842     //    environment.
1843     //  - If the scalar value does not fit the size of uintptr, it has to be
1844     //    passed by reference, regardless the result in the table above.
1845     //  - For pointers mapped by value that have either an implicit map or an
1846     //    array section, the runtime library may pass the NULL value to the
1847     //    device instead of the value passed to it by the compiler.
1848 
1849     if (Ty->isReferenceType())
1850       Ty = Ty->castAs<ReferenceType>()->getPointeeType();
1851 
1852     // Locate map clauses and see if the variable being captured is referred to
1853     // in any of those clauses. Here we only care about variables, not fields,
1854     // because fields are part of aggregates.
1855     bool IsVariableAssociatedWithSection = false;
1856 
1857     DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1858         D, Level,
1859         [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D](
1860             OMPClauseMappableExprCommon::MappableExprComponentListRef
1861                 MapExprComponents,
1862             OpenMPClauseKind WhereFoundClauseKind) {
1863           // Only the map clause information influences how a variable is
1864           // captured. E.g. is_device_ptr does not require changing the default
1865           // behavior.
1866           if (WhereFoundClauseKind != OMPC_map)
1867             return false;
1868 
1869           auto EI = MapExprComponents.rbegin();
1870           auto EE = MapExprComponents.rend();
1871 
1872           assert(EI != EE && "Invalid map expression!");
1873 
1874           if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
1875             IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
1876 
1877           ++EI;
1878           if (EI == EE)
1879             return false;
1880 
1881           if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
1882               isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
1883               isa<MemberExpr>(EI->getAssociatedExpression())) {
1884             IsVariableAssociatedWithSection = true;
1885             // There is nothing more we need to know about this variable.
1886             return true;
1887           }
1888 
1889           // Keep looking for more map info.
1890           return false;
1891         });
1892 
1893     if (IsVariableUsedInMapClause) {
1894       // If variable is identified in a map clause it is always captured by
1895       // reference except if it is a pointer that is dereferenced somehow.
1896       IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
1897     } else {
1898       // By default, all the data that has a scalar type is mapped by copy
1899       // (except for reduction variables).
1900       // Defaultmap scalar is mutual exclusive to defaultmap pointer
1901       IsByRef =
1902           (DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
1903            !Ty->isAnyPointerType()) ||
1904           !Ty->isScalarType() ||
1905           DSAStack->isDefaultmapCapturedByRef(
1906               Level, getVariableCategoryFromDecl(LangOpts, D)) ||
1907           DSAStack->hasExplicitDSA(
1908               D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level);
1909     }
1910   }
1911 
1912   if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
1913     IsByRef =
1914         ((IsVariableUsedInMapClause &&
1915           DSAStack->getCaptureRegion(Level, OpenMPCaptureLevel) ==
1916               OMPD_target) ||
1917          !DSAStack->hasExplicitDSA(
1918              D,
1919              [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
1920              Level, /*NotLastprivate=*/true)) &&
1921         // If the variable is artificial and must be captured by value - try to
1922         // capture by value.
1923         !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
1924           !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue());
1925   }
1926 
1927   // When passing data by copy, we need to make sure it fits the uintptr size
1928   // and alignment, because the runtime library only deals with uintptr types.
1929   // If it does not fit the uintptr size, we need to pass the data by reference
1930   // instead.
1931   if (!IsByRef &&
1932       (Ctx.getTypeSizeInChars(Ty) >
1933            Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
1934        Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
1935     IsByRef = true;
1936   }
1937 
1938   return IsByRef;
1939 }
1940 
1941 unsigned Sema::getOpenMPNestingLevel() const {
1942   assert(getLangOpts().OpenMP);
1943   return DSAStack->getNestingLevel();
1944 }
1945 
1946 bool Sema::isInOpenMPTargetExecutionDirective() const {
1947   return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
1948           !DSAStack->isClauseParsingMode()) ||
1949          DSAStack->hasDirective(
1950              [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
1951                 SourceLocation) -> bool {
1952                return isOpenMPTargetExecutionDirective(K);
1953              },
1954              false);
1955 }
1956 
1957 VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo,
1958                                     unsigned StopAt) {
1959   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1960   D = getCanonicalDecl(D);
1961 
1962   auto *VD = dyn_cast<VarDecl>(D);
1963   // Do not capture constexpr variables.
1964   if (VD && VD->isConstexpr())
1965     return nullptr;
1966 
1967   // If we want to determine whether the variable should be captured from the
1968   // perspective of the current capturing scope, and we've already left all the
1969   // capturing scopes of the top directive on the stack, check from the
1970   // perspective of its parent directive (if any) instead.
1971   DSAStackTy::ParentDirectiveScope InParentDirectiveRAII(
1972       *DSAStack, CheckScopeInfo && DSAStack->isBodyComplete());
1973 
1974   // If we are attempting to capture a global variable in a directive with
1975   // 'target' we return true so that this global is also mapped to the device.
1976   //
1977   if (VD && !VD->hasLocalStorage() &&
1978       (getCurCapturedRegion() || getCurBlock() || getCurLambda())) {
1979     if (isInOpenMPDeclareTargetContext()) {
1980       // Try to mark variable as declare target if it is used in capturing
1981       // regions.
1982       if (LangOpts.OpenMP <= 45 &&
1983           !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
1984         checkDeclIsAllowedInOpenMPTarget(nullptr, VD);
1985       return nullptr;
1986     } else if (isInOpenMPTargetExecutionDirective()) {
1987       // If the declaration is enclosed in a 'declare target' directive,
1988       // then it should not be captured.
1989       //
1990       if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
1991         return nullptr;
1992       return VD;
1993     }
1994   }
1995 
1996   if (CheckScopeInfo) {
1997     bool OpenMPFound = false;
1998     for (unsigned I = StopAt + 1; I > 0; --I) {
1999       FunctionScopeInfo *FSI = FunctionScopes[I - 1];
2000       if(!isa<CapturingScopeInfo>(FSI))
2001         return nullptr;
2002       if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI))
2003         if (RSI->CapRegionKind == CR_OpenMP) {
2004           OpenMPFound = true;
2005           break;
2006         }
2007     }
2008     if (!OpenMPFound)
2009       return nullptr;
2010   }
2011 
2012   if (DSAStack->getCurrentDirective() != OMPD_unknown &&
2013       (!DSAStack->isClauseParsingMode() ||
2014        DSAStack->getParentDirective() != OMPD_unknown)) {
2015     auto &&Info = DSAStack->isLoopControlVariable(D);
2016     if (Info.first ||
2017         (VD && VD->hasLocalStorage() &&
2018          isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) ||
2019         (VD && DSAStack->isForceVarCapturing()))
2020       return VD ? VD : Info.second;
2021     DSAStackTy::DSAVarData DVarPrivate =
2022         DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
2023     if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
2024       return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
2025     // Threadprivate variables must not be captured.
2026     if (isOpenMPThreadPrivate(DVarPrivate.CKind))
2027       return nullptr;
2028     // The variable is not private or it is the variable in the directive with
2029     // default(none) clause and not used in any clause.
2030     DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate,
2031                                    [](OpenMPDirectiveKind) { return true; },
2032                                    DSAStack->isClauseParsingMode());
2033     if (DVarPrivate.CKind != OMPC_unknown ||
2034         (VD && DSAStack->getDefaultDSA() == DSA_none))
2035       return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
2036   }
2037   return nullptr;
2038 }
2039 
2040 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
2041                                         unsigned Level) const {
2042   SmallVector<OpenMPDirectiveKind, 4> Regions;
2043   getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
2044   FunctionScopesIndex -= Regions.size();
2045 }
2046 
2047 void Sema::startOpenMPLoop() {
2048   assert(LangOpts.OpenMP && "OpenMP must be enabled.");
2049   if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()))
2050     DSAStack->loopInit();
2051 }
2052 
2053 void Sema::startOpenMPCXXRangeFor() {
2054   assert(LangOpts.OpenMP && "OpenMP must be enabled.");
2055   if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2056     DSAStack->resetPossibleLoopCounter();
2057     DSAStack->loopStart();
2058   }
2059 }
2060 
2061 bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const {
2062   assert(LangOpts.OpenMP && "OpenMP is not allowed");
2063   if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2064     if (DSAStack->getAssociatedLoops() > 0 &&
2065         !DSAStack->isLoopStarted()) {
2066       DSAStack->resetPossibleLoopCounter(D);
2067       DSAStack->loopStart();
2068       return true;
2069     }
2070     if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() ||
2071          DSAStack->isLoopControlVariable(D).first) &&
2072         !DSAStack->hasExplicitDSA(
2073             D, [](OpenMPClauseKind K) { return K != OMPC_private; }, Level) &&
2074         !isOpenMPSimdDirective(DSAStack->getCurrentDirective()))
2075       return true;
2076   }
2077   if (const auto *VD = dyn_cast<VarDecl>(D)) {
2078     if (DSAStack->isThreadPrivate(const_cast<VarDecl *>(VD)) &&
2079         DSAStack->isForceVarCapturing() &&
2080         !DSAStack->hasExplicitDSA(
2081             D, [](OpenMPClauseKind K) { return K == OMPC_copyin; }, Level))
2082       return true;
2083   }
2084   return DSAStack->hasExplicitDSA(
2085              D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) ||
2086          (DSAStack->isClauseParsingMode() &&
2087           DSAStack->getClauseParsingMode() == OMPC_private) ||
2088          // Consider taskgroup reduction descriptor variable a private to avoid
2089          // possible capture in the region.
2090          (DSAStack->hasExplicitDirective(
2091               [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; },
2092               Level) &&
2093           DSAStack->isTaskgroupReductionRef(D, Level));
2094 }
2095 
2096 void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
2097                                 unsigned Level) {
2098   assert(LangOpts.OpenMP && "OpenMP is not allowed");
2099   D = getCanonicalDecl(D);
2100   OpenMPClauseKind OMPC = OMPC_unknown;
2101   for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
2102     const unsigned NewLevel = I - 1;
2103     if (DSAStack->hasExplicitDSA(D,
2104                                  [&OMPC](const OpenMPClauseKind K) {
2105                                    if (isOpenMPPrivate(K)) {
2106                                      OMPC = K;
2107                                      return true;
2108                                    }
2109                                    return false;
2110                                  },
2111                                  NewLevel))
2112       break;
2113     if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
2114             D, NewLevel,
2115             [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
2116                OpenMPClauseKind) { return true; })) {
2117       OMPC = OMPC_map;
2118       break;
2119     }
2120     if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
2121                                        NewLevel)) {
2122       OMPC = OMPC_map;
2123       if (DSAStack->mustBeFirstprivateAtLevel(
2124               NewLevel, getVariableCategoryFromDecl(LangOpts, D)))
2125         OMPC = OMPC_firstprivate;
2126       break;
2127     }
2128   }
2129   if (OMPC != OMPC_unknown)
2130     FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC));
2131 }
2132 
2133 bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D,
2134                                       unsigned Level) const {
2135   assert(LangOpts.OpenMP && "OpenMP is not allowed");
2136   // Return true if the current level is no longer enclosed in a target region.
2137 
2138   const auto *VD = dyn_cast<VarDecl>(D);
2139   return VD && !VD->hasLocalStorage() &&
2140          DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
2141                                         Level);
2142 }
2143 
2144 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
2145 
2146 void Sema::finalizeOpenMPDelayedAnalysis() {
2147   assert(LangOpts.OpenMP && "Expected OpenMP compilation mode.");
2148   // Diagnose implicit declare target functions and their callees.
2149   for (const auto &CallerCallees : DeviceCallGraph) {
2150     Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
2151         OMPDeclareTargetDeclAttr::getDeviceType(
2152             CallerCallees.getFirst()->getMostRecentDecl());
2153     // Ignore host functions during device analyzis.
2154     if (LangOpts.OpenMPIsDevice && DevTy &&
2155         *DevTy == OMPDeclareTargetDeclAttr::DT_Host)
2156       continue;
2157     // Ignore nohost functions during host analyzis.
2158     if (!LangOpts.OpenMPIsDevice && DevTy &&
2159         *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost)
2160       continue;
2161     for (const std::pair<CanonicalDeclPtr<FunctionDecl>, SourceLocation>
2162              &Callee : CallerCallees.getSecond()) {
2163       const FunctionDecl *FD = Callee.first->getMostRecentDecl();
2164       Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
2165           OMPDeclareTargetDeclAttr::getDeviceType(FD);
2166       if (LangOpts.OpenMPIsDevice && DevTy &&
2167           *DevTy == OMPDeclareTargetDeclAttr::DT_Host) {
2168         // Diagnose host function called during device codegen.
2169         StringRef HostDevTy = getOpenMPSimpleClauseTypeName(
2170             OMPC_device_type, OMPC_DEVICE_TYPE_host);
2171         Diag(Callee.second, diag::err_omp_wrong_device_function_call)
2172             << HostDevTy << 0;
2173         Diag(FD->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(),
2174              diag::note_omp_marked_device_type_here)
2175             << HostDevTy;
2176         continue;
2177       }
2178       if (!LangOpts.OpenMPIsDevice && DevTy &&
2179           *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) {
2180         // Diagnose nohost function called during host codegen.
2181         StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName(
2182             OMPC_device_type, OMPC_DEVICE_TYPE_nohost);
2183         Diag(Callee.second, diag::err_omp_wrong_device_function_call)
2184             << NoHostDevTy << 1;
2185         Diag(FD->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(),
2186              diag::note_omp_marked_device_type_here)
2187             << NoHostDevTy;
2188         continue;
2189       }
2190     }
2191   }
2192 }
2193 
2194 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
2195                                const DeclarationNameInfo &DirName,
2196                                Scope *CurScope, SourceLocation Loc) {
2197   DSAStack->push(DKind, DirName, CurScope, Loc);
2198   PushExpressionEvaluationContext(
2199       ExpressionEvaluationContext::PotentiallyEvaluated);
2200 }
2201 
2202 void Sema::StartOpenMPClause(OpenMPClauseKind K) {
2203   DSAStack->setClauseParsingMode(K);
2204 }
2205 
2206 void Sema::EndOpenMPClause() {
2207   DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
2208 }
2209 
2210 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
2211                                  ArrayRef<OMPClause *> Clauses);
2212 
2213 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
2214   // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
2215   //  A variable of class type (or array thereof) that appears in a lastprivate
2216   //  clause requires an accessible, unambiguous default constructor for the
2217   //  class type, unless the list item is also specified in a firstprivate
2218   //  clause.
2219   if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
2220     for (OMPClause *C : D->clauses()) {
2221       if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
2222         SmallVector<Expr *, 8> PrivateCopies;
2223         for (Expr *DE : Clause->varlists()) {
2224           if (DE->isValueDependent() || DE->isTypeDependent()) {
2225             PrivateCopies.push_back(nullptr);
2226             continue;
2227           }
2228           auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
2229           auto *VD = cast<VarDecl>(DRE->getDecl());
2230           QualType Type = VD->getType().getNonReferenceType();
2231           const DSAStackTy::DSAVarData DVar =
2232               DSAStack->getTopDSA(VD, /*FromParent=*/false);
2233           if (DVar.CKind == OMPC_lastprivate) {
2234             // Generate helper private variable and initialize it with the
2235             // default value. The address of the original variable is replaced
2236             // by the address of the new private variable in CodeGen. This new
2237             // variable is not added to IdResolver, so the code in the OpenMP
2238             // region uses original variable for proper diagnostics.
2239             VarDecl *VDPrivate = buildVarDecl(
2240                 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
2241                 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE);
2242             ActOnUninitializedDecl(VDPrivate);
2243             if (VDPrivate->isInvalidDecl()) {
2244               PrivateCopies.push_back(nullptr);
2245               continue;
2246             }
2247             PrivateCopies.push_back(buildDeclRefExpr(
2248                 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
2249           } else {
2250             // The variable is also a firstprivate, so initialization sequence
2251             // for private copy is generated already.
2252             PrivateCopies.push_back(nullptr);
2253           }
2254         }
2255         Clause->setPrivateCopies(PrivateCopies);
2256       }
2257     }
2258     // Check allocate clauses.
2259     if (!CurContext->isDependentContext())
2260       checkAllocateClauses(*this, DSAStack, D->clauses());
2261   }
2262 
2263   DSAStack->pop();
2264   DiscardCleanupsInEvaluationContext();
2265   PopExpressionEvaluationContext();
2266 }
2267 
2268 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
2269                                      Expr *NumIterations, Sema &SemaRef,
2270                                      Scope *S, DSAStackTy *Stack);
2271 
2272 namespace {
2273 
2274 class VarDeclFilterCCC final : public CorrectionCandidateCallback {
2275 private:
2276   Sema &SemaRef;
2277 
2278 public:
2279   explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
2280   bool ValidateCandidate(const TypoCorrection &Candidate) override {
2281     NamedDecl *ND = Candidate.getCorrectionDecl();
2282     if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
2283       return VD->hasGlobalStorage() &&
2284              SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
2285                                    SemaRef.getCurScope());
2286     }
2287     return false;
2288   }
2289 
2290   std::unique_ptr<CorrectionCandidateCallback> clone() override {
2291     return std::make_unique<VarDeclFilterCCC>(*this);
2292   }
2293 
2294 };
2295 
2296 class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
2297 private:
2298   Sema &SemaRef;
2299 
2300 public:
2301   explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
2302   bool ValidateCandidate(const TypoCorrection &Candidate) override {
2303     NamedDecl *ND = Candidate.getCorrectionDecl();
2304     if (ND && ((isa<VarDecl>(ND) && ND->getKind() == Decl::Var) ||
2305                isa<FunctionDecl>(ND))) {
2306       return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
2307                                    SemaRef.getCurScope());
2308     }
2309     return false;
2310   }
2311 
2312   std::unique_ptr<CorrectionCandidateCallback> clone() override {
2313     return std::make_unique<VarOrFuncDeclFilterCCC>(*this);
2314   }
2315 };
2316 
2317 } // namespace
2318 
2319 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
2320                                          CXXScopeSpec &ScopeSpec,
2321                                          const DeclarationNameInfo &Id,
2322                                          OpenMPDirectiveKind Kind) {
2323   LookupResult Lookup(*this, Id, LookupOrdinaryName);
2324   LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
2325 
2326   if (Lookup.isAmbiguous())
2327     return ExprError();
2328 
2329   VarDecl *VD;
2330   if (!Lookup.isSingleResult()) {
2331     VarDeclFilterCCC CCC(*this);
2332     if (TypoCorrection Corrected =
2333             CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC,
2334                         CTK_ErrorRecovery)) {
2335       diagnoseTypo(Corrected,
2336                    PDiag(Lookup.empty()
2337                              ? diag::err_undeclared_var_use_suggest
2338                              : diag::err_omp_expected_var_arg_suggest)
2339                        << Id.getName());
2340       VD = Corrected.getCorrectionDeclAs<VarDecl>();
2341     } else {
2342       Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
2343                                        : diag::err_omp_expected_var_arg)
2344           << Id.getName();
2345       return ExprError();
2346     }
2347   } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
2348     Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
2349     Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
2350     return ExprError();
2351   }
2352   Lookup.suppressDiagnostics();
2353 
2354   // OpenMP [2.9.2, Syntax, C/C++]
2355   //   Variables must be file-scope, namespace-scope, or static block-scope.
2356   if (Kind == OMPD_threadprivate && !VD->hasGlobalStorage()) {
2357     Diag(Id.getLoc(), diag::err_omp_global_var_arg)
2358         << getOpenMPDirectiveName(Kind) << !VD->isStaticLocal();
2359     bool IsDecl =
2360         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2361     Diag(VD->getLocation(),
2362          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2363         << VD;
2364     return ExprError();
2365   }
2366 
2367   VarDecl *CanonicalVD = VD->getCanonicalDecl();
2368   NamedDecl *ND = CanonicalVD;
2369   // OpenMP [2.9.2, Restrictions, C/C++, p.2]
2370   //   A threadprivate directive for file-scope variables must appear outside
2371   //   any definition or declaration.
2372   if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
2373       !getCurLexicalContext()->isTranslationUnit()) {
2374     Diag(Id.getLoc(), diag::err_omp_var_scope)
2375         << getOpenMPDirectiveName(Kind) << VD;
2376     bool IsDecl =
2377         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2378     Diag(VD->getLocation(),
2379          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2380         << VD;
2381     return ExprError();
2382   }
2383   // OpenMP [2.9.2, Restrictions, C/C++, p.3]
2384   //   A threadprivate directive for static class member variables must appear
2385   //   in the class definition, in the same scope in which the member
2386   //   variables are declared.
2387   if (CanonicalVD->isStaticDataMember() &&
2388       !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
2389     Diag(Id.getLoc(), diag::err_omp_var_scope)
2390         << getOpenMPDirectiveName(Kind) << VD;
2391     bool IsDecl =
2392         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2393     Diag(VD->getLocation(),
2394          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2395         << VD;
2396     return ExprError();
2397   }
2398   // OpenMP [2.9.2, Restrictions, C/C++, p.4]
2399   //   A threadprivate directive for namespace-scope variables must appear
2400   //   outside any definition or declaration other than the namespace
2401   //   definition itself.
2402   if (CanonicalVD->getDeclContext()->isNamespace() &&
2403       (!getCurLexicalContext()->isFileContext() ||
2404        !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
2405     Diag(Id.getLoc(), diag::err_omp_var_scope)
2406         << getOpenMPDirectiveName(Kind) << VD;
2407     bool IsDecl =
2408         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2409     Diag(VD->getLocation(),
2410          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2411         << VD;
2412     return ExprError();
2413   }
2414   // OpenMP [2.9.2, Restrictions, C/C++, p.6]
2415   //   A threadprivate directive for static block-scope variables must appear
2416   //   in the scope of the variable and not in a nested scope.
2417   if (CanonicalVD->isLocalVarDecl() && CurScope &&
2418       !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
2419     Diag(Id.getLoc(), diag::err_omp_var_scope)
2420         << getOpenMPDirectiveName(Kind) << VD;
2421     bool IsDecl =
2422         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2423     Diag(VD->getLocation(),
2424          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2425         << VD;
2426     return ExprError();
2427   }
2428 
2429   // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
2430   //   A threadprivate directive must lexically precede all references to any
2431   //   of the variables in its list.
2432   if (Kind == OMPD_threadprivate && VD->isUsed() &&
2433       !DSAStack->isThreadPrivate(VD)) {
2434     Diag(Id.getLoc(), diag::err_omp_var_used)
2435         << getOpenMPDirectiveName(Kind) << VD;
2436     return ExprError();
2437   }
2438 
2439   QualType ExprType = VD->getType().getNonReferenceType();
2440   return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
2441                              SourceLocation(), VD,
2442                              /*RefersToEnclosingVariableOrCapture=*/false,
2443                              Id.getLoc(), ExprType, VK_LValue);
2444 }
2445 
2446 Sema::DeclGroupPtrTy
2447 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
2448                                         ArrayRef<Expr *> VarList) {
2449   if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
2450     CurContext->addDecl(D);
2451     return DeclGroupPtrTy::make(DeclGroupRef(D));
2452   }
2453   return nullptr;
2454 }
2455 
2456 namespace {
2457 class LocalVarRefChecker final
2458     : public ConstStmtVisitor<LocalVarRefChecker, bool> {
2459   Sema &SemaRef;
2460 
2461 public:
2462   bool VisitDeclRefExpr(const DeclRefExpr *E) {
2463     if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
2464       if (VD->hasLocalStorage()) {
2465         SemaRef.Diag(E->getBeginLoc(),
2466                      diag::err_omp_local_var_in_threadprivate_init)
2467             << E->getSourceRange();
2468         SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
2469             << VD << VD->getSourceRange();
2470         return true;
2471       }
2472     }
2473     return false;
2474   }
2475   bool VisitStmt(const Stmt *S) {
2476     for (const Stmt *Child : S->children()) {
2477       if (Child && Visit(Child))
2478         return true;
2479     }
2480     return false;
2481   }
2482   explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
2483 };
2484 } // namespace
2485 
2486 OMPThreadPrivateDecl *
2487 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
2488   SmallVector<Expr *, 8> Vars;
2489   for (Expr *RefExpr : VarList) {
2490     auto *DE = cast<DeclRefExpr>(RefExpr);
2491     auto *VD = cast<VarDecl>(DE->getDecl());
2492     SourceLocation ILoc = DE->getExprLoc();
2493 
2494     // Mark variable as used.
2495     VD->setReferenced();
2496     VD->markUsed(Context);
2497 
2498     QualType QType = VD->getType();
2499     if (QType->isDependentType() || QType->isInstantiationDependentType()) {
2500       // It will be analyzed later.
2501       Vars.push_back(DE);
2502       continue;
2503     }
2504 
2505     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2506     //   A threadprivate variable must not have an incomplete type.
2507     if (RequireCompleteType(ILoc, VD->getType(),
2508                             diag::err_omp_threadprivate_incomplete_type)) {
2509       continue;
2510     }
2511 
2512     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2513     //   A threadprivate variable must not have a reference type.
2514     if (VD->getType()->isReferenceType()) {
2515       Diag(ILoc, diag::err_omp_ref_type_arg)
2516           << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
2517       bool IsDecl =
2518           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2519       Diag(VD->getLocation(),
2520            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2521           << VD;
2522       continue;
2523     }
2524 
2525     // Check if this is a TLS variable. If TLS is not being supported, produce
2526     // the corresponding diagnostic.
2527     if ((VD->getTLSKind() != VarDecl::TLS_None &&
2528          !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
2529            getLangOpts().OpenMPUseTLS &&
2530            getASTContext().getTargetInfo().isTLSSupported())) ||
2531         (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
2532          !VD->isLocalVarDecl())) {
2533       Diag(ILoc, diag::err_omp_var_thread_local)
2534           << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
2535       bool IsDecl =
2536           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2537       Diag(VD->getLocation(),
2538            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2539           << VD;
2540       continue;
2541     }
2542 
2543     // Check if initial value of threadprivate variable reference variable with
2544     // local storage (it is not supported by runtime).
2545     if (const Expr *Init = VD->getAnyInitializer()) {
2546       LocalVarRefChecker Checker(*this);
2547       if (Checker.Visit(Init))
2548         continue;
2549     }
2550 
2551     Vars.push_back(RefExpr);
2552     DSAStack->addDSA(VD, DE, OMPC_threadprivate);
2553     VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
2554         Context, SourceRange(Loc, Loc)));
2555     if (ASTMutationListener *ML = Context.getASTMutationListener())
2556       ML->DeclarationMarkedOpenMPThreadPrivate(VD);
2557   }
2558   OMPThreadPrivateDecl *D = nullptr;
2559   if (!Vars.empty()) {
2560     D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
2561                                      Vars);
2562     D->setAccess(AS_public);
2563   }
2564   return D;
2565 }
2566 
2567 static OMPAllocateDeclAttr::AllocatorTypeTy
2568 getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) {
2569   if (!Allocator)
2570     return OMPAllocateDeclAttr::OMPDefaultMemAlloc;
2571   if (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
2572       Allocator->isInstantiationDependent() ||
2573       Allocator->containsUnexpandedParameterPack())
2574     return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
2575   auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
2576   const Expr *AE = Allocator->IgnoreParenImpCasts();
2577   for (int I = OMPAllocateDeclAttr::OMPDefaultMemAlloc;
2578        I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
2579     auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
2580     const Expr *DefAllocator = Stack->getAllocator(AllocatorKind);
2581     llvm::FoldingSetNodeID AEId, DAEId;
2582     AE->Profile(AEId, S.getASTContext(), /*Canonical=*/true);
2583     DefAllocator->Profile(DAEId, S.getASTContext(), /*Canonical=*/true);
2584     if (AEId == DAEId) {
2585       AllocatorKindRes = AllocatorKind;
2586       break;
2587     }
2588   }
2589   return AllocatorKindRes;
2590 }
2591 
2592 static bool checkPreviousOMPAllocateAttribute(
2593     Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD,
2594     OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) {
2595   if (!VD->hasAttr<OMPAllocateDeclAttr>())
2596     return false;
2597   const auto *A = VD->getAttr<OMPAllocateDeclAttr>();
2598   Expr *PrevAllocator = A->getAllocator();
2599   OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind =
2600       getAllocatorKind(S, Stack, PrevAllocator);
2601   bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind;
2602   if (AllocatorsMatch &&
2603       AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc &&
2604       Allocator && PrevAllocator) {
2605     const Expr *AE = Allocator->IgnoreParenImpCasts();
2606     const Expr *PAE = PrevAllocator->IgnoreParenImpCasts();
2607     llvm::FoldingSetNodeID AEId, PAEId;
2608     AE->Profile(AEId, S.Context, /*Canonical=*/true);
2609     PAE->Profile(PAEId, S.Context, /*Canonical=*/true);
2610     AllocatorsMatch = AEId == PAEId;
2611   }
2612   if (!AllocatorsMatch) {
2613     SmallString<256> AllocatorBuffer;
2614     llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer);
2615     if (Allocator)
2616       Allocator->printPretty(AllocatorStream, nullptr, S.getPrintingPolicy());
2617     SmallString<256> PrevAllocatorBuffer;
2618     llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer);
2619     if (PrevAllocator)
2620       PrevAllocator->printPretty(PrevAllocatorStream, nullptr,
2621                                  S.getPrintingPolicy());
2622 
2623     SourceLocation AllocatorLoc =
2624         Allocator ? Allocator->getExprLoc() : RefExpr->getExprLoc();
2625     SourceRange AllocatorRange =
2626         Allocator ? Allocator->getSourceRange() : RefExpr->getSourceRange();
2627     SourceLocation PrevAllocatorLoc =
2628         PrevAllocator ? PrevAllocator->getExprLoc() : A->getLocation();
2629     SourceRange PrevAllocatorRange =
2630         PrevAllocator ? PrevAllocator->getSourceRange() : A->getRange();
2631     S.Diag(AllocatorLoc, diag::warn_omp_used_different_allocator)
2632         << (Allocator ? 1 : 0) << AllocatorStream.str()
2633         << (PrevAllocator ? 1 : 0) << PrevAllocatorStream.str()
2634         << AllocatorRange;
2635     S.Diag(PrevAllocatorLoc, diag::note_omp_previous_allocator)
2636         << PrevAllocatorRange;
2637     return true;
2638   }
2639   return false;
2640 }
2641 
2642 static void
2643 applyOMPAllocateAttribute(Sema &S, VarDecl *VD,
2644                           OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
2645                           Expr *Allocator, SourceRange SR) {
2646   if (VD->hasAttr<OMPAllocateDeclAttr>())
2647     return;
2648   if (Allocator &&
2649       (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
2650        Allocator->isInstantiationDependent() ||
2651        Allocator->containsUnexpandedParameterPack()))
2652     return;
2653   auto *A = OMPAllocateDeclAttr::CreateImplicit(S.Context, AllocatorKind,
2654                                                 Allocator, SR);
2655   VD->addAttr(A);
2656   if (ASTMutationListener *ML = S.Context.getASTMutationListener())
2657     ML->DeclarationMarkedOpenMPAllocate(VD, A);
2658 }
2659 
2660 Sema::DeclGroupPtrTy Sema::ActOnOpenMPAllocateDirective(
2661     SourceLocation Loc, ArrayRef<Expr *> VarList,
2662     ArrayRef<OMPClause *> Clauses, DeclContext *Owner) {
2663   assert(Clauses.size() <= 1 && "Expected at most one clause.");
2664   Expr *Allocator = nullptr;
2665   if (Clauses.empty()) {
2666     // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions.
2667     // allocate directives that appear in a target region must specify an
2668     // allocator clause unless a requires directive with the dynamic_allocators
2669     // clause is present in the same compilation unit.
2670     if (LangOpts.OpenMPIsDevice &&
2671         !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
2672       targetDiag(Loc, diag::err_expected_allocator_clause);
2673   } else {
2674     Allocator = cast<OMPAllocatorClause>(Clauses.back())->getAllocator();
2675   }
2676   OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
2677       getAllocatorKind(*this, DSAStack, Allocator);
2678   SmallVector<Expr *, 8> Vars;
2679   for (Expr *RefExpr : VarList) {
2680     auto *DE = cast<DeclRefExpr>(RefExpr);
2681     auto *VD = cast<VarDecl>(DE->getDecl());
2682 
2683     // Check if this is a TLS variable or global register.
2684     if (VD->getTLSKind() != VarDecl::TLS_None ||
2685         VD->hasAttr<OMPThreadPrivateDeclAttr>() ||
2686         (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
2687          !VD->isLocalVarDecl()))
2688       continue;
2689 
2690     // If the used several times in the allocate directive, the same allocator
2691     // must be used.
2692     if (checkPreviousOMPAllocateAttribute(*this, DSAStack, RefExpr, VD,
2693                                           AllocatorKind, Allocator))
2694       continue;
2695 
2696     // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++
2697     // If a list item has a static storage type, the allocator expression in the
2698     // allocator clause must be a constant expression that evaluates to one of
2699     // the predefined memory allocator values.
2700     if (Allocator && VD->hasGlobalStorage()) {
2701       if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) {
2702         Diag(Allocator->getExprLoc(),
2703              diag::err_omp_expected_predefined_allocator)
2704             << Allocator->getSourceRange();
2705         bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2706                       VarDecl::DeclarationOnly;
2707         Diag(VD->getLocation(),
2708              IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2709             << VD;
2710         continue;
2711       }
2712     }
2713 
2714     Vars.push_back(RefExpr);
2715     applyOMPAllocateAttribute(*this, VD, AllocatorKind, Allocator,
2716                               DE->getSourceRange());
2717   }
2718   if (Vars.empty())
2719     return nullptr;
2720   if (!Owner)
2721     Owner = getCurLexicalContext();
2722   auto *D = OMPAllocateDecl::Create(Context, Owner, Loc, Vars, Clauses);
2723   D->setAccess(AS_public);
2724   Owner->addDecl(D);
2725   return DeclGroupPtrTy::make(DeclGroupRef(D));
2726 }
2727 
2728 Sema::DeclGroupPtrTy
2729 Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc,
2730                                    ArrayRef<OMPClause *> ClauseList) {
2731   OMPRequiresDecl *D = nullptr;
2732   if (!CurContext->isFileContext()) {
2733     Diag(Loc, diag::err_omp_invalid_scope) << "requires";
2734   } else {
2735     D = CheckOMPRequiresDecl(Loc, ClauseList);
2736     if (D) {
2737       CurContext->addDecl(D);
2738       DSAStack->addRequiresDecl(D);
2739     }
2740   }
2741   return DeclGroupPtrTy::make(DeclGroupRef(D));
2742 }
2743 
2744 OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc,
2745                                             ArrayRef<OMPClause *> ClauseList) {
2746   /// For target specific clauses, the requires directive cannot be
2747   /// specified after the handling of any of the target regions in the
2748   /// current compilation unit.
2749   ArrayRef<SourceLocation> TargetLocations =
2750       DSAStack->getEncounteredTargetLocs();
2751   if (!TargetLocations.empty()) {
2752     for (const OMPClause *CNew : ClauseList) {
2753       // Check if any of the requires clauses affect target regions.
2754       if (isa<OMPUnifiedSharedMemoryClause>(CNew) ||
2755           isa<OMPUnifiedAddressClause>(CNew) ||
2756           isa<OMPReverseOffloadClause>(CNew) ||
2757           isa<OMPDynamicAllocatorsClause>(CNew)) {
2758         Diag(Loc, diag::err_omp_target_before_requires)
2759             << getOpenMPClauseName(CNew->getClauseKind());
2760         for (SourceLocation TargetLoc : TargetLocations) {
2761           Diag(TargetLoc, diag::note_omp_requires_encountered_target);
2762         }
2763       }
2764     }
2765   }
2766 
2767   if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
2768     return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc,
2769                                    ClauseList);
2770   return nullptr;
2771 }
2772 
2773 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
2774                               const ValueDecl *D,
2775                               const DSAStackTy::DSAVarData &DVar,
2776                               bool IsLoopIterVar = false) {
2777   if (DVar.RefExpr) {
2778     SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
2779         << getOpenMPClauseName(DVar.CKind);
2780     return;
2781   }
2782   enum {
2783     PDSA_StaticMemberShared,
2784     PDSA_StaticLocalVarShared,
2785     PDSA_LoopIterVarPrivate,
2786     PDSA_LoopIterVarLinear,
2787     PDSA_LoopIterVarLastprivate,
2788     PDSA_ConstVarShared,
2789     PDSA_GlobalVarShared,
2790     PDSA_TaskVarFirstprivate,
2791     PDSA_LocalVarPrivate,
2792     PDSA_Implicit
2793   } Reason = PDSA_Implicit;
2794   bool ReportHint = false;
2795   auto ReportLoc = D->getLocation();
2796   auto *VD = dyn_cast<VarDecl>(D);
2797   if (IsLoopIterVar) {
2798     if (DVar.CKind == OMPC_private)
2799       Reason = PDSA_LoopIterVarPrivate;
2800     else if (DVar.CKind == OMPC_lastprivate)
2801       Reason = PDSA_LoopIterVarLastprivate;
2802     else
2803       Reason = PDSA_LoopIterVarLinear;
2804   } else if (isOpenMPTaskingDirective(DVar.DKind) &&
2805              DVar.CKind == OMPC_firstprivate) {
2806     Reason = PDSA_TaskVarFirstprivate;
2807     ReportLoc = DVar.ImplicitDSALoc;
2808   } else if (VD && VD->isStaticLocal())
2809     Reason = PDSA_StaticLocalVarShared;
2810   else if (VD && VD->isStaticDataMember())
2811     Reason = PDSA_StaticMemberShared;
2812   else if (VD && VD->isFileVarDecl())
2813     Reason = PDSA_GlobalVarShared;
2814   else if (D->getType().isConstant(SemaRef.getASTContext()))
2815     Reason = PDSA_ConstVarShared;
2816   else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
2817     ReportHint = true;
2818     Reason = PDSA_LocalVarPrivate;
2819   }
2820   if (Reason != PDSA_Implicit) {
2821     SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
2822         << Reason << ReportHint
2823         << getOpenMPDirectiveName(Stack->getCurrentDirective());
2824   } else if (DVar.ImplicitDSALoc.isValid()) {
2825     SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
2826         << getOpenMPClauseName(DVar.CKind);
2827   }
2828 }
2829 
2830 static OpenMPMapClauseKind
2831 getMapClauseKindFromModifier(OpenMPDefaultmapClauseModifier M,
2832                              bool IsAggregateOrDeclareTarget) {
2833   OpenMPMapClauseKind Kind = OMPC_MAP_unknown;
2834   switch (M) {
2835   case OMPC_DEFAULTMAP_MODIFIER_alloc:
2836     Kind = OMPC_MAP_alloc;
2837     break;
2838   case OMPC_DEFAULTMAP_MODIFIER_to:
2839     Kind = OMPC_MAP_to;
2840     break;
2841   case OMPC_DEFAULTMAP_MODIFIER_from:
2842     Kind = OMPC_MAP_from;
2843     break;
2844   case OMPC_DEFAULTMAP_MODIFIER_tofrom:
2845     Kind = OMPC_MAP_tofrom;
2846     break;
2847   case OMPC_DEFAULTMAP_MODIFIER_firstprivate:
2848   case OMPC_DEFAULTMAP_MODIFIER_last:
2849     llvm_unreachable("Unexpected defaultmap implicit behavior");
2850   case OMPC_DEFAULTMAP_MODIFIER_none:
2851   case OMPC_DEFAULTMAP_MODIFIER_default:
2852   case OMPC_DEFAULTMAP_MODIFIER_unknown:
2853     // IsAggregateOrDeclareTarget could be true if:
2854     // 1. the implicit behavior for aggregate is tofrom
2855     // 2. it's a declare target link
2856     if (IsAggregateOrDeclareTarget) {
2857       Kind = OMPC_MAP_tofrom;
2858       break;
2859     }
2860     llvm_unreachable("Unexpected defaultmap implicit behavior");
2861   }
2862   assert(Kind != OMPC_MAP_unknown && "Expect map kind to be known");
2863   return Kind;
2864 }
2865 
2866 namespace {
2867 class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
2868   DSAStackTy *Stack;
2869   Sema &SemaRef;
2870   bool ErrorFound = false;
2871   bool TryCaptureCXXThisMembers = false;
2872   CapturedStmt *CS = nullptr;
2873   llvm::SmallVector<Expr *, 4> ImplicitFirstprivate;
2874   llvm::SmallVector<Expr *, 4> ImplicitMap[OMPC_MAP_delete];
2875   Sema::VarsWithInheritedDSAType VarsWithInheritedDSA;
2876   llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
2877 
2878   void VisitSubCaptures(OMPExecutableDirective *S) {
2879     // Check implicitly captured variables.
2880     if (!S->hasAssociatedStmt() || !S->getAssociatedStmt())
2881       return;
2882     visitSubCaptures(S->getInnermostCapturedStmt());
2883     // Try to capture inner this->member references to generate correct mappings
2884     // and diagnostics.
2885     if (TryCaptureCXXThisMembers ||
2886         (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
2887          llvm::any_of(S->getInnermostCapturedStmt()->captures(),
2888                       [](const CapturedStmt::Capture &C) {
2889                         return C.capturesThis();
2890                       }))) {
2891       bool SavedTryCaptureCXXThisMembers = TryCaptureCXXThisMembers;
2892       TryCaptureCXXThisMembers = true;
2893       Visit(S->getInnermostCapturedStmt()->getCapturedStmt());
2894       TryCaptureCXXThisMembers = SavedTryCaptureCXXThisMembers;
2895     }
2896   }
2897 
2898 public:
2899   void VisitDeclRefExpr(DeclRefExpr *E) {
2900     if (TryCaptureCXXThisMembers || E->isTypeDependent() ||
2901         E->isValueDependent() || E->containsUnexpandedParameterPack() ||
2902         E->isInstantiationDependent())
2903       return;
2904     if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
2905       // Check the datasharing rules for the expressions in the clauses.
2906       if (!CS) {
2907         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
2908           if (!CED->hasAttr<OMPCaptureNoInitAttr>()) {
2909             Visit(CED->getInit());
2910             return;
2911           }
2912       } else if (VD->isImplicit() || isa<OMPCapturedExprDecl>(VD))
2913         // Do not analyze internal variables and do not enclose them into
2914         // implicit clauses.
2915         return;
2916       VD = VD->getCanonicalDecl();
2917       // Skip internally declared variables.
2918       if (VD->hasLocalStorage() && CS && !CS->capturesVariable(VD))
2919         return;
2920 
2921       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
2922       // Check if the variable has explicit DSA set and stop analysis if it so.
2923       if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second)
2924         return;
2925 
2926       // Skip internally declared static variables.
2927       llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
2928           OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
2929       if (VD->hasGlobalStorage() && CS && !CS->capturesVariable(VD) &&
2930           (Stack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() ||
2931            !Res || *Res != OMPDeclareTargetDeclAttr::MT_Link))
2932         return;
2933 
2934       SourceLocation ELoc = E->getExprLoc();
2935       OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
2936       // The default(none) clause requires that each variable that is referenced
2937       // in the construct, and does not have a predetermined data-sharing
2938       // attribute, must have its data-sharing attribute explicitly determined
2939       // by being listed in a data-sharing attribute clause.
2940       if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
2941           isImplicitOrExplicitTaskingRegion(DKind) &&
2942           VarsWithInheritedDSA.count(VD) == 0) {
2943         VarsWithInheritedDSA[VD] = E;
2944         return;
2945       }
2946 
2947       // OpenMP 5.0 [2.19.7.2, defaultmap clause, Description]
2948       // If implicit-behavior is none, each variable referenced in the
2949       // construct that does not have a predetermined data-sharing attribute
2950       // and does not appear in a to or link clause on a declare target
2951       // directive must be listed in a data-mapping attribute clause, a
2952       // data-haring attribute clause (including a data-sharing attribute
2953       // clause on a combined construct where target. is one of the
2954       // constituent constructs), or an is_device_ptr clause.
2955       OpenMPDefaultmapClauseKind ClauseKind =
2956           getVariableCategoryFromDecl(SemaRef.getLangOpts(), VD);
2957       if (SemaRef.getLangOpts().OpenMP >= 50) {
2958         bool IsModifierNone = Stack->getDefaultmapModifier(ClauseKind) ==
2959                               OMPC_DEFAULTMAP_MODIFIER_none;
2960         if (DVar.CKind == OMPC_unknown && IsModifierNone &&
2961             VarsWithInheritedDSA.count(VD) == 0 && !Res) {
2962           // Only check for data-mapping attribute and is_device_ptr here
2963           // since we have already make sure that the declaration does not
2964           // have a data-sharing attribute above
2965           if (!Stack->checkMappableExprComponentListsForDecl(
2966                   VD, /*CurrentRegionOnly=*/true,
2967                   [VD](OMPClauseMappableExprCommon::MappableExprComponentListRef
2968                            MapExprComponents,
2969                        OpenMPClauseKind) {
2970                     auto MI = MapExprComponents.rbegin();
2971                     auto ME = MapExprComponents.rend();
2972                     return MI != ME && MI->getAssociatedDeclaration() == VD;
2973                   })) {
2974             VarsWithInheritedDSA[VD] = E;
2975             return;
2976           }
2977         }
2978       }
2979 
2980       if (isOpenMPTargetExecutionDirective(DKind) &&
2981           !Stack->isLoopControlVariable(VD).first) {
2982         if (!Stack->checkMappableExprComponentListsForDecl(
2983                 VD, /*CurrentRegionOnly=*/true,
2984                 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2985                        StackComponents,
2986                    OpenMPClauseKind) {
2987                   // Variable is used if it has been marked as an array, array
2988                   // section or the variable iself.
2989                   return StackComponents.size() == 1 ||
2990                          std::all_of(
2991                              std::next(StackComponents.rbegin()),
2992                              StackComponents.rend(),
2993                              [](const OMPClauseMappableExprCommon::
2994                                     MappableComponent &MC) {
2995                                return MC.getAssociatedDeclaration() ==
2996                                           nullptr &&
2997                                       (isa<OMPArraySectionExpr>(
2998                                            MC.getAssociatedExpression()) ||
2999                                        isa<ArraySubscriptExpr>(
3000                                            MC.getAssociatedExpression()));
3001                              });
3002                 })) {
3003           bool IsFirstprivate = false;
3004           // By default lambdas are captured as firstprivates.
3005           if (const auto *RD =
3006                   VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
3007             IsFirstprivate = RD->isLambda();
3008           IsFirstprivate =
3009               IsFirstprivate || (Stack->mustBeFirstprivate(ClauseKind) && !Res);
3010           if (IsFirstprivate) {
3011             ImplicitFirstprivate.emplace_back(E);
3012           } else {
3013             OpenMPDefaultmapClauseModifier M =
3014                 Stack->getDefaultmapModifier(ClauseKind);
3015             OpenMPMapClauseKind Kind = getMapClauseKindFromModifier(
3016                 M, ClauseKind == OMPC_DEFAULTMAP_aggregate || Res);
3017             ImplicitMap[Kind].emplace_back(E);
3018           }
3019           return;
3020         }
3021       }
3022 
3023       // OpenMP [2.9.3.6, Restrictions, p.2]
3024       //  A list item that appears in a reduction clause of the innermost
3025       //  enclosing worksharing or parallel construct may not be accessed in an
3026       //  explicit task.
3027       DVar = Stack->hasInnermostDSA(
3028           VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
3029           [](OpenMPDirectiveKind K) {
3030             return isOpenMPParallelDirective(K) ||
3031                    isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
3032           },
3033           /*FromParent=*/true);
3034       if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
3035         ErrorFound = true;
3036         SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
3037         reportOriginalDsa(SemaRef, Stack, VD, DVar);
3038         return;
3039       }
3040 
3041       // Define implicit data-sharing attributes for task.
3042       DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false);
3043       if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
3044           !Stack->isLoopControlVariable(VD).first) {
3045         ImplicitFirstprivate.push_back(E);
3046         return;
3047       }
3048 
3049       // Store implicitly used globals with declare target link for parent
3050       // target.
3051       if (!isOpenMPTargetExecutionDirective(DKind) && Res &&
3052           *Res == OMPDeclareTargetDeclAttr::MT_Link) {
3053         Stack->addToParentTargetRegionLinkGlobals(E);
3054         return;
3055       }
3056     }
3057   }
3058   void VisitMemberExpr(MemberExpr *E) {
3059     if (E->isTypeDependent() || E->isValueDependent() ||
3060         E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
3061       return;
3062     auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
3063     OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
3064     if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParens())) {
3065       if (!FD)
3066         return;
3067       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false);
3068       // Check if the variable has explicit DSA set and stop analysis if it
3069       // so.
3070       if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second)
3071         return;
3072 
3073       if (isOpenMPTargetExecutionDirective(DKind) &&
3074           !Stack->isLoopControlVariable(FD).first &&
3075           !Stack->checkMappableExprComponentListsForDecl(
3076               FD, /*CurrentRegionOnly=*/true,
3077               [](OMPClauseMappableExprCommon::MappableExprComponentListRef
3078                      StackComponents,
3079                  OpenMPClauseKind) {
3080                 return isa<CXXThisExpr>(
3081                     cast<MemberExpr>(
3082                         StackComponents.back().getAssociatedExpression())
3083                         ->getBase()
3084                         ->IgnoreParens());
3085               })) {
3086         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
3087         //  A bit-field cannot appear in a map clause.
3088         //
3089         if (FD->isBitField())
3090           return;
3091 
3092         // Check to see if the member expression is referencing a class that
3093         // has already been explicitly mapped
3094         if (Stack->isClassPreviouslyMapped(TE->getType()))
3095           return;
3096 
3097         OpenMPDefaultmapClauseModifier Modifier =
3098             Stack->getDefaultmapModifier(OMPC_DEFAULTMAP_aggregate);
3099         OpenMPMapClauseKind Kind = getMapClauseKindFromModifier(
3100             Modifier, /*IsAggregateOrDeclareTarget*/ true);
3101         ImplicitMap[Kind].emplace_back(E);
3102         return;
3103       }
3104 
3105       SourceLocation ELoc = E->getExprLoc();
3106       // OpenMP [2.9.3.6, Restrictions, p.2]
3107       //  A list item that appears in a reduction clause of the innermost
3108       //  enclosing worksharing or parallel construct may not be accessed in
3109       //  an  explicit task.
3110       DVar = Stack->hasInnermostDSA(
3111           FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
3112           [](OpenMPDirectiveKind K) {
3113             return isOpenMPParallelDirective(K) ||
3114                    isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
3115           },
3116           /*FromParent=*/true);
3117       if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
3118         ErrorFound = true;
3119         SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
3120         reportOriginalDsa(SemaRef, Stack, FD, DVar);
3121         return;
3122       }
3123 
3124       // Define implicit data-sharing attributes for task.
3125       DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false);
3126       if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
3127           !Stack->isLoopControlVariable(FD).first) {
3128         // Check if there is a captured expression for the current field in the
3129         // region. Do not mark it as firstprivate unless there is no captured
3130         // expression.
3131         // TODO: try to make it firstprivate.
3132         if (DVar.CKind != OMPC_unknown)
3133           ImplicitFirstprivate.push_back(E);
3134       }
3135       return;
3136     }
3137     if (isOpenMPTargetExecutionDirective(DKind)) {
3138       OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
3139       if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
3140                                         /*NoDiagnose=*/true))
3141         return;
3142       const auto *VD = cast<ValueDecl>(
3143           CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
3144       if (!Stack->checkMappableExprComponentListsForDecl(
3145               VD, /*CurrentRegionOnly=*/true,
3146               [&CurComponents](
3147                   OMPClauseMappableExprCommon::MappableExprComponentListRef
3148                       StackComponents,
3149                   OpenMPClauseKind) {
3150                 auto CCI = CurComponents.rbegin();
3151                 auto CCE = CurComponents.rend();
3152                 for (const auto &SC : llvm::reverse(StackComponents)) {
3153                   // Do both expressions have the same kind?
3154                   if (CCI->getAssociatedExpression()->getStmtClass() !=
3155                       SC.getAssociatedExpression()->getStmtClass())
3156                     if (!(isa<OMPArraySectionExpr>(
3157                               SC.getAssociatedExpression()) &&
3158                           isa<ArraySubscriptExpr>(
3159                               CCI->getAssociatedExpression())))
3160                       return false;
3161 
3162                   const Decl *CCD = CCI->getAssociatedDeclaration();
3163                   const Decl *SCD = SC.getAssociatedDeclaration();
3164                   CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
3165                   SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
3166                   if (SCD != CCD)
3167                     return false;
3168                   std::advance(CCI, 1);
3169                   if (CCI == CCE)
3170                     break;
3171                 }
3172                 return true;
3173               })) {
3174         Visit(E->getBase());
3175       }
3176     } else if (!TryCaptureCXXThisMembers) {
3177       Visit(E->getBase());
3178     }
3179   }
3180   void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
3181     for (OMPClause *C : S->clauses()) {
3182       // Skip analysis of arguments of implicitly defined firstprivate clause
3183       // for task|target directives.
3184       // Skip analysis of arguments of implicitly defined map clause for target
3185       // directives.
3186       if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
3187                  C->isImplicit())) {
3188         for (Stmt *CC : C->children()) {
3189           if (CC)
3190             Visit(CC);
3191         }
3192       }
3193     }
3194     // Check implicitly captured variables.
3195     VisitSubCaptures(S);
3196   }
3197   void VisitStmt(Stmt *S) {
3198     for (Stmt *C : S->children()) {
3199       if (C) {
3200         // Check implicitly captured variables in the task-based directives to
3201         // check if they must be firstprivatized.
3202         Visit(C);
3203       }
3204     }
3205   }
3206 
3207   void visitSubCaptures(CapturedStmt *S) {
3208     for (const CapturedStmt::Capture &Cap : S->captures()) {
3209       if (!Cap.capturesVariable() && !Cap.capturesVariableByCopy())
3210         continue;
3211       VarDecl *VD = Cap.getCapturedVar();
3212       // Do not try to map the variable if it or its sub-component was mapped
3213       // already.
3214       if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
3215           Stack->checkMappableExprComponentListsForDecl(
3216               VD, /*CurrentRegionOnly=*/true,
3217               [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
3218                  OpenMPClauseKind) { return true; }))
3219         continue;
3220       DeclRefExpr *DRE = buildDeclRefExpr(
3221           SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context),
3222           Cap.getLocation(), /*RefersToCapture=*/true);
3223       Visit(DRE);
3224     }
3225   }
3226   bool isErrorFound() const { return ErrorFound; }
3227   ArrayRef<Expr *> getImplicitFirstprivate() const {
3228     return ImplicitFirstprivate;
3229   }
3230   ArrayRef<Expr *> getImplicitMap(OpenMPDefaultmapClauseKind Kind) const {
3231     return ImplicitMap[Kind];
3232   }
3233   const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
3234     return VarsWithInheritedDSA;
3235   }
3236 
3237   DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
3238       : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {
3239     // Process declare target link variables for the target directives.
3240     if (isOpenMPTargetExecutionDirective(S->getCurrentDirective())) {
3241       for (DeclRefExpr *E : Stack->getLinkGlobals())
3242         Visit(E);
3243     }
3244   }
3245 };
3246 } // namespace
3247 
3248 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
3249   switch (DKind) {
3250   case OMPD_parallel:
3251   case OMPD_parallel_for:
3252   case OMPD_parallel_for_simd:
3253   case OMPD_parallel_sections:
3254   case OMPD_parallel_master:
3255   case OMPD_teams:
3256   case OMPD_teams_distribute:
3257   case OMPD_teams_distribute_simd: {
3258     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3259     QualType KmpInt32PtrTy =
3260         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3261     Sema::CapturedParamNameType Params[] = {
3262         std::make_pair(".global_tid.", KmpInt32PtrTy),
3263         std::make_pair(".bound_tid.", KmpInt32PtrTy),
3264         std::make_pair(StringRef(), QualType()) // __context with shared vars
3265     };
3266     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3267                              Params);
3268     break;
3269   }
3270   case OMPD_target_teams:
3271   case OMPD_target_parallel:
3272   case OMPD_target_parallel_for:
3273   case OMPD_target_parallel_for_simd:
3274   case OMPD_target_teams_distribute:
3275   case OMPD_target_teams_distribute_simd: {
3276     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3277     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3278     QualType KmpInt32PtrTy =
3279         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3280     QualType Args[] = {VoidPtrTy};
3281     FunctionProtoType::ExtProtoInfo EPI;
3282     EPI.Variadic = true;
3283     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3284     Sema::CapturedParamNameType Params[] = {
3285         std::make_pair(".global_tid.", KmpInt32Ty),
3286         std::make_pair(".part_id.", KmpInt32PtrTy),
3287         std::make_pair(".privates.", VoidPtrTy),
3288         std::make_pair(
3289             ".copy_fn.",
3290             Context.getPointerType(CopyFnType).withConst().withRestrict()),
3291         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3292         std::make_pair(StringRef(), QualType()) // __context with shared vars
3293     };
3294     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3295                              Params, /*OpenMPCaptureLevel=*/0);
3296     // Mark this captured region as inlined, because we don't use outlined
3297     // function directly.
3298     getCurCapturedRegion()->TheCapturedDecl->addAttr(
3299         AlwaysInlineAttr::CreateImplicit(
3300             Context, {}, AttributeCommonInfo::AS_Keyword,
3301             AlwaysInlineAttr::Keyword_forceinline));
3302     Sema::CapturedParamNameType ParamsTarget[] = {
3303         std::make_pair(StringRef(), QualType()) // __context with shared vars
3304     };
3305     // Start a captured region for 'target' with no implicit parameters.
3306     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3307                              ParamsTarget, /*OpenMPCaptureLevel=*/1);
3308     Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
3309         std::make_pair(".global_tid.", KmpInt32PtrTy),
3310         std::make_pair(".bound_tid.", KmpInt32PtrTy),
3311         std::make_pair(StringRef(), QualType()) // __context with shared vars
3312     };
3313     // Start a captured region for 'teams' or 'parallel'.  Both regions have
3314     // the same implicit parameters.
3315     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3316                              ParamsTeamsOrParallel, /*OpenMPCaptureLevel=*/2);
3317     break;
3318   }
3319   case OMPD_target:
3320   case OMPD_target_simd: {
3321     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3322     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3323     QualType KmpInt32PtrTy =
3324         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3325     QualType Args[] = {VoidPtrTy};
3326     FunctionProtoType::ExtProtoInfo EPI;
3327     EPI.Variadic = true;
3328     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3329     Sema::CapturedParamNameType Params[] = {
3330         std::make_pair(".global_tid.", KmpInt32Ty),
3331         std::make_pair(".part_id.", KmpInt32PtrTy),
3332         std::make_pair(".privates.", VoidPtrTy),
3333         std::make_pair(
3334             ".copy_fn.",
3335             Context.getPointerType(CopyFnType).withConst().withRestrict()),
3336         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3337         std::make_pair(StringRef(), QualType()) // __context with shared vars
3338     };
3339     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3340                              Params, /*OpenMPCaptureLevel=*/0);
3341     // Mark this captured region as inlined, because we don't use outlined
3342     // function directly.
3343     getCurCapturedRegion()->TheCapturedDecl->addAttr(
3344         AlwaysInlineAttr::CreateImplicit(
3345             Context, {}, AttributeCommonInfo::AS_Keyword,
3346             AlwaysInlineAttr::Keyword_forceinline));
3347     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3348                              std::make_pair(StringRef(), QualType()),
3349                              /*OpenMPCaptureLevel=*/1);
3350     break;
3351   }
3352   case OMPD_simd:
3353   case OMPD_for:
3354   case OMPD_for_simd:
3355   case OMPD_sections:
3356   case OMPD_section:
3357   case OMPD_single:
3358   case OMPD_master:
3359   case OMPD_critical:
3360   case OMPD_taskgroup:
3361   case OMPD_distribute:
3362   case OMPD_distribute_simd:
3363   case OMPD_ordered:
3364   case OMPD_atomic:
3365   case OMPD_target_data: {
3366     Sema::CapturedParamNameType Params[] = {
3367         std::make_pair(StringRef(), QualType()) // __context with shared vars
3368     };
3369     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3370                              Params);
3371     break;
3372   }
3373   case OMPD_task: {
3374     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3375     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3376     QualType KmpInt32PtrTy =
3377         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3378     QualType Args[] = {VoidPtrTy};
3379     FunctionProtoType::ExtProtoInfo EPI;
3380     EPI.Variadic = true;
3381     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3382     Sema::CapturedParamNameType Params[] = {
3383         std::make_pair(".global_tid.", KmpInt32Ty),
3384         std::make_pair(".part_id.", KmpInt32PtrTy),
3385         std::make_pair(".privates.", VoidPtrTy),
3386         std::make_pair(
3387             ".copy_fn.",
3388             Context.getPointerType(CopyFnType).withConst().withRestrict()),
3389         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3390         std::make_pair(StringRef(), QualType()) // __context with shared vars
3391     };
3392     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3393                              Params);
3394     // Mark this captured region as inlined, because we don't use outlined
3395     // function directly.
3396     getCurCapturedRegion()->TheCapturedDecl->addAttr(
3397         AlwaysInlineAttr::CreateImplicit(
3398             Context, {}, AttributeCommonInfo::AS_Keyword,
3399             AlwaysInlineAttr::Keyword_forceinline));
3400     break;
3401   }
3402   case OMPD_taskloop:
3403   case OMPD_taskloop_simd:
3404   case OMPD_master_taskloop:
3405   case OMPD_master_taskloop_simd: {
3406     QualType KmpInt32Ty =
3407         Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
3408             .withConst();
3409     QualType KmpUInt64Ty =
3410         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
3411             .withConst();
3412     QualType KmpInt64Ty =
3413         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
3414             .withConst();
3415     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3416     QualType KmpInt32PtrTy =
3417         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3418     QualType Args[] = {VoidPtrTy};
3419     FunctionProtoType::ExtProtoInfo EPI;
3420     EPI.Variadic = true;
3421     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3422     Sema::CapturedParamNameType Params[] = {
3423         std::make_pair(".global_tid.", KmpInt32Ty),
3424         std::make_pair(".part_id.", KmpInt32PtrTy),
3425         std::make_pair(".privates.", VoidPtrTy),
3426         std::make_pair(
3427             ".copy_fn.",
3428             Context.getPointerType(CopyFnType).withConst().withRestrict()),
3429         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3430         std::make_pair(".lb.", KmpUInt64Ty),
3431         std::make_pair(".ub.", KmpUInt64Ty),
3432         std::make_pair(".st.", KmpInt64Ty),
3433         std::make_pair(".liter.", KmpInt32Ty),
3434         std::make_pair(".reductions.", VoidPtrTy),
3435         std::make_pair(StringRef(), QualType()) // __context with shared vars
3436     };
3437     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3438                              Params);
3439     // Mark this captured region as inlined, because we don't use outlined
3440     // function directly.
3441     getCurCapturedRegion()->TheCapturedDecl->addAttr(
3442         AlwaysInlineAttr::CreateImplicit(
3443             Context, {}, AttributeCommonInfo::AS_Keyword,
3444             AlwaysInlineAttr::Keyword_forceinline));
3445     break;
3446   }
3447   case OMPD_parallel_master_taskloop:
3448   case OMPD_parallel_master_taskloop_simd: {
3449     QualType KmpInt32Ty =
3450         Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
3451             .withConst();
3452     QualType KmpUInt64Ty =
3453         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
3454             .withConst();
3455     QualType KmpInt64Ty =
3456         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
3457             .withConst();
3458     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3459     QualType KmpInt32PtrTy =
3460         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3461     Sema::CapturedParamNameType ParamsParallel[] = {
3462         std::make_pair(".global_tid.", KmpInt32PtrTy),
3463         std::make_pair(".bound_tid.", KmpInt32PtrTy),
3464         std::make_pair(StringRef(), QualType()) // __context with shared vars
3465     };
3466     // Start a captured region for 'parallel'.
3467     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3468                              ParamsParallel, /*OpenMPCaptureLevel=*/1);
3469     QualType Args[] = {VoidPtrTy};
3470     FunctionProtoType::ExtProtoInfo EPI;
3471     EPI.Variadic = true;
3472     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3473     Sema::CapturedParamNameType Params[] = {
3474         std::make_pair(".global_tid.", KmpInt32Ty),
3475         std::make_pair(".part_id.", KmpInt32PtrTy),
3476         std::make_pair(".privates.", VoidPtrTy),
3477         std::make_pair(
3478             ".copy_fn.",
3479             Context.getPointerType(CopyFnType).withConst().withRestrict()),
3480         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3481         std::make_pair(".lb.", KmpUInt64Ty),
3482         std::make_pair(".ub.", KmpUInt64Ty),
3483         std::make_pair(".st.", KmpInt64Ty),
3484         std::make_pair(".liter.", KmpInt32Ty),
3485         std::make_pair(".reductions.", VoidPtrTy),
3486         std::make_pair(StringRef(), QualType()) // __context with shared vars
3487     };
3488     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3489                              Params, /*OpenMPCaptureLevel=*/2);
3490     // Mark this captured region as inlined, because we don't use outlined
3491     // function directly.
3492     getCurCapturedRegion()->TheCapturedDecl->addAttr(
3493         AlwaysInlineAttr::CreateImplicit(
3494             Context, {}, AttributeCommonInfo::AS_Keyword,
3495             AlwaysInlineAttr::Keyword_forceinline));
3496     break;
3497   }
3498   case OMPD_distribute_parallel_for_simd:
3499   case OMPD_distribute_parallel_for: {
3500     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3501     QualType KmpInt32PtrTy =
3502         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3503     Sema::CapturedParamNameType Params[] = {
3504         std::make_pair(".global_tid.", KmpInt32PtrTy),
3505         std::make_pair(".bound_tid.", KmpInt32PtrTy),
3506         std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
3507         std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
3508         std::make_pair(StringRef(), QualType()) // __context with shared vars
3509     };
3510     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3511                              Params);
3512     break;
3513   }
3514   case OMPD_target_teams_distribute_parallel_for:
3515   case OMPD_target_teams_distribute_parallel_for_simd: {
3516     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3517     QualType KmpInt32PtrTy =
3518         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3519     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3520 
3521     QualType Args[] = {VoidPtrTy};
3522     FunctionProtoType::ExtProtoInfo EPI;
3523     EPI.Variadic = true;
3524     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3525     Sema::CapturedParamNameType Params[] = {
3526         std::make_pair(".global_tid.", KmpInt32Ty),
3527         std::make_pair(".part_id.", KmpInt32PtrTy),
3528         std::make_pair(".privates.", VoidPtrTy),
3529         std::make_pair(
3530             ".copy_fn.",
3531             Context.getPointerType(CopyFnType).withConst().withRestrict()),
3532         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3533         std::make_pair(StringRef(), QualType()) // __context with shared vars
3534     };
3535     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3536                              Params, /*OpenMPCaptureLevel=*/0);
3537     // Mark this captured region as inlined, because we don't use outlined
3538     // function directly.
3539     getCurCapturedRegion()->TheCapturedDecl->addAttr(
3540         AlwaysInlineAttr::CreateImplicit(
3541             Context, {}, AttributeCommonInfo::AS_Keyword,
3542             AlwaysInlineAttr::Keyword_forceinline));
3543     Sema::CapturedParamNameType ParamsTarget[] = {
3544         std::make_pair(StringRef(), QualType()) // __context with shared vars
3545     };
3546     // Start a captured region for 'target' with no implicit parameters.
3547     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3548                              ParamsTarget, /*OpenMPCaptureLevel=*/1);
3549 
3550     Sema::CapturedParamNameType ParamsTeams[] = {
3551         std::make_pair(".global_tid.", KmpInt32PtrTy),
3552         std::make_pair(".bound_tid.", KmpInt32PtrTy),
3553         std::make_pair(StringRef(), QualType()) // __context with shared vars
3554     };
3555     // Start a captured region for 'target' with no implicit parameters.
3556     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3557                              ParamsTeams, /*OpenMPCaptureLevel=*/2);
3558 
3559     Sema::CapturedParamNameType ParamsParallel[] = {
3560         std::make_pair(".global_tid.", KmpInt32PtrTy),
3561         std::make_pair(".bound_tid.", KmpInt32PtrTy),
3562         std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
3563         std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
3564         std::make_pair(StringRef(), QualType()) // __context with shared vars
3565     };
3566     // Start a captured region for 'teams' or 'parallel'.  Both regions have
3567     // the same implicit parameters.
3568     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3569                              ParamsParallel, /*OpenMPCaptureLevel=*/3);
3570     break;
3571   }
3572 
3573   case OMPD_teams_distribute_parallel_for:
3574   case OMPD_teams_distribute_parallel_for_simd: {
3575     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3576     QualType KmpInt32PtrTy =
3577         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3578 
3579     Sema::CapturedParamNameType ParamsTeams[] = {
3580         std::make_pair(".global_tid.", KmpInt32PtrTy),
3581         std::make_pair(".bound_tid.", KmpInt32PtrTy),
3582         std::make_pair(StringRef(), QualType()) // __context with shared vars
3583     };
3584     // Start a captured region for 'target' with no implicit parameters.
3585     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3586                              ParamsTeams, /*OpenMPCaptureLevel=*/0);
3587 
3588     Sema::CapturedParamNameType ParamsParallel[] = {
3589         std::make_pair(".global_tid.", KmpInt32PtrTy),
3590         std::make_pair(".bound_tid.", KmpInt32PtrTy),
3591         std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
3592         std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
3593         std::make_pair(StringRef(), QualType()) // __context with shared vars
3594     };
3595     // Start a captured region for 'teams' or 'parallel'.  Both regions have
3596     // the same implicit parameters.
3597     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3598                              ParamsParallel, /*OpenMPCaptureLevel=*/1);
3599     break;
3600   }
3601   case OMPD_target_update:
3602   case OMPD_target_enter_data:
3603   case OMPD_target_exit_data: {
3604     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3605     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3606     QualType KmpInt32PtrTy =
3607         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3608     QualType Args[] = {VoidPtrTy};
3609     FunctionProtoType::ExtProtoInfo EPI;
3610     EPI.Variadic = true;
3611     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3612     Sema::CapturedParamNameType Params[] = {
3613         std::make_pair(".global_tid.", KmpInt32Ty),
3614         std::make_pair(".part_id.", KmpInt32PtrTy),
3615         std::make_pair(".privates.", VoidPtrTy),
3616         std::make_pair(
3617             ".copy_fn.",
3618             Context.getPointerType(CopyFnType).withConst().withRestrict()),
3619         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3620         std::make_pair(StringRef(), QualType()) // __context with shared vars
3621     };
3622     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3623                              Params);
3624     // Mark this captured region as inlined, because we don't use outlined
3625     // function directly.
3626     getCurCapturedRegion()->TheCapturedDecl->addAttr(
3627         AlwaysInlineAttr::CreateImplicit(
3628             Context, {}, AttributeCommonInfo::AS_Keyword,
3629             AlwaysInlineAttr::Keyword_forceinline));
3630     break;
3631   }
3632   case OMPD_threadprivate:
3633   case OMPD_allocate:
3634   case OMPD_taskyield:
3635   case OMPD_barrier:
3636   case OMPD_taskwait:
3637   case OMPD_cancellation_point:
3638   case OMPD_cancel:
3639   case OMPD_flush:
3640   case OMPD_declare_reduction:
3641   case OMPD_declare_mapper:
3642   case OMPD_declare_simd:
3643   case OMPD_declare_target:
3644   case OMPD_end_declare_target:
3645   case OMPD_requires:
3646   case OMPD_declare_variant:
3647     llvm_unreachable("OpenMP Directive is not allowed");
3648   case OMPD_unknown:
3649     llvm_unreachable("Unknown OpenMP directive");
3650   }
3651 }
3652 
3653 int Sema::getNumberOfConstructScopes(unsigned Level) const {
3654   return getOpenMPCaptureLevels(DSAStack->getDirective(Level));
3655 }
3656 
3657 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
3658   SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
3659   getOpenMPCaptureRegions(CaptureRegions, DKind);
3660   return CaptureRegions.size();
3661 }
3662 
3663 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
3664                                              Expr *CaptureExpr, bool WithInit,
3665                                              bool AsExpression) {
3666   assert(CaptureExpr);
3667   ASTContext &C = S.getASTContext();
3668   Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
3669   QualType Ty = Init->getType();
3670   if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
3671     if (S.getLangOpts().CPlusPlus) {
3672       Ty = C.getLValueReferenceType(Ty);
3673     } else {
3674       Ty = C.getPointerType(Ty);
3675       ExprResult Res =
3676           S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
3677       if (!Res.isUsable())
3678         return nullptr;
3679       Init = Res.get();
3680     }
3681     WithInit = true;
3682   }
3683   auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
3684                                           CaptureExpr->getBeginLoc());
3685   if (!WithInit)
3686     CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C));
3687   S.CurContext->addHiddenDecl(CED);
3688   S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
3689   return CED;
3690 }
3691 
3692 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
3693                                  bool WithInit) {
3694   OMPCapturedExprDecl *CD;
3695   if (VarDecl *VD = S.isOpenMPCapturedDecl(D))
3696     CD = cast<OMPCapturedExprDecl>(VD);
3697   else
3698     CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
3699                           /*AsExpression=*/false);
3700   return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
3701                           CaptureExpr->getExprLoc());
3702 }
3703 
3704 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
3705   CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
3706   if (!Ref) {
3707     OMPCapturedExprDecl *CD = buildCaptureDecl(
3708         S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
3709         /*WithInit=*/true, /*AsExpression=*/true);
3710     Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
3711                            CaptureExpr->getExprLoc());
3712   }
3713   ExprResult Res = Ref;
3714   if (!S.getLangOpts().CPlusPlus &&
3715       CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
3716       Ref->getType()->isPointerType()) {
3717     Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
3718     if (!Res.isUsable())
3719       return ExprError();
3720   }
3721   return S.DefaultLvalueConversion(Res.get());
3722 }
3723 
3724 namespace {
3725 // OpenMP directives parsed in this section are represented as a
3726 // CapturedStatement with an associated statement.  If a syntax error
3727 // is detected during the parsing of the associated statement, the
3728 // compiler must abort processing and close the CapturedStatement.
3729 //
3730 // Combined directives such as 'target parallel' have more than one
3731 // nested CapturedStatements.  This RAII ensures that we unwind out
3732 // of all the nested CapturedStatements when an error is found.
3733 class CaptureRegionUnwinderRAII {
3734 private:
3735   Sema &S;
3736   bool &ErrorFound;
3737   OpenMPDirectiveKind DKind = OMPD_unknown;
3738 
3739 public:
3740   CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
3741                             OpenMPDirectiveKind DKind)
3742       : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
3743   ~CaptureRegionUnwinderRAII() {
3744     if (ErrorFound) {
3745       int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
3746       while (--ThisCaptureLevel >= 0)
3747         S.ActOnCapturedRegionError();
3748     }
3749   }
3750 };
3751 } // namespace
3752 
3753 void Sema::tryCaptureOpenMPLambdas(ValueDecl *V) {
3754   // Capture variables captured by reference in lambdas for target-based
3755   // directives.
3756   if (!CurContext->isDependentContext() &&
3757       (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) ||
3758        isOpenMPTargetDataManagementDirective(
3759            DSAStack->getCurrentDirective()))) {
3760     QualType Type = V->getType();
3761     if (const auto *RD = Type.getCanonicalType()
3762                              .getNonReferenceType()
3763                              ->getAsCXXRecordDecl()) {
3764       bool SavedForceCaptureByReferenceInTargetExecutable =
3765           DSAStack->isForceCaptureByReferenceInTargetExecutable();
3766       DSAStack->setForceCaptureByReferenceInTargetExecutable(
3767           /*V=*/true);
3768       if (RD->isLambda()) {
3769         llvm::DenseMap<const VarDecl *, FieldDecl *> Captures;
3770         FieldDecl *ThisCapture;
3771         RD->getCaptureFields(Captures, ThisCapture);
3772         for (const LambdaCapture &LC : RD->captures()) {
3773           if (LC.getCaptureKind() == LCK_ByRef) {
3774             VarDecl *VD = LC.getCapturedVar();
3775             DeclContext *VDC = VD->getDeclContext();
3776             if (!VDC->Encloses(CurContext))
3777               continue;
3778             MarkVariableReferenced(LC.getLocation(), VD);
3779           } else if (LC.getCaptureKind() == LCK_This) {
3780             QualType ThisTy = getCurrentThisType();
3781             if (!ThisTy.isNull() &&
3782                 Context.typesAreCompatible(ThisTy, ThisCapture->getType()))
3783               CheckCXXThisCapture(LC.getLocation());
3784           }
3785         }
3786       }
3787       DSAStack->setForceCaptureByReferenceInTargetExecutable(
3788           SavedForceCaptureByReferenceInTargetExecutable);
3789     }
3790   }
3791 }
3792 
3793 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
3794                                       ArrayRef<OMPClause *> Clauses) {
3795   bool ErrorFound = false;
3796   CaptureRegionUnwinderRAII CaptureRegionUnwinder(
3797       *this, ErrorFound, DSAStack->getCurrentDirective());
3798   if (!S.isUsable()) {
3799     ErrorFound = true;
3800     return StmtError();
3801   }
3802 
3803   SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
3804   getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
3805   OMPOrderedClause *OC = nullptr;
3806   OMPScheduleClause *SC = nullptr;
3807   SmallVector<const OMPLinearClause *, 4> LCs;
3808   SmallVector<const OMPClauseWithPreInit *, 4> PICs;
3809   // This is required for proper codegen.
3810   for (OMPClause *Clause : Clauses) {
3811     if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
3812         Clause->getClauseKind() == OMPC_in_reduction) {
3813       // Capture taskgroup task_reduction descriptors inside the tasking regions
3814       // with the corresponding in_reduction items.
3815       auto *IRC = cast<OMPInReductionClause>(Clause);
3816       for (Expr *E : IRC->taskgroup_descriptors())
3817         if (E)
3818           MarkDeclarationsReferencedInExpr(E);
3819     }
3820     if (isOpenMPPrivate(Clause->getClauseKind()) ||
3821         Clause->getClauseKind() == OMPC_copyprivate ||
3822         (getLangOpts().OpenMPUseTLS &&
3823          getASTContext().getTargetInfo().isTLSSupported() &&
3824          Clause->getClauseKind() == OMPC_copyin)) {
3825       DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
3826       // Mark all variables in private list clauses as used in inner region.
3827       for (Stmt *VarRef : Clause->children()) {
3828         if (auto *E = cast_or_null<Expr>(VarRef)) {
3829           MarkDeclarationsReferencedInExpr(E);
3830         }
3831       }
3832       DSAStack->setForceVarCapturing(/*V=*/false);
3833     } else if (CaptureRegions.size() > 1 ||
3834                CaptureRegions.back() != OMPD_unknown) {
3835       if (auto *C = OMPClauseWithPreInit::get(Clause))
3836         PICs.push_back(C);
3837       if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
3838         if (Expr *E = C->getPostUpdateExpr())
3839           MarkDeclarationsReferencedInExpr(E);
3840       }
3841     }
3842     if (Clause->getClauseKind() == OMPC_schedule)
3843       SC = cast<OMPScheduleClause>(Clause);
3844     else if (Clause->getClauseKind() == OMPC_ordered)
3845       OC = cast<OMPOrderedClause>(Clause);
3846     else if (Clause->getClauseKind() == OMPC_linear)
3847       LCs.push_back(cast<OMPLinearClause>(Clause));
3848   }
3849   // OpenMP, 2.7.1 Loop Construct, Restrictions
3850   // The nonmonotonic modifier cannot be specified if an ordered clause is
3851   // specified.
3852   if (SC &&
3853       (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
3854        SC->getSecondScheduleModifier() ==
3855            OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
3856       OC) {
3857     Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
3858              ? SC->getFirstScheduleModifierLoc()
3859              : SC->getSecondScheduleModifierLoc(),
3860          diag::err_omp_schedule_nonmonotonic_ordered)
3861         << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
3862     ErrorFound = true;
3863   }
3864   if (!LCs.empty() && OC && OC->getNumForLoops()) {
3865     for (const OMPLinearClause *C : LCs) {
3866       Diag(C->getBeginLoc(), diag::err_omp_linear_ordered)
3867           << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
3868     }
3869     ErrorFound = true;
3870   }
3871   if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
3872       isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
3873       OC->getNumForLoops()) {
3874     Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd)
3875         << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
3876     ErrorFound = true;
3877   }
3878   if (ErrorFound) {
3879     return StmtError();
3880   }
3881   StmtResult SR = S;
3882   unsigned CompletedRegions = 0;
3883   for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
3884     // Mark all variables in private list clauses as used in inner region.
3885     // Required for proper codegen of combined directives.
3886     // TODO: add processing for other clauses.
3887     if (ThisCaptureRegion != OMPD_unknown) {
3888       for (const clang::OMPClauseWithPreInit *C : PICs) {
3889         OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
3890         // Find the particular capture region for the clause if the
3891         // directive is a combined one with multiple capture regions.
3892         // If the directive is not a combined one, the capture region
3893         // associated with the clause is OMPD_unknown and is generated
3894         // only once.
3895         if (CaptureRegion == ThisCaptureRegion ||
3896             CaptureRegion == OMPD_unknown) {
3897           if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
3898             for (Decl *D : DS->decls())
3899               MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
3900           }
3901         }
3902       }
3903     }
3904     if (++CompletedRegions == CaptureRegions.size())
3905       DSAStack->setBodyComplete();
3906     SR = ActOnCapturedRegionEnd(SR.get());
3907   }
3908   return SR;
3909 }
3910 
3911 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
3912                               OpenMPDirectiveKind CancelRegion,
3913                               SourceLocation StartLoc) {
3914   // CancelRegion is only needed for cancel and cancellation_point.
3915   if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
3916     return false;
3917 
3918   if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
3919       CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
3920     return false;
3921 
3922   SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
3923       << getOpenMPDirectiveName(CancelRegion);
3924   return true;
3925 }
3926 
3927 static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
3928                                   OpenMPDirectiveKind CurrentRegion,
3929                                   const DeclarationNameInfo &CurrentName,
3930                                   OpenMPDirectiveKind CancelRegion,
3931                                   SourceLocation StartLoc) {
3932   if (Stack->getCurScope()) {
3933     OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
3934     OpenMPDirectiveKind OffendingRegion = ParentRegion;
3935     bool NestingProhibited = false;
3936     bool CloseNesting = true;
3937     bool OrphanSeen = false;
3938     enum {
3939       NoRecommend,
3940       ShouldBeInParallelRegion,
3941       ShouldBeInOrderedRegion,
3942       ShouldBeInTargetRegion,
3943       ShouldBeInTeamsRegion
3944     } Recommend = NoRecommend;
3945     if (isOpenMPSimdDirective(ParentRegion) &&
3946         ((SemaRef.LangOpts.OpenMP <= 45 && CurrentRegion != OMPD_ordered) ||
3947          (SemaRef.LangOpts.OpenMP >= 50 && CurrentRegion != OMPD_ordered &&
3948           CurrentRegion != OMPD_simd && CurrentRegion != OMPD_atomic))) {
3949       // OpenMP [2.16, Nesting of Regions]
3950       // OpenMP constructs may not be nested inside a simd region.
3951       // OpenMP [2.8.1,simd Construct, Restrictions]
3952       // An ordered construct with the simd clause is the only OpenMP
3953       // construct that can appear in the simd region.
3954       // Allowing a SIMD construct nested in another SIMD construct is an
3955       // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
3956       // message.
3957       // OpenMP 5.0 [2.9.3.1, simd Construct, Restrictions]
3958       // The only OpenMP constructs that can be encountered during execution of
3959       // a simd region are the atomic construct, the loop construct, the simd
3960       // construct and the ordered construct with the simd clause.
3961       SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3962                                  ? diag::err_omp_prohibited_region_simd
3963                                  : diag::warn_omp_nesting_simd)
3964           << (SemaRef.LangOpts.OpenMP >= 50 ? 1 : 0);
3965       return CurrentRegion != OMPD_simd;
3966     }
3967     if (ParentRegion == OMPD_atomic) {
3968       // OpenMP [2.16, Nesting of Regions]
3969       // OpenMP constructs may not be nested inside an atomic region.
3970       SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3971       return true;
3972     }
3973     if (CurrentRegion == OMPD_section) {
3974       // OpenMP [2.7.2, sections Construct, Restrictions]
3975       // Orphaned section directives are prohibited. That is, the section
3976       // directives must appear within the sections construct and must not be
3977       // encountered elsewhere in the sections region.
3978       if (ParentRegion != OMPD_sections &&
3979           ParentRegion != OMPD_parallel_sections) {
3980         SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3981             << (ParentRegion != OMPD_unknown)
3982             << getOpenMPDirectiveName(ParentRegion);
3983         return true;
3984       }
3985       return false;
3986     }
3987     // Allow some constructs (except teams and cancellation constructs) to be
3988     // orphaned (they could be used in functions, called from OpenMP regions
3989     // with the required preconditions).
3990     if (ParentRegion == OMPD_unknown &&
3991         !isOpenMPNestingTeamsDirective(CurrentRegion) &&
3992         CurrentRegion != OMPD_cancellation_point &&
3993         CurrentRegion != OMPD_cancel)
3994       return false;
3995     if (CurrentRegion == OMPD_cancellation_point ||
3996         CurrentRegion == OMPD_cancel) {
3997       // OpenMP [2.16, Nesting of Regions]
3998       // A cancellation point construct for which construct-type-clause is
3999       // taskgroup must be nested inside a task construct. A cancellation
4000       // point construct for which construct-type-clause is not taskgroup must
4001       // be closely nested inside an OpenMP construct that matches the type
4002       // specified in construct-type-clause.
4003       // A cancel construct for which construct-type-clause is taskgroup must be
4004       // nested inside a task construct. A cancel construct for which
4005       // construct-type-clause is not taskgroup must be closely nested inside an
4006       // OpenMP construct that matches the type specified in
4007       // construct-type-clause.
4008       NestingProhibited =
4009           !((CancelRegion == OMPD_parallel &&
4010              (ParentRegion == OMPD_parallel ||
4011               ParentRegion == OMPD_target_parallel)) ||
4012             (CancelRegion == OMPD_for &&
4013              (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
4014               ParentRegion == OMPD_target_parallel_for ||
4015               ParentRegion == OMPD_distribute_parallel_for ||
4016               ParentRegion == OMPD_teams_distribute_parallel_for ||
4017               ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
4018             (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
4019             (CancelRegion == OMPD_sections &&
4020              (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
4021               ParentRegion == OMPD_parallel_sections)));
4022       OrphanSeen = ParentRegion == OMPD_unknown;
4023     } else if (CurrentRegion == OMPD_master) {
4024       // OpenMP [2.16, Nesting of Regions]
4025       // A master region may not be closely nested inside a worksharing,
4026       // atomic, or explicit task region.
4027       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
4028                           isOpenMPTaskingDirective(ParentRegion);
4029     } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
4030       // OpenMP [2.16, Nesting of Regions]
4031       // A critical region may not be nested (closely or otherwise) inside a
4032       // critical region with the same name. Note that this restriction is not
4033       // sufficient to prevent deadlock.
4034       SourceLocation PreviousCriticalLoc;
4035       bool DeadLock = Stack->hasDirective(
4036           [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
4037                                               const DeclarationNameInfo &DNI,
4038                                               SourceLocation Loc) {
4039             if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
4040               PreviousCriticalLoc = Loc;
4041               return true;
4042             }
4043             return false;
4044           },
4045           false /* skip top directive */);
4046       if (DeadLock) {
4047         SemaRef.Diag(StartLoc,
4048                      diag::err_omp_prohibited_region_critical_same_name)
4049             << CurrentName.getName();
4050         if (PreviousCriticalLoc.isValid())
4051           SemaRef.Diag(PreviousCriticalLoc,
4052                        diag::note_omp_previous_critical_region);
4053         return true;
4054       }
4055     } else if (CurrentRegion == OMPD_barrier) {
4056       // OpenMP [2.16, Nesting of Regions]
4057       // A barrier region may not be closely nested inside a worksharing,
4058       // explicit task, critical, ordered, atomic, or master region.
4059       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
4060                           isOpenMPTaskingDirective(ParentRegion) ||
4061                           ParentRegion == OMPD_master ||
4062                           ParentRegion == OMPD_parallel_master ||
4063                           ParentRegion == OMPD_critical ||
4064                           ParentRegion == OMPD_ordered;
4065     } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
4066                !isOpenMPParallelDirective(CurrentRegion) &&
4067                !isOpenMPTeamsDirective(CurrentRegion)) {
4068       // OpenMP [2.16, Nesting of Regions]
4069       // A worksharing region may not be closely nested inside a worksharing,
4070       // explicit task, critical, ordered, atomic, or master region.
4071       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
4072                           isOpenMPTaskingDirective(ParentRegion) ||
4073                           ParentRegion == OMPD_master ||
4074                           ParentRegion == OMPD_parallel_master ||
4075                           ParentRegion == OMPD_critical ||
4076                           ParentRegion == OMPD_ordered;
4077       Recommend = ShouldBeInParallelRegion;
4078     } else if (CurrentRegion == OMPD_ordered) {
4079       // OpenMP [2.16, Nesting of Regions]
4080       // An ordered region may not be closely nested inside a critical,
4081       // atomic, or explicit task region.
4082       // An ordered region must be closely nested inside a loop region (or
4083       // parallel loop region) with an ordered clause.
4084       // OpenMP [2.8.1,simd Construct, Restrictions]
4085       // An ordered construct with the simd clause is the only OpenMP construct
4086       // that can appear in the simd region.
4087       NestingProhibited = ParentRegion == OMPD_critical ||
4088                           isOpenMPTaskingDirective(ParentRegion) ||
4089                           !(isOpenMPSimdDirective(ParentRegion) ||
4090                             Stack->isParentOrderedRegion());
4091       Recommend = ShouldBeInOrderedRegion;
4092     } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
4093       // OpenMP [2.16, Nesting of Regions]
4094       // If specified, a teams construct must be contained within a target
4095       // construct.
4096       NestingProhibited =
4097           (SemaRef.LangOpts.OpenMP <= 45 && ParentRegion != OMPD_target) ||
4098           (SemaRef.LangOpts.OpenMP >= 50 && ParentRegion != OMPD_unknown &&
4099            ParentRegion != OMPD_target);
4100       OrphanSeen = ParentRegion == OMPD_unknown;
4101       Recommend = ShouldBeInTargetRegion;
4102     }
4103     if (!NestingProhibited &&
4104         !isOpenMPTargetExecutionDirective(CurrentRegion) &&
4105         !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
4106         (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
4107       // OpenMP [2.16, Nesting of Regions]
4108       // distribute, parallel, parallel sections, parallel workshare, and the
4109       // parallel loop and parallel loop SIMD constructs are the only OpenMP
4110       // constructs that can be closely nested in the teams region.
4111       NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
4112                           !isOpenMPDistributeDirective(CurrentRegion);
4113       Recommend = ShouldBeInParallelRegion;
4114     }
4115     if (!NestingProhibited &&
4116         isOpenMPNestingDistributeDirective(CurrentRegion)) {
4117       // OpenMP 4.5 [2.17 Nesting of Regions]
4118       // The region associated with the distribute construct must be strictly
4119       // nested inside a teams region
4120       NestingProhibited =
4121           (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
4122       Recommend = ShouldBeInTeamsRegion;
4123     }
4124     if (!NestingProhibited &&
4125         (isOpenMPTargetExecutionDirective(CurrentRegion) ||
4126          isOpenMPTargetDataManagementDirective(CurrentRegion))) {
4127       // OpenMP 4.5 [2.17 Nesting of Regions]
4128       // If a target, target update, target data, target enter data, or
4129       // target exit data construct is encountered during execution of a
4130       // target region, the behavior is unspecified.
4131       NestingProhibited = Stack->hasDirective(
4132           [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
4133                              SourceLocation) {
4134             if (isOpenMPTargetExecutionDirective(K)) {
4135               OffendingRegion = K;
4136               return true;
4137             }
4138             return false;
4139           },
4140           false /* don't skip top directive */);
4141       CloseNesting = false;
4142     }
4143     if (NestingProhibited) {
4144       if (OrphanSeen) {
4145         SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
4146             << getOpenMPDirectiveName(CurrentRegion) << Recommend;
4147       } else {
4148         SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
4149             << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
4150             << Recommend << getOpenMPDirectiveName(CurrentRegion);
4151       }
4152       return true;
4153     }
4154   }
4155   return false;
4156 }
4157 
4158 struct Kind2Unsigned {
4159   using argument_type = OpenMPDirectiveKind;
4160   unsigned operator()(argument_type DK) { return unsigned(DK); }
4161 };
4162 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
4163                            ArrayRef<OMPClause *> Clauses,
4164                            ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
4165   bool ErrorFound = false;
4166   unsigned NamedModifiersNumber = 0;
4167   llvm::IndexedMap<const OMPIfClause *, Kind2Unsigned> FoundNameModifiers;
4168   FoundNameModifiers.resize(unsigned(OMPD_unknown) + 1);
4169   SmallVector<SourceLocation, 4> NameModifierLoc;
4170   for (const OMPClause *C : Clauses) {
4171     if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
4172       // At most one if clause without a directive-name-modifier can appear on
4173       // the directive.
4174       OpenMPDirectiveKind CurNM = IC->getNameModifier();
4175       if (FoundNameModifiers[CurNM]) {
4176         S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
4177             << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
4178             << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
4179         ErrorFound = true;
4180       } else if (CurNM != OMPD_unknown) {
4181         NameModifierLoc.push_back(IC->getNameModifierLoc());
4182         ++NamedModifiersNumber;
4183       }
4184       FoundNameModifiers[CurNM] = IC;
4185       if (CurNM == OMPD_unknown)
4186         continue;
4187       // Check if the specified name modifier is allowed for the current
4188       // directive.
4189       // At most one if clause with the particular directive-name-modifier can
4190       // appear on the directive.
4191       bool MatchFound = false;
4192       for (auto NM : AllowedNameModifiers) {
4193         if (CurNM == NM) {
4194           MatchFound = true;
4195           break;
4196         }
4197       }
4198       if (!MatchFound) {
4199         S.Diag(IC->getNameModifierLoc(),
4200                diag::err_omp_wrong_if_directive_name_modifier)
4201             << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
4202         ErrorFound = true;
4203       }
4204     }
4205   }
4206   // If any if clause on the directive includes a directive-name-modifier then
4207   // all if clauses on the directive must include a directive-name-modifier.
4208   if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
4209     if (NamedModifiersNumber == AllowedNameModifiers.size()) {
4210       S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
4211              diag::err_omp_no_more_if_clause);
4212     } else {
4213       std::string Values;
4214       std::string Sep(", ");
4215       unsigned AllowedCnt = 0;
4216       unsigned TotalAllowedNum =
4217           AllowedNameModifiers.size() - NamedModifiersNumber;
4218       for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
4219            ++Cnt) {
4220         OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
4221         if (!FoundNameModifiers[NM]) {
4222           Values += "'";
4223           Values += getOpenMPDirectiveName(NM);
4224           Values += "'";
4225           if (AllowedCnt + 2 == TotalAllowedNum)
4226             Values += " or ";
4227           else if (AllowedCnt + 1 != TotalAllowedNum)
4228             Values += Sep;
4229           ++AllowedCnt;
4230         }
4231       }
4232       S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
4233              diag::err_omp_unnamed_if_clause)
4234           << (TotalAllowedNum > 1) << Values;
4235     }
4236     for (SourceLocation Loc : NameModifierLoc) {
4237       S.Diag(Loc, diag::note_omp_previous_named_if_clause);
4238     }
4239     ErrorFound = true;
4240   }
4241   return ErrorFound;
4242 }
4243 
4244 static std::pair<ValueDecl *, bool>
4245 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
4246                SourceRange &ERange, bool AllowArraySection = false) {
4247   if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
4248       RefExpr->containsUnexpandedParameterPack())
4249     return std::make_pair(nullptr, true);
4250 
4251   // OpenMP [3.1, C/C++]
4252   //  A list item is a variable name.
4253   // OpenMP  [2.9.3.3, Restrictions, p.1]
4254   //  A variable that is part of another variable (as an array or
4255   //  structure element) cannot appear in a private clause.
4256   RefExpr = RefExpr->IgnoreParens();
4257   enum {
4258     NoArrayExpr = -1,
4259     ArraySubscript = 0,
4260     OMPArraySection = 1
4261   } IsArrayExpr = NoArrayExpr;
4262   if (AllowArraySection) {
4263     if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
4264       Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
4265       while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
4266         Base = TempASE->getBase()->IgnoreParenImpCasts();
4267       RefExpr = Base;
4268       IsArrayExpr = ArraySubscript;
4269     } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
4270       Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
4271       while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
4272         Base = TempOASE->getBase()->IgnoreParenImpCasts();
4273       while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
4274         Base = TempASE->getBase()->IgnoreParenImpCasts();
4275       RefExpr = Base;
4276       IsArrayExpr = OMPArraySection;
4277     }
4278   }
4279   ELoc = RefExpr->getExprLoc();
4280   ERange = RefExpr->getSourceRange();
4281   RefExpr = RefExpr->IgnoreParenImpCasts();
4282   auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
4283   auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
4284   if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
4285       (S.getCurrentThisType().isNull() || !ME ||
4286        !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
4287        !isa<FieldDecl>(ME->getMemberDecl()))) {
4288     if (IsArrayExpr != NoArrayExpr) {
4289       S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
4290                                                          << ERange;
4291     } else {
4292       S.Diag(ELoc,
4293              AllowArraySection
4294                  ? diag::err_omp_expected_var_name_member_expr_or_array_item
4295                  : diag::err_omp_expected_var_name_member_expr)
4296           << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
4297     }
4298     return std::make_pair(nullptr, false);
4299   }
4300   return std::make_pair(
4301       getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
4302 }
4303 
4304 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
4305                                  ArrayRef<OMPClause *> Clauses) {
4306   assert(!S.CurContext->isDependentContext() &&
4307          "Expected non-dependent context.");
4308   auto AllocateRange =
4309       llvm::make_filter_range(Clauses, OMPAllocateClause::classof);
4310   llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>>
4311       DeclToCopy;
4312   auto PrivateRange = llvm::make_filter_range(Clauses, [](const OMPClause *C) {
4313     return isOpenMPPrivate(C->getClauseKind());
4314   });
4315   for (OMPClause *Cl : PrivateRange) {
4316     MutableArrayRef<Expr *>::iterator I, It, Et;
4317     if (Cl->getClauseKind() == OMPC_private) {
4318       auto *PC = cast<OMPPrivateClause>(Cl);
4319       I = PC->private_copies().begin();
4320       It = PC->varlist_begin();
4321       Et = PC->varlist_end();
4322     } else if (Cl->getClauseKind() == OMPC_firstprivate) {
4323       auto *PC = cast<OMPFirstprivateClause>(Cl);
4324       I = PC->private_copies().begin();
4325       It = PC->varlist_begin();
4326       Et = PC->varlist_end();
4327     } else if (Cl->getClauseKind() == OMPC_lastprivate) {
4328       auto *PC = cast<OMPLastprivateClause>(Cl);
4329       I = PC->private_copies().begin();
4330       It = PC->varlist_begin();
4331       Et = PC->varlist_end();
4332     } else if (Cl->getClauseKind() == OMPC_linear) {
4333       auto *PC = cast<OMPLinearClause>(Cl);
4334       I = PC->privates().begin();
4335       It = PC->varlist_begin();
4336       Et = PC->varlist_end();
4337     } else if (Cl->getClauseKind() == OMPC_reduction) {
4338       auto *PC = cast<OMPReductionClause>(Cl);
4339       I = PC->privates().begin();
4340       It = PC->varlist_begin();
4341       Et = PC->varlist_end();
4342     } else if (Cl->getClauseKind() == OMPC_task_reduction) {
4343       auto *PC = cast<OMPTaskReductionClause>(Cl);
4344       I = PC->privates().begin();
4345       It = PC->varlist_begin();
4346       Et = PC->varlist_end();
4347     } else if (Cl->getClauseKind() == OMPC_in_reduction) {
4348       auto *PC = cast<OMPInReductionClause>(Cl);
4349       I = PC->privates().begin();
4350       It = PC->varlist_begin();
4351       Et = PC->varlist_end();
4352     } else {
4353       llvm_unreachable("Expected private clause.");
4354     }
4355     for (Expr *E : llvm::make_range(It, Et)) {
4356       if (!*I) {
4357         ++I;
4358         continue;
4359       }
4360       SourceLocation ELoc;
4361       SourceRange ERange;
4362       Expr *SimpleRefExpr = E;
4363       auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
4364                                 /*AllowArraySection=*/true);
4365       DeclToCopy.try_emplace(Res.first,
4366                              cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()));
4367       ++I;
4368     }
4369   }
4370   for (OMPClause *C : AllocateRange) {
4371     auto *AC = cast<OMPAllocateClause>(C);
4372     OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
4373         getAllocatorKind(S, Stack, AC->getAllocator());
4374     // OpenMP, 2.11.4 allocate Clause, Restrictions.
4375     // For task, taskloop or target directives, allocation requests to memory
4376     // allocators with the trait access set to thread result in unspecified
4377     // behavior.
4378     if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc &&
4379         (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
4380          isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()))) {
4381       S.Diag(AC->getAllocator()->getExprLoc(),
4382              diag::warn_omp_allocate_thread_on_task_target_directive)
4383           << getOpenMPDirectiveName(Stack->getCurrentDirective());
4384     }
4385     for (Expr *E : AC->varlists()) {
4386       SourceLocation ELoc;
4387       SourceRange ERange;
4388       Expr *SimpleRefExpr = E;
4389       auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange);
4390       ValueDecl *VD = Res.first;
4391       DSAStackTy::DSAVarData Data = Stack->getTopDSA(VD, /*FromParent=*/false);
4392       if (!isOpenMPPrivate(Data.CKind)) {
4393         S.Diag(E->getExprLoc(),
4394                diag::err_omp_expected_private_copy_for_allocate);
4395         continue;
4396       }
4397       VarDecl *PrivateVD = DeclToCopy[VD];
4398       if (checkPreviousOMPAllocateAttribute(S, Stack, E, PrivateVD,
4399                                             AllocatorKind, AC->getAllocator()))
4400         continue;
4401       applyOMPAllocateAttribute(S, PrivateVD, AllocatorKind, AC->getAllocator(),
4402                                 E->getSourceRange());
4403     }
4404   }
4405 }
4406 
4407 StmtResult Sema::ActOnOpenMPExecutableDirective(
4408     OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
4409     OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
4410     Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
4411   StmtResult Res = StmtError();
4412   // First check CancelRegion which is then used in checkNestingOfRegions.
4413   if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
4414       checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
4415                             StartLoc))
4416     return StmtError();
4417 
4418   llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
4419   VarsWithInheritedDSAType VarsWithInheritedDSA;
4420   bool ErrorFound = false;
4421   ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
4422   if (AStmt && !CurContext->isDependentContext()) {
4423     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4424 
4425     // Check default data sharing attributes for referenced variables.
4426     DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
4427     int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
4428     Stmt *S = AStmt;
4429     while (--ThisCaptureLevel >= 0)
4430       S = cast<CapturedStmt>(S)->getCapturedStmt();
4431     DSAChecker.Visit(S);
4432     if (!isOpenMPTargetDataManagementDirective(Kind) &&
4433         !isOpenMPTaskingDirective(Kind)) {
4434       // Visit subcaptures to generate implicit clauses for captured vars.
4435       auto *CS = cast<CapturedStmt>(AStmt);
4436       SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
4437       getOpenMPCaptureRegions(CaptureRegions, Kind);
4438       // Ignore outer tasking regions for target directives.
4439       if (CaptureRegions.size() > 1 && CaptureRegions.front() == OMPD_task)
4440         CS = cast<CapturedStmt>(CS->getCapturedStmt());
4441       DSAChecker.visitSubCaptures(CS);
4442     }
4443     if (DSAChecker.isErrorFound())
4444       return StmtError();
4445     // Generate list of implicitly defined firstprivate variables.
4446     VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
4447 
4448     SmallVector<Expr *, 4> ImplicitFirstprivates(
4449         DSAChecker.getImplicitFirstprivate().begin(),
4450         DSAChecker.getImplicitFirstprivate().end());
4451     SmallVector<Expr *, 4> ImplicitMaps[OMPC_MAP_delete];
4452     for (unsigned I = 0; I < OMPC_MAP_delete; ++I) {
4453       ArrayRef<Expr *> ImplicitMap =
4454           DSAChecker.getImplicitMap(static_cast<OpenMPDefaultmapClauseKind>(I));
4455       ImplicitMaps[I].append(ImplicitMap.begin(), ImplicitMap.end());
4456     }
4457     // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
4458     for (OMPClause *C : Clauses) {
4459       if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
4460         for (Expr *E : IRC->taskgroup_descriptors())
4461           if (E)
4462             ImplicitFirstprivates.emplace_back(E);
4463       }
4464     }
4465     if (!ImplicitFirstprivates.empty()) {
4466       if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
4467               ImplicitFirstprivates, SourceLocation(), SourceLocation(),
4468               SourceLocation())) {
4469         ClausesWithImplicit.push_back(Implicit);
4470         ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
4471                      ImplicitFirstprivates.size();
4472       } else {
4473         ErrorFound = true;
4474       }
4475     }
4476     int ClauseKindCnt = -1;
4477     for (ArrayRef<Expr *> ImplicitMap : ImplicitMaps) {
4478       ++ClauseKindCnt;
4479       if (ImplicitMap.empty())
4480         continue;
4481       CXXScopeSpec MapperIdScopeSpec;
4482       DeclarationNameInfo MapperId;
4483       auto Kind = static_cast<OpenMPMapClauseKind>(ClauseKindCnt);
4484       if (OMPClause *Implicit = ActOnOpenMPMapClause(
4485               llvm::None, llvm::None, MapperIdScopeSpec, MapperId, Kind,
4486               /*IsMapTypeImplicit=*/true, SourceLocation(), SourceLocation(),
4487               ImplicitMap, OMPVarListLocTy())) {
4488         ClausesWithImplicit.emplace_back(Implicit);
4489         ErrorFound |=
4490             cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMap.size();
4491       } else {
4492         ErrorFound = true;
4493       }
4494     }
4495   }
4496 
4497   llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
4498   switch (Kind) {
4499   case OMPD_parallel:
4500     Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
4501                                        EndLoc);
4502     AllowedNameModifiers.push_back(OMPD_parallel);
4503     break;
4504   case OMPD_simd:
4505     Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
4506                                    VarsWithInheritedDSA);
4507     if (LangOpts.OpenMP >= 50)
4508       AllowedNameModifiers.push_back(OMPD_simd);
4509     break;
4510   case OMPD_for:
4511     Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
4512                                   VarsWithInheritedDSA);
4513     break;
4514   case OMPD_for_simd:
4515     Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
4516                                       EndLoc, VarsWithInheritedDSA);
4517     if (LangOpts.OpenMP >= 50)
4518       AllowedNameModifiers.push_back(OMPD_simd);
4519     break;
4520   case OMPD_sections:
4521     Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
4522                                        EndLoc);
4523     break;
4524   case OMPD_section:
4525     assert(ClausesWithImplicit.empty() &&
4526            "No clauses are allowed for 'omp section' directive");
4527     Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
4528     break;
4529   case OMPD_single:
4530     Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
4531                                      EndLoc);
4532     break;
4533   case OMPD_master:
4534     assert(ClausesWithImplicit.empty() &&
4535            "No clauses are allowed for 'omp master' directive");
4536     Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
4537     break;
4538   case OMPD_critical:
4539     Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
4540                                        StartLoc, EndLoc);
4541     break;
4542   case OMPD_parallel_for:
4543     Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
4544                                           EndLoc, VarsWithInheritedDSA);
4545     AllowedNameModifiers.push_back(OMPD_parallel);
4546     break;
4547   case OMPD_parallel_for_simd:
4548     Res = ActOnOpenMPParallelForSimdDirective(
4549         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4550     AllowedNameModifiers.push_back(OMPD_parallel);
4551     if (LangOpts.OpenMP >= 50)
4552       AllowedNameModifiers.push_back(OMPD_simd);
4553     break;
4554   case OMPD_parallel_master:
4555     Res = ActOnOpenMPParallelMasterDirective(ClausesWithImplicit, AStmt,
4556                                                StartLoc, EndLoc);
4557     AllowedNameModifiers.push_back(OMPD_parallel);
4558     break;
4559   case OMPD_parallel_sections:
4560     Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
4561                                                StartLoc, EndLoc);
4562     AllowedNameModifiers.push_back(OMPD_parallel);
4563     break;
4564   case OMPD_task:
4565     Res =
4566         ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
4567     AllowedNameModifiers.push_back(OMPD_task);
4568     break;
4569   case OMPD_taskyield:
4570     assert(ClausesWithImplicit.empty() &&
4571            "No clauses are allowed for 'omp taskyield' directive");
4572     assert(AStmt == nullptr &&
4573            "No associated statement allowed for 'omp taskyield' directive");
4574     Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
4575     break;
4576   case OMPD_barrier:
4577     assert(ClausesWithImplicit.empty() &&
4578            "No clauses are allowed for 'omp barrier' directive");
4579     assert(AStmt == nullptr &&
4580            "No associated statement allowed for 'omp barrier' directive");
4581     Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
4582     break;
4583   case OMPD_taskwait:
4584     assert(ClausesWithImplicit.empty() &&
4585            "No clauses are allowed for 'omp taskwait' directive");
4586     assert(AStmt == nullptr &&
4587            "No associated statement allowed for 'omp taskwait' directive");
4588     Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
4589     break;
4590   case OMPD_taskgroup:
4591     Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
4592                                         EndLoc);
4593     break;
4594   case OMPD_flush:
4595     assert(AStmt == nullptr &&
4596            "No associated statement allowed for 'omp flush' directive");
4597     Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
4598     break;
4599   case OMPD_ordered:
4600     Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
4601                                       EndLoc);
4602     break;
4603   case OMPD_atomic:
4604     Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
4605                                      EndLoc);
4606     break;
4607   case OMPD_teams:
4608     Res =
4609         ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
4610     break;
4611   case OMPD_target:
4612     Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
4613                                      EndLoc);
4614     AllowedNameModifiers.push_back(OMPD_target);
4615     break;
4616   case OMPD_target_parallel:
4617     Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
4618                                              StartLoc, EndLoc);
4619     AllowedNameModifiers.push_back(OMPD_target);
4620     AllowedNameModifiers.push_back(OMPD_parallel);
4621     break;
4622   case OMPD_target_parallel_for:
4623     Res = ActOnOpenMPTargetParallelForDirective(
4624         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4625     AllowedNameModifiers.push_back(OMPD_target);
4626     AllowedNameModifiers.push_back(OMPD_parallel);
4627     break;
4628   case OMPD_cancellation_point:
4629     assert(ClausesWithImplicit.empty() &&
4630            "No clauses are allowed for 'omp cancellation point' directive");
4631     assert(AStmt == nullptr && "No associated statement allowed for 'omp "
4632                                "cancellation point' directive");
4633     Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
4634     break;
4635   case OMPD_cancel:
4636     assert(AStmt == nullptr &&
4637            "No associated statement allowed for 'omp cancel' directive");
4638     Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
4639                                      CancelRegion);
4640     AllowedNameModifiers.push_back(OMPD_cancel);
4641     break;
4642   case OMPD_target_data:
4643     Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
4644                                          EndLoc);
4645     AllowedNameModifiers.push_back(OMPD_target_data);
4646     break;
4647   case OMPD_target_enter_data:
4648     Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
4649                                               EndLoc, AStmt);
4650     AllowedNameModifiers.push_back(OMPD_target_enter_data);
4651     break;
4652   case OMPD_target_exit_data:
4653     Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
4654                                              EndLoc, AStmt);
4655     AllowedNameModifiers.push_back(OMPD_target_exit_data);
4656     break;
4657   case OMPD_taskloop:
4658     Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
4659                                        EndLoc, VarsWithInheritedDSA);
4660     AllowedNameModifiers.push_back(OMPD_taskloop);
4661     break;
4662   case OMPD_taskloop_simd:
4663     Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
4664                                            EndLoc, VarsWithInheritedDSA);
4665     AllowedNameModifiers.push_back(OMPD_taskloop);
4666     if (LangOpts.OpenMP >= 50)
4667       AllowedNameModifiers.push_back(OMPD_simd);
4668     break;
4669   case OMPD_master_taskloop:
4670     Res = ActOnOpenMPMasterTaskLoopDirective(
4671         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4672     AllowedNameModifiers.push_back(OMPD_taskloop);
4673     break;
4674   case OMPD_master_taskloop_simd:
4675     Res = ActOnOpenMPMasterTaskLoopSimdDirective(
4676         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4677     AllowedNameModifiers.push_back(OMPD_taskloop);
4678     if (LangOpts.OpenMP >= 50)
4679       AllowedNameModifiers.push_back(OMPD_simd);
4680     break;
4681   case OMPD_parallel_master_taskloop:
4682     Res = ActOnOpenMPParallelMasterTaskLoopDirective(
4683         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4684     AllowedNameModifiers.push_back(OMPD_taskloop);
4685     AllowedNameModifiers.push_back(OMPD_parallel);
4686     break;
4687   case OMPD_parallel_master_taskloop_simd:
4688     Res = ActOnOpenMPParallelMasterTaskLoopSimdDirective(
4689         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4690     AllowedNameModifiers.push_back(OMPD_taskloop);
4691     AllowedNameModifiers.push_back(OMPD_parallel);
4692     if (LangOpts.OpenMP >= 50)
4693       AllowedNameModifiers.push_back(OMPD_simd);
4694     break;
4695   case OMPD_distribute:
4696     Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
4697                                          EndLoc, VarsWithInheritedDSA);
4698     break;
4699   case OMPD_target_update:
4700     Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
4701                                            EndLoc, AStmt);
4702     AllowedNameModifiers.push_back(OMPD_target_update);
4703     break;
4704   case OMPD_distribute_parallel_for:
4705     Res = ActOnOpenMPDistributeParallelForDirective(
4706         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4707     AllowedNameModifiers.push_back(OMPD_parallel);
4708     break;
4709   case OMPD_distribute_parallel_for_simd:
4710     Res = ActOnOpenMPDistributeParallelForSimdDirective(
4711         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4712     AllowedNameModifiers.push_back(OMPD_parallel);
4713     if (LangOpts.OpenMP >= 50)
4714       AllowedNameModifiers.push_back(OMPD_simd);
4715     break;
4716   case OMPD_distribute_simd:
4717     Res = ActOnOpenMPDistributeSimdDirective(
4718         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4719     if (LangOpts.OpenMP >= 50)
4720       AllowedNameModifiers.push_back(OMPD_simd);
4721     break;
4722   case OMPD_target_parallel_for_simd:
4723     Res = ActOnOpenMPTargetParallelForSimdDirective(
4724         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4725     AllowedNameModifiers.push_back(OMPD_target);
4726     AllowedNameModifiers.push_back(OMPD_parallel);
4727     break;
4728   case OMPD_target_simd:
4729     Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
4730                                          EndLoc, VarsWithInheritedDSA);
4731     AllowedNameModifiers.push_back(OMPD_target);
4732     break;
4733   case OMPD_teams_distribute:
4734     Res = ActOnOpenMPTeamsDistributeDirective(
4735         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4736     break;
4737   case OMPD_teams_distribute_simd:
4738     Res = ActOnOpenMPTeamsDistributeSimdDirective(
4739         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4740     break;
4741   case OMPD_teams_distribute_parallel_for_simd:
4742     Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
4743         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4744     AllowedNameModifiers.push_back(OMPD_parallel);
4745     break;
4746   case OMPD_teams_distribute_parallel_for:
4747     Res = ActOnOpenMPTeamsDistributeParallelForDirective(
4748         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4749     AllowedNameModifiers.push_back(OMPD_parallel);
4750     break;
4751   case OMPD_target_teams:
4752     Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
4753                                           EndLoc);
4754     AllowedNameModifiers.push_back(OMPD_target);
4755     break;
4756   case OMPD_target_teams_distribute:
4757     Res = ActOnOpenMPTargetTeamsDistributeDirective(
4758         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4759     AllowedNameModifiers.push_back(OMPD_target);
4760     break;
4761   case OMPD_target_teams_distribute_parallel_for:
4762     Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
4763         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4764     AllowedNameModifiers.push_back(OMPD_target);
4765     AllowedNameModifiers.push_back(OMPD_parallel);
4766     break;
4767   case OMPD_target_teams_distribute_parallel_for_simd:
4768     Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
4769         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4770     AllowedNameModifiers.push_back(OMPD_target);
4771     AllowedNameModifiers.push_back(OMPD_parallel);
4772     break;
4773   case OMPD_target_teams_distribute_simd:
4774     Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
4775         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4776     AllowedNameModifiers.push_back(OMPD_target);
4777     break;
4778   case OMPD_declare_target:
4779   case OMPD_end_declare_target:
4780   case OMPD_threadprivate:
4781   case OMPD_allocate:
4782   case OMPD_declare_reduction:
4783   case OMPD_declare_mapper:
4784   case OMPD_declare_simd:
4785   case OMPD_requires:
4786   case OMPD_declare_variant:
4787     llvm_unreachable("OpenMP Directive is not allowed");
4788   case OMPD_unknown:
4789     llvm_unreachable("Unknown OpenMP directive");
4790   }
4791 
4792   ErrorFound = Res.isInvalid() || ErrorFound;
4793 
4794   // Check variables in the clauses if default(none) was specified.
4795   if (DSAStack->getDefaultDSA() == DSA_none) {
4796     DSAAttrChecker DSAChecker(DSAStack, *this, nullptr);
4797     for (OMPClause *C : Clauses) {
4798       switch (C->getClauseKind()) {
4799       case OMPC_num_threads:
4800       case OMPC_dist_schedule:
4801         // Do not analyse if no parent teams directive.
4802         if (isOpenMPTeamsDirective(Kind))
4803           break;
4804         continue;
4805       case OMPC_if:
4806         if (isOpenMPTeamsDirective(Kind) &&
4807             cast<OMPIfClause>(C)->getNameModifier() != OMPD_target)
4808           break;
4809         if (isOpenMPParallelDirective(Kind) &&
4810             isOpenMPTaskLoopDirective(Kind) &&
4811             cast<OMPIfClause>(C)->getNameModifier() != OMPD_parallel)
4812           break;
4813         continue;
4814       case OMPC_schedule:
4815         break;
4816       case OMPC_grainsize:
4817       case OMPC_num_tasks:
4818       case OMPC_final:
4819       case OMPC_priority:
4820         // Do not analyze if no parent parallel directive.
4821         if (isOpenMPParallelDirective(Kind))
4822           break;
4823         continue;
4824       case OMPC_ordered:
4825       case OMPC_device:
4826       case OMPC_num_teams:
4827       case OMPC_thread_limit:
4828       case OMPC_hint:
4829       case OMPC_collapse:
4830       case OMPC_safelen:
4831       case OMPC_simdlen:
4832       case OMPC_default:
4833       case OMPC_proc_bind:
4834       case OMPC_private:
4835       case OMPC_firstprivate:
4836       case OMPC_lastprivate:
4837       case OMPC_shared:
4838       case OMPC_reduction:
4839       case OMPC_task_reduction:
4840       case OMPC_in_reduction:
4841       case OMPC_linear:
4842       case OMPC_aligned:
4843       case OMPC_copyin:
4844       case OMPC_copyprivate:
4845       case OMPC_nowait:
4846       case OMPC_untied:
4847       case OMPC_mergeable:
4848       case OMPC_allocate:
4849       case OMPC_read:
4850       case OMPC_write:
4851       case OMPC_update:
4852       case OMPC_capture:
4853       case OMPC_seq_cst:
4854       case OMPC_depend:
4855       case OMPC_threads:
4856       case OMPC_simd:
4857       case OMPC_map:
4858       case OMPC_nogroup:
4859       case OMPC_defaultmap:
4860       case OMPC_to:
4861       case OMPC_from:
4862       case OMPC_use_device_ptr:
4863       case OMPC_is_device_ptr:
4864         continue;
4865       case OMPC_allocator:
4866       case OMPC_flush:
4867       case OMPC_threadprivate:
4868       case OMPC_uniform:
4869       case OMPC_unknown:
4870       case OMPC_unified_address:
4871       case OMPC_unified_shared_memory:
4872       case OMPC_reverse_offload:
4873       case OMPC_dynamic_allocators:
4874       case OMPC_atomic_default_mem_order:
4875       case OMPC_device_type:
4876       case OMPC_match:
4877         llvm_unreachable("Unexpected clause");
4878       }
4879       for (Stmt *CC : C->children()) {
4880         if (CC)
4881           DSAChecker.Visit(CC);
4882       }
4883     }
4884     for (auto &P : DSAChecker.getVarsWithInheritedDSA())
4885       VarsWithInheritedDSA[P.getFirst()] = P.getSecond();
4886   }
4887   for (const auto &P : VarsWithInheritedDSA) {
4888     if (P.getFirst()->isImplicit() || isa<OMPCapturedExprDecl>(P.getFirst()))
4889       continue;
4890     ErrorFound = true;
4891     if (DSAStack->getDefaultDSA() == DSA_none) {
4892       Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
4893           << P.first << P.second->getSourceRange();
4894       Diag(DSAStack->getDefaultDSALocation(), diag::note_omp_default_dsa_none);
4895     } else if (getLangOpts().OpenMP >= 50) {
4896       Diag(P.second->getExprLoc(),
4897            diag::err_omp_defaultmap_no_attr_for_variable)
4898           << P.first << P.second->getSourceRange();
4899       Diag(DSAStack->getDefaultDSALocation(),
4900            diag::note_omp_defaultmap_attr_none);
4901     }
4902   }
4903 
4904   if (!AllowedNameModifiers.empty())
4905     ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
4906                  ErrorFound;
4907 
4908   if (ErrorFound)
4909     return StmtError();
4910 
4911   if (!(Res.getAs<OMPExecutableDirective>()->isStandaloneDirective())) {
4912     Res.getAs<OMPExecutableDirective>()
4913         ->getStructuredBlock()
4914         ->setIsOMPStructuredBlock(true);
4915   }
4916 
4917   if (!CurContext->isDependentContext() &&
4918       isOpenMPTargetExecutionDirective(Kind) &&
4919       !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() ||
4920         DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() ||
4921         DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() ||
4922         DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) {
4923     // Register target to DSA Stack.
4924     DSAStack->addTargetDirLocation(StartLoc);
4925   }
4926 
4927   return Res;
4928 }
4929 
4930 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
4931     DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
4932     ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
4933     ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
4934     ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
4935   assert(Aligneds.size() == Alignments.size());
4936   assert(Linears.size() == LinModifiers.size());
4937   assert(Linears.size() == Steps.size());
4938   if (!DG || DG.get().isNull())
4939     return DeclGroupPtrTy();
4940 
4941   const int SimdId = 0;
4942   if (!DG.get().isSingleDecl()) {
4943     Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant)
4944         << SimdId;
4945     return DG;
4946   }
4947   Decl *ADecl = DG.get().getSingleDecl();
4948   if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
4949     ADecl = FTD->getTemplatedDecl();
4950 
4951   auto *FD = dyn_cast<FunctionDecl>(ADecl);
4952   if (!FD) {
4953     Diag(ADecl->getLocation(), diag::err_omp_function_expected) << SimdId;
4954     return DeclGroupPtrTy();
4955   }
4956 
4957   // OpenMP [2.8.2, declare simd construct, Description]
4958   // The parameter of the simdlen clause must be a constant positive integer
4959   // expression.
4960   ExprResult SL;
4961   if (Simdlen)
4962     SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
4963   // OpenMP [2.8.2, declare simd construct, Description]
4964   // The special this pointer can be used as if was one of the arguments to the
4965   // function in any of the linear, aligned, or uniform clauses.
4966   // The uniform clause declares one or more arguments to have an invariant
4967   // value for all concurrent invocations of the function in the execution of a
4968   // single SIMD loop.
4969   llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
4970   const Expr *UniformedLinearThis = nullptr;
4971   for (const Expr *E : Uniforms) {
4972     E = E->IgnoreParenImpCasts();
4973     if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
4974       if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
4975         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
4976             FD->getParamDecl(PVD->getFunctionScopeIndex())
4977                     ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
4978           UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E);
4979           continue;
4980         }
4981     if (isa<CXXThisExpr>(E)) {
4982       UniformedLinearThis = E;
4983       continue;
4984     }
4985     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
4986         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
4987   }
4988   // OpenMP [2.8.2, declare simd construct, Description]
4989   // The aligned clause declares that the object to which each list item points
4990   // is aligned to the number of bytes expressed in the optional parameter of
4991   // the aligned clause.
4992   // The special this pointer can be used as if was one of the arguments to the
4993   // function in any of the linear, aligned, or uniform clauses.
4994   // The type of list items appearing in the aligned clause must be array,
4995   // pointer, reference to array, or reference to pointer.
4996   llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
4997   const Expr *AlignedThis = nullptr;
4998   for (const Expr *E : Aligneds) {
4999     E = E->IgnoreParenImpCasts();
5000     if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
5001       if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
5002         const VarDecl *CanonPVD = PVD->getCanonicalDecl();
5003         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
5004             FD->getParamDecl(PVD->getFunctionScopeIndex())
5005                     ->getCanonicalDecl() == CanonPVD) {
5006           // OpenMP  [2.8.1, simd construct, Restrictions]
5007           // A list-item cannot appear in more than one aligned clause.
5008           if (AlignedArgs.count(CanonPVD) > 0) {
5009             Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
5010                 << 1 << E->getSourceRange();
5011             Diag(AlignedArgs[CanonPVD]->getExprLoc(),
5012                  diag::note_omp_explicit_dsa)
5013                 << getOpenMPClauseName(OMPC_aligned);
5014             continue;
5015           }
5016           AlignedArgs[CanonPVD] = E;
5017           QualType QTy = PVD->getType()
5018                              .getNonReferenceType()
5019                              .getUnqualifiedType()
5020                              .getCanonicalType();
5021           const Type *Ty = QTy.getTypePtrOrNull();
5022           if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
5023             Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
5024                 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
5025             Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
5026           }
5027           continue;
5028         }
5029       }
5030     if (isa<CXXThisExpr>(E)) {
5031       if (AlignedThis) {
5032         Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
5033             << 2 << E->getSourceRange();
5034         Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
5035             << getOpenMPClauseName(OMPC_aligned);
5036       }
5037       AlignedThis = E;
5038       continue;
5039     }
5040     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
5041         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
5042   }
5043   // The optional parameter of the aligned clause, alignment, must be a constant
5044   // positive integer expression. If no optional parameter is specified,
5045   // implementation-defined default alignments for SIMD instructions on the
5046   // target platforms are assumed.
5047   SmallVector<const Expr *, 4> NewAligns;
5048   for (Expr *E : Alignments) {
5049     ExprResult Align;
5050     if (E)
5051       Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
5052     NewAligns.push_back(Align.get());
5053   }
5054   // OpenMP [2.8.2, declare simd construct, Description]
5055   // The linear clause declares one or more list items to be private to a SIMD
5056   // lane and to have a linear relationship with respect to the iteration space
5057   // of a loop.
5058   // The special this pointer can be used as if was one of the arguments to the
5059   // function in any of the linear, aligned, or uniform clauses.
5060   // When a linear-step expression is specified in a linear clause it must be
5061   // either a constant integer expression or an integer-typed parameter that is
5062   // specified in a uniform clause on the directive.
5063   llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
5064   const bool IsUniformedThis = UniformedLinearThis != nullptr;
5065   auto MI = LinModifiers.begin();
5066   for (const Expr *E : Linears) {
5067     auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
5068     ++MI;
5069     E = E->IgnoreParenImpCasts();
5070     if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
5071       if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
5072         const VarDecl *CanonPVD = PVD->getCanonicalDecl();
5073         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
5074             FD->getParamDecl(PVD->getFunctionScopeIndex())
5075                     ->getCanonicalDecl() == CanonPVD) {
5076           // OpenMP  [2.15.3.7, linear Clause, Restrictions]
5077           // A list-item cannot appear in more than one linear clause.
5078           if (LinearArgs.count(CanonPVD) > 0) {
5079             Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
5080                 << getOpenMPClauseName(OMPC_linear)
5081                 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
5082             Diag(LinearArgs[CanonPVD]->getExprLoc(),
5083                  diag::note_omp_explicit_dsa)
5084                 << getOpenMPClauseName(OMPC_linear);
5085             continue;
5086           }
5087           // Each argument can appear in at most one uniform or linear clause.
5088           if (UniformedArgs.count(CanonPVD) > 0) {
5089             Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
5090                 << getOpenMPClauseName(OMPC_linear)
5091                 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
5092             Diag(UniformedArgs[CanonPVD]->getExprLoc(),
5093                  diag::note_omp_explicit_dsa)
5094                 << getOpenMPClauseName(OMPC_uniform);
5095             continue;
5096           }
5097           LinearArgs[CanonPVD] = E;
5098           if (E->isValueDependent() || E->isTypeDependent() ||
5099               E->isInstantiationDependent() ||
5100               E->containsUnexpandedParameterPack())
5101             continue;
5102           (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
5103                                       PVD->getOriginalType());
5104           continue;
5105         }
5106       }
5107     if (isa<CXXThisExpr>(E)) {
5108       if (UniformedLinearThis) {
5109         Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
5110             << getOpenMPClauseName(OMPC_linear)
5111             << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
5112             << E->getSourceRange();
5113         Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
5114             << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
5115                                                    : OMPC_linear);
5116         continue;
5117       }
5118       UniformedLinearThis = E;
5119       if (E->isValueDependent() || E->isTypeDependent() ||
5120           E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
5121         continue;
5122       (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
5123                                   E->getType());
5124       continue;
5125     }
5126     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
5127         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
5128   }
5129   Expr *Step = nullptr;
5130   Expr *NewStep = nullptr;
5131   SmallVector<Expr *, 4> NewSteps;
5132   for (Expr *E : Steps) {
5133     // Skip the same step expression, it was checked already.
5134     if (Step == E || !E) {
5135       NewSteps.push_back(E ? NewStep : nullptr);
5136       continue;
5137     }
5138     Step = E;
5139     if (const auto *DRE = dyn_cast<DeclRefExpr>(Step))
5140       if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
5141         const VarDecl *CanonPVD = PVD->getCanonicalDecl();
5142         if (UniformedArgs.count(CanonPVD) == 0) {
5143           Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
5144               << Step->getSourceRange();
5145         } else if (E->isValueDependent() || E->isTypeDependent() ||
5146                    E->isInstantiationDependent() ||
5147                    E->containsUnexpandedParameterPack() ||
5148                    CanonPVD->getType()->hasIntegerRepresentation()) {
5149           NewSteps.push_back(Step);
5150         } else {
5151           Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
5152               << Step->getSourceRange();
5153         }
5154         continue;
5155       }
5156     NewStep = Step;
5157     if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
5158         !Step->isInstantiationDependent() &&
5159         !Step->containsUnexpandedParameterPack()) {
5160       NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
5161                     .get();
5162       if (NewStep)
5163         NewStep = VerifyIntegerConstantExpression(NewStep).get();
5164     }
5165     NewSteps.push_back(NewStep);
5166   }
5167   auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
5168       Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
5169       Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
5170       const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
5171       const_cast<Expr **>(Linears.data()), Linears.size(),
5172       const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
5173       NewSteps.data(), NewSteps.size(), SR);
5174   ADecl->addAttr(NewAttr);
5175   return DG;
5176 }
5177 
5178 Optional<std::pair<FunctionDecl *, Expr *>>
5179 Sema::checkOpenMPDeclareVariantFunction(Sema::DeclGroupPtrTy DG,
5180                                         Expr *VariantRef, SourceRange SR) {
5181   if (!DG || DG.get().isNull())
5182     return None;
5183 
5184   const int VariantId = 1;
5185   // Must be applied only to single decl.
5186   if (!DG.get().isSingleDecl()) {
5187     Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant)
5188         << VariantId << SR;
5189     return None;
5190   }
5191   Decl *ADecl = DG.get().getSingleDecl();
5192   if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
5193     ADecl = FTD->getTemplatedDecl();
5194 
5195   // Decl must be a function.
5196   auto *FD = dyn_cast<FunctionDecl>(ADecl);
5197   if (!FD) {
5198     Diag(ADecl->getLocation(), diag::err_omp_function_expected)
5199         << VariantId << SR;
5200     return None;
5201   }
5202 
5203   auto &&HasMultiVersionAttributes = [](const FunctionDecl *FD) {
5204     return FD->hasAttrs() &&
5205            (FD->hasAttr<CPUDispatchAttr>() || FD->hasAttr<CPUSpecificAttr>() ||
5206             FD->hasAttr<TargetAttr>());
5207   };
5208   // OpenMP is not compatible with CPU-specific attributes.
5209   if (HasMultiVersionAttributes(FD)) {
5210     Diag(FD->getLocation(), diag::err_omp_declare_variant_incompat_attributes)
5211         << SR;
5212     return None;
5213   }
5214 
5215   // Allow #pragma omp declare variant only if the function is not used.
5216   if (FD->isUsed(false))
5217     Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_used)
5218         << FD->getLocation();
5219 
5220   // Check if the function was emitted already.
5221   const FunctionDecl *Definition;
5222   if (!FD->isThisDeclarationADefinition() && FD->isDefined(Definition) &&
5223       (LangOpts.EmitAllDecls || Context.DeclMustBeEmitted(Definition)))
5224     Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_emitted)
5225         << FD->getLocation();
5226 
5227   // The VariantRef must point to function.
5228   if (!VariantRef) {
5229     Diag(SR.getBegin(), diag::err_omp_function_expected) << VariantId;
5230     return None;
5231   }
5232 
5233   // Do not check templates, wait until instantiation.
5234   if (VariantRef->isTypeDependent() || VariantRef->isValueDependent() ||
5235       VariantRef->containsUnexpandedParameterPack() ||
5236       VariantRef->isInstantiationDependent() || FD->isDependentContext())
5237     return std::make_pair(FD, VariantRef);
5238 
5239   // Convert VariantRef expression to the type of the original function to
5240   // resolve possible conflicts.
5241   ExprResult VariantRefCast;
5242   if (LangOpts.CPlusPlus) {
5243     QualType FnPtrType;
5244     auto *Method = dyn_cast<CXXMethodDecl>(FD);
5245     if (Method && !Method->isStatic()) {
5246       const Type *ClassType =
5247           Context.getTypeDeclType(Method->getParent()).getTypePtr();
5248       FnPtrType = Context.getMemberPointerType(FD->getType(), ClassType);
5249       ExprResult ER;
5250       {
5251         // Build adrr_of unary op to correctly handle type checks for member
5252         // functions.
5253         Sema::TentativeAnalysisScope Trap(*this);
5254         ER = CreateBuiltinUnaryOp(VariantRef->getBeginLoc(), UO_AddrOf,
5255                                   VariantRef);
5256       }
5257       if (!ER.isUsable()) {
5258         Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected)
5259             << VariantId << VariantRef->getSourceRange();
5260         return None;
5261       }
5262       VariantRef = ER.get();
5263     } else {
5264       FnPtrType = Context.getPointerType(FD->getType());
5265     }
5266     ImplicitConversionSequence ICS =
5267         TryImplicitConversion(VariantRef, FnPtrType.getUnqualifiedType(),
5268                               /*SuppressUserConversions=*/false,
5269                               /*AllowExplicit=*/false,
5270                               /*InOverloadResolution=*/false,
5271                               /*CStyle=*/false,
5272                               /*AllowObjCWritebackConversion=*/false);
5273     if (ICS.isFailure()) {
5274       Diag(VariantRef->getExprLoc(),
5275            diag::err_omp_declare_variant_incompat_types)
5276           << VariantRef->getType() << FnPtrType << VariantRef->getSourceRange();
5277       return None;
5278     }
5279     VariantRefCast = PerformImplicitConversion(
5280         VariantRef, FnPtrType.getUnqualifiedType(), AA_Converting);
5281     if (!VariantRefCast.isUsable())
5282       return None;
5283     // Drop previously built artificial addr_of unary op for member functions.
5284     if (Method && !Method->isStatic()) {
5285       Expr *PossibleAddrOfVariantRef = VariantRefCast.get();
5286       if (auto *UO = dyn_cast<UnaryOperator>(
5287               PossibleAddrOfVariantRef->IgnoreImplicit()))
5288         VariantRefCast = UO->getSubExpr();
5289     }
5290   } else {
5291     VariantRefCast = VariantRef;
5292   }
5293 
5294   ExprResult ER = CheckPlaceholderExpr(VariantRefCast.get());
5295   if (!ER.isUsable() ||
5296       !ER.get()->IgnoreParenImpCasts()->getType()->isFunctionType()) {
5297     Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected)
5298         << VariantId << VariantRef->getSourceRange();
5299     return None;
5300   }
5301 
5302   // The VariantRef must point to function.
5303   auto *DRE = dyn_cast<DeclRefExpr>(ER.get()->IgnoreParenImpCasts());
5304   if (!DRE) {
5305     Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected)
5306         << VariantId << VariantRef->getSourceRange();
5307     return None;
5308   }
5309   auto *NewFD = dyn_cast_or_null<FunctionDecl>(DRE->getDecl());
5310   if (!NewFD) {
5311     Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected)
5312         << VariantId << VariantRef->getSourceRange();
5313     return None;
5314   }
5315 
5316   // Check if variant function is not marked with declare variant directive.
5317   if (NewFD->hasAttrs() && NewFD->hasAttr<OMPDeclareVariantAttr>()) {
5318     Diag(VariantRef->getExprLoc(),
5319          diag::warn_omp_declare_variant_marked_as_declare_variant)
5320         << VariantRef->getSourceRange();
5321     SourceRange SR =
5322         NewFD->specific_attr_begin<OMPDeclareVariantAttr>()->getRange();
5323     Diag(SR.getBegin(), diag::note_omp_marked_declare_variant_here) << SR;
5324     return None;
5325   }
5326 
5327   enum DoesntSupport {
5328     VirtFuncs = 1,
5329     Constructors = 3,
5330     Destructors = 4,
5331     DeletedFuncs = 5,
5332     DefaultedFuncs = 6,
5333     ConstexprFuncs = 7,
5334     ConstevalFuncs = 8,
5335   };
5336   if (const auto *CXXFD = dyn_cast<CXXMethodDecl>(FD)) {
5337     if (CXXFD->isVirtual()) {
5338       Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
5339           << VirtFuncs;
5340       return None;
5341     }
5342 
5343     if (isa<CXXConstructorDecl>(FD)) {
5344       Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
5345           << Constructors;
5346       return None;
5347     }
5348 
5349     if (isa<CXXDestructorDecl>(FD)) {
5350       Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
5351           << Destructors;
5352       return None;
5353     }
5354   }
5355 
5356   if (FD->isDeleted()) {
5357     Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
5358         << DeletedFuncs;
5359     return None;
5360   }
5361 
5362   if (FD->isDefaulted()) {
5363     Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
5364         << DefaultedFuncs;
5365     return None;
5366   }
5367 
5368   if (FD->isConstexpr()) {
5369     Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
5370         << (NewFD->isConsteval() ? ConstevalFuncs : ConstexprFuncs);
5371     return None;
5372   }
5373 
5374   // Check general compatibility.
5375   if (areMultiversionVariantFunctionsCompatible(
5376           FD, NewFD, PDiag(diag::err_omp_declare_variant_noproto),
5377           PartialDiagnosticAt(
5378               SR.getBegin(),
5379               PDiag(diag::note_omp_declare_variant_specified_here) << SR),
5380           PartialDiagnosticAt(
5381               VariantRef->getExprLoc(),
5382               PDiag(diag::err_omp_declare_variant_doesnt_support)),
5383           PartialDiagnosticAt(VariantRef->getExprLoc(),
5384                               PDiag(diag::err_omp_declare_variant_diff)
5385                                   << FD->getLocation()),
5386           /*TemplatesSupported=*/true, /*ConstexprSupported=*/false,
5387           /*CLinkageMayDiffer=*/true))
5388     return None;
5389   return std::make_pair(FD, cast<Expr>(DRE));
5390 }
5391 
5392 void Sema::ActOnOpenMPDeclareVariantDirective(
5393     FunctionDecl *FD, Expr *VariantRef, SourceRange SR,
5394     ArrayRef<OMPCtxSelectorData> Data) {
5395   if (Data.empty())
5396     return;
5397   SmallVector<Expr *, 4> CtxScores;
5398   SmallVector<unsigned, 4> CtxSets;
5399   SmallVector<unsigned, 4> Ctxs;
5400   SmallVector<StringRef, 4> ImplVendors, DeviceKinds;
5401   bool IsError = false;
5402   for (const OMPCtxSelectorData &D : Data) {
5403     OpenMPContextSelectorSetKind CtxSet = D.CtxSet;
5404     OpenMPContextSelectorKind Ctx = D.Ctx;
5405     if (CtxSet == OMP_CTX_SET_unknown || Ctx == OMP_CTX_unknown)
5406       return;
5407     Expr *Score = nullptr;
5408     if (D.Score.isUsable()) {
5409       Score = D.Score.get();
5410       if (!Score->isTypeDependent() && !Score->isValueDependent() &&
5411           !Score->isInstantiationDependent() &&
5412           !Score->containsUnexpandedParameterPack()) {
5413         Score =
5414             PerformOpenMPImplicitIntegerConversion(Score->getExprLoc(), Score)
5415                 .get();
5416         if (Score)
5417           Score = VerifyIntegerConstantExpression(Score).get();
5418       }
5419     } else {
5420       // OpenMP 5.0, 2.3.3 Matching and Scoring Context Selectors.
5421       // The kind, arch, and isa selectors are given the values 2^l, 2^(l+1) and
5422       // 2^(l+2), respectively, where l is the number of traits in the construct
5423       // set.
5424       // TODO: implement correct logic for isa and arch traits.
5425       // TODO: take the construct context set into account when it is
5426       // implemented.
5427       int L = 0; // Currently set the number of traits in construct set to 0,
5428                  // since the construct trait set in not supported yet.
5429       if (CtxSet == OMP_CTX_SET_device && Ctx == OMP_CTX_kind)
5430         Score = ActOnIntegerConstant(SourceLocation(), std::pow(2, L)).get();
5431       else
5432         Score = ActOnIntegerConstant(SourceLocation(), 0).get();
5433     }
5434     switch (Ctx) {
5435     case OMP_CTX_vendor:
5436       assert(CtxSet == OMP_CTX_SET_implementation &&
5437              "Expected implementation context selector set.");
5438       ImplVendors.append(D.Names.begin(), D.Names.end());
5439       break;
5440     case OMP_CTX_kind:
5441       assert(CtxSet == OMP_CTX_SET_device &&
5442              "Expected device context selector set.");
5443       DeviceKinds.append(D.Names.begin(), D.Names.end());
5444       break;
5445     case OMP_CTX_unknown:
5446       llvm_unreachable("Unknown context selector kind.");
5447     }
5448     IsError = IsError || !Score;
5449     CtxSets.push_back(CtxSet);
5450     Ctxs.push_back(Ctx);
5451     CtxScores.push_back(Score);
5452   }
5453   if (!IsError) {
5454     auto *NewAttr = OMPDeclareVariantAttr::CreateImplicit(
5455         Context, VariantRef, CtxScores.begin(), CtxScores.size(),
5456         CtxSets.begin(), CtxSets.size(), Ctxs.begin(), Ctxs.size(),
5457         ImplVendors.begin(), ImplVendors.size(), DeviceKinds.begin(),
5458         DeviceKinds.size(), SR);
5459     FD->addAttr(NewAttr);
5460   }
5461 }
5462 
5463 void Sema::markOpenMPDeclareVariantFuncsReferenced(SourceLocation Loc,
5464                                                    FunctionDecl *Func,
5465                                                    bool MightBeOdrUse) {
5466   assert(LangOpts.OpenMP && "Expected OpenMP mode.");
5467 
5468   if (!Func->isDependentContext() && Func->hasAttrs()) {
5469     for (OMPDeclareVariantAttr *A :
5470          Func->specific_attrs<OMPDeclareVariantAttr>()) {
5471       // TODO: add checks for active OpenMP context where possible.
5472       Expr *VariantRef = A->getVariantFuncRef();
5473       auto *DRE = cast<DeclRefExpr>(VariantRef->IgnoreParenImpCasts());
5474       auto *F = cast<FunctionDecl>(DRE->getDecl());
5475       if (!F->isDefined() && F->isTemplateInstantiation())
5476         InstantiateFunctionDefinition(Loc, F->getFirstDecl());
5477       MarkFunctionReferenced(Loc, F, MightBeOdrUse);
5478     }
5479   }
5480 }
5481 
5482 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
5483                                               Stmt *AStmt,
5484                                               SourceLocation StartLoc,
5485                                               SourceLocation EndLoc) {
5486   if (!AStmt)
5487     return StmtError();
5488 
5489   auto *CS = cast<CapturedStmt>(AStmt);
5490   // 1.2.2 OpenMP Language Terminology
5491   // Structured block - An executable statement with a single entry at the
5492   // top and a single exit at the bottom.
5493   // The point of exit cannot be a branch out of the structured block.
5494   // longjmp() and throw() must not violate the entry/exit criteria.
5495   CS->getCapturedDecl()->setNothrow();
5496 
5497   setFunctionHasBranchProtectedScope();
5498 
5499   return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5500                                       DSAStack->isCancelRegion());
5501 }
5502 
5503 namespace {
5504 /// Iteration space of a single for loop.
5505 struct LoopIterationSpace final {
5506   /// True if the condition operator is the strict compare operator (<, > or
5507   /// !=).
5508   bool IsStrictCompare = false;
5509   /// Condition of the loop.
5510   Expr *PreCond = nullptr;
5511   /// This expression calculates the number of iterations in the loop.
5512   /// It is always possible to calculate it before starting the loop.
5513   Expr *NumIterations = nullptr;
5514   /// The loop counter variable.
5515   Expr *CounterVar = nullptr;
5516   /// Private loop counter variable.
5517   Expr *PrivateCounterVar = nullptr;
5518   /// This is initializer for the initial value of #CounterVar.
5519   Expr *CounterInit = nullptr;
5520   /// This is step for the #CounterVar used to generate its update:
5521   /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
5522   Expr *CounterStep = nullptr;
5523   /// Should step be subtracted?
5524   bool Subtract = false;
5525   /// Source range of the loop init.
5526   SourceRange InitSrcRange;
5527   /// Source range of the loop condition.
5528   SourceRange CondSrcRange;
5529   /// Source range of the loop increment.
5530   SourceRange IncSrcRange;
5531   /// Minimum value that can have the loop control variable. Used to support
5532   /// non-rectangular loops. Applied only for LCV with the non-iterator types,
5533   /// since only such variables can be used in non-loop invariant expressions.
5534   Expr *MinValue = nullptr;
5535   /// Maximum value that can have the loop control variable. Used to support
5536   /// non-rectangular loops. Applied only for LCV with the non-iterator type,
5537   /// since only such variables can be used in non-loop invariant expressions.
5538   Expr *MaxValue = nullptr;
5539   /// true, if the lower bound depends on the outer loop control var.
5540   bool IsNonRectangularLB = false;
5541   /// true, if the upper bound depends on the outer loop control var.
5542   bool IsNonRectangularUB = false;
5543   /// Index of the loop this loop depends on and forms non-rectangular loop
5544   /// nest.
5545   unsigned LoopDependentIdx = 0;
5546   /// Final condition for the non-rectangular loop nest support. It is used to
5547   /// check that the number of iterations for this particular counter must be
5548   /// finished.
5549   Expr *FinalCondition = nullptr;
5550 };
5551 
5552 /// Helper class for checking canonical form of the OpenMP loops and
5553 /// extracting iteration space of each loop in the loop nest, that will be used
5554 /// for IR generation.
5555 class OpenMPIterationSpaceChecker {
5556   /// Reference to Sema.
5557   Sema &SemaRef;
5558   /// Data-sharing stack.
5559   DSAStackTy &Stack;
5560   /// A location for diagnostics (when there is no some better location).
5561   SourceLocation DefaultLoc;
5562   /// A location for diagnostics (when increment is not compatible).
5563   SourceLocation ConditionLoc;
5564   /// A source location for referring to loop init later.
5565   SourceRange InitSrcRange;
5566   /// A source location for referring to condition later.
5567   SourceRange ConditionSrcRange;
5568   /// A source location for referring to increment later.
5569   SourceRange IncrementSrcRange;
5570   /// Loop variable.
5571   ValueDecl *LCDecl = nullptr;
5572   /// Reference to loop variable.
5573   Expr *LCRef = nullptr;
5574   /// Lower bound (initializer for the var).
5575   Expr *LB = nullptr;
5576   /// Upper bound.
5577   Expr *UB = nullptr;
5578   /// Loop step (increment).
5579   Expr *Step = nullptr;
5580   /// This flag is true when condition is one of:
5581   ///   Var <  UB
5582   ///   Var <= UB
5583   ///   UB  >  Var
5584   ///   UB  >= Var
5585   /// This will have no value when the condition is !=
5586   llvm::Optional<bool> TestIsLessOp;
5587   /// This flag is true when condition is strict ( < or > ).
5588   bool TestIsStrictOp = false;
5589   /// This flag is true when step is subtracted on each iteration.
5590   bool SubtractStep = false;
5591   /// The outer loop counter this loop depends on (if any).
5592   const ValueDecl *DepDecl = nullptr;
5593   /// Contains number of loop (starts from 1) on which loop counter init
5594   /// expression of this loop depends on.
5595   Optional<unsigned> InitDependOnLC;
5596   /// Contains number of loop (starts from 1) on which loop counter condition
5597   /// expression of this loop depends on.
5598   Optional<unsigned> CondDependOnLC;
5599   /// Checks if the provide statement depends on the loop counter.
5600   Optional<unsigned> doesDependOnLoopCounter(const Stmt *S, bool IsInitializer);
5601   /// Original condition required for checking of the exit condition for
5602   /// non-rectangular loop.
5603   Expr *Condition = nullptr;
5604 
5605 public:
5606   OpenMPIterationSpaceChecker(Sema &SemaRef, DSAStackTy &Stack,
5607                               SourceLocation DefaultLoc)
5608       : SemaRef(SemaRef), Stack(Stack), DefaultLoc(DefaultLoc),
5609         ConditionLoc(DefaultLoc) {}
5610   /// Check init-expr for canonical loop form and save loop counter
5611   /// variable - #Var and its initialization value - #LB.
5612   bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
5613   /// Check test-expr for canonical form, save upper-bound (#UB), flags
5614   /// for less/greater and for strict/non-strict comparison.
5615   bool checkAndSetCond(Expr *S);
5616   /// Check incr-expr for canonical loop form and return true if it
5617   /// does not conform, otherwise save loop step (#Step).
5618   bool checkAndSetInc(Expr *S);
5619   /// Return the loop counter variable.
5620   ValueDecl *getLoopDecl() const { return LCDecl; }
5621   /// Return the reference expression to loop counter variable.
5622   Expr *getLoopDeclRefExpr() const { return LCRef; }
5623   /// Source range of the loop init.
5624   SourceRange getInitSrcRange() const { return InitSrcRange; }
5625   /// Source range of the loop condition.
5626   SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
5627   /// Source range of the loop increment.
5628   SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
5629   /// True if the step should be subtracted.
5630   bool shouldSubtractStep() const { return SubtractStep; }
5631   /// True, if the compare operator is strict (<, > or !=).
5632   bool isStrictTestOp() const { return TestIsStrictOp; }
5633   /// Build the expression to calculate the number of iterations.
5634   Expr *buildNumIterations(
5635       Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType,
5636       llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
5637   /// Build the precondition expression for the loops.
5638   Expr *
5639   buildPreCond(Scope *S, Expr *Cond,
5640                llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
5641   /// Build reference expression to the counter be used for codegen.
5642   DeclRefExpr *
5643   buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
5644                   DSAStackTy &DSA) const;
5645   /// Build reference expression to the private counter be used for
5646   /// codegen.
5647   Expr *buildPrivateCounterVar() const;
5648   /// Build initialization of the counter be used for codegen.
5649   Expr *buildCounterInit() const;
5650   /// Build step of the counter be used for codegen.
5651   Expr *buildCounterStep() const;
5652   /// Build loop data with counter value for depend clauses in ordered
5653   /// directives.
5654   Expr *
5655   buildOrderedLoopData(Scope *S, Expr *Counter,
5656                        llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
5657                        SourceLocation Loc, Expr *Inc = nullptr,
5658                        OverloadedOperatorKind OOK = OO_Amp);
5659   /// Builds the minimum value for the loop counter.
5660   std::pair<Expr *, Expr *> buildMinMaxValues(
5661       Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
5662   /// Builds final condition for the non-rectangular loops.
5663   Expr *buildFinalCondition(Scope *S) const;
5664   /// Return true if any expression is dependent.
5665   bool dependent() const;
5666   /// Returns true if the initializer forms non-rectangular loop.
5667   bool doesInitDependOnLC() const { return InitDependOnLC.hasValue(); }
5668   /// Returns true if the condition forms non-rectangular loop.
5669   bool doesCondDependOnLC() const { return CondDependOnLC.hasValue(); }
5670   /// Returns index of the loop we depend on (starting from 1), or 0 otherwise.
5671   unsigned getLoopDependentIdx() const {
5672     return InitDependOnLC.getValueOr(CondDependOnLC.getValueOr(0));
5673   }
5674 
5675 private:
5676   /// Check the right-hand side of an assignment in the increment
5677   /// expression.
5678   bool checkAndSetIncRHS(Expr *RHS);
5679   /// Helper to set loop counter variable and its initializer.
5680   bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB,
5681                       bool EmitDiags);
5682   /// Helper to set upper bound.
5683   bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp,
5684              SourceRange SR, SourceLocation SL);
5685   /// Helper to set loop increment.
5686   bool setStep(Expr *NewStep, bool Subtract);
5687 };
5688 
5689 bool OpenMPIterationSpaceChecker::dependent() const {
5690   if (!LCDecl) {
5691     assert(!LB && !UB && !Step);
5692     return false;
5693   }
5694   return LCDecl->getType()->isDependentType() ||
5695          (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
5696          (Step && Step->isValueDependent());
5697 }
5698 
5699 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
5700                                                  Expr *NewLCRefExpr,
5701                                                  Expr *NewLB, bool EmitDiags) {
5702   // State consistency checking to ensure correct usage.
5703   assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
5704          UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
5705   if (!NewLCDecl || !NewLB)
5706     return true;
5707   LCDecl = getCanonicalDecl(NewLCDecl);
5708   LCRef = NewLCRefExpr;
5709   if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
5710     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
5711       if ((Ctor->isCopyOrMoveConstructor() ||
5712            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
5713           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
5714         NewLB = CE->getArg(0)->IgnoreParenImpCasts();
5715   LB = NewLB;
5716   if (EmitDiags)
5717     InitDependOnLC = doesDependOnLoopCounter(LB, /*IsInitializer=*/true);
5718   return false;
5719 }
5720 
5721 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB,
5722                                         llvm::Optional<bool> LessOp,
5723                                         bool StrictOp, SourceRange SR,
5724                                         SourceLocation SL) {
5725   // State consistency checking to ensure correct usage.
5726   assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
5727          Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
5728   if (!NewUB)
5729     return true;
5730   UB = NewUB;
5731   if (LessOp)
5732     TestIsLessOp = LessOp;
5733   TestIsStrictOp = StrictOp;
5734   ConditionSrcRange = SR;
5735   ConditionLoc = SL;
5736   CondDependOnLC = doesDependOnLoopCounter(UB, /*IsInitializer=*/false);
5737   return false;
5738 }
5739 
5740 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
5741   // State consistency checking to ensure correct usage.
5742   assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
5743   if (!NewStep)
5744     return true;
5745   if (!NewStep->isValueDependent()) {
5746     // Check that the step is integer expression.
5747     SourceLocation StepLoc = NewStep->getBeginLoc();
5748     ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion(
5749         StepLoc, getExprAsWritten(NewStep));
5750     if (Val.isInvalid())
5751       return true;
5752     NewStep = Val.get();
5753 
5754     // OpenMP [2.6, Canonical Loop Form, Restrictions]
5755     //  If test-expr is of form var relational-op b and relational-op is < or
5756     //  <= then incr-expr must cause var to increase on each iteration of the
5757     //  loop. If test-expr is of form var relational-op b and relational-op is
5758     //  > or >= then incr-expr must cause var to decrease on each iteration of
5759     //  the loop.
5760     //  If test-expr is of form b relational-op var and relational-op is < or
5761     //  <= then incr-expr must cause var to decrease on each iteration of the
5762     //  loop. If test-expr is of form b relational-op var and relational-op is
5763     //  > or >= then incr-expr must cause var to increase on each iteration of
5764     //  the loop.
5765     llvm::APSInt Result;
5766     bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
5767     bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
5768     bool IsConstNeg =
5769         IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
5770     bool IsConstPos =
5771         IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
5772     bool IsConstZero = IsConstant && !Result.getBoolValue();
5773 
5774     // != with increment is treated as <; != with decrement is treated as >
5775     if (!TestIsLessOp.hasValue())
5776       TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract);
5777     if (UB && (IsConstZero ||
5778                (TestIsLessOp.getValue() ?
5779                   (IsConstNeg || (IsUnsigned && Subtract)) :
5780                   (IsConstPos || (IsUnsigned && !Subtract))))) {
5781       SemaRef.Diag(NewStep->getExprLoc(),
5782                    diag::err_omp_loop_incr_not_compatible)
5783           << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange();
5784       SemaRef.Diag(ConditionLoc,
5785                    diag::note_omp_loop_cond_requres_compatible_incr)
5786           << TestIsLessOp.getValue() << ConditionSrcRange;
5787       return true;
5788     }
5789     if (TestIsLessOp.getValue() == Subtract) {
5790       NewStep =
5791           SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
5792               .get();
5793       Subtract = !Subtract;
5794     }
5795   }
5796 
5797   Step = NewStep;
5798   SubtractStep = Subtract;
5799   return false;
5800 }
5801 
5802 namespace {
5803 /// Checker for the non-rectangular loops. Checks if the initializer or
5804 /// condition expression references loop counter variable.
5805 class LoopCounterRefChecker final
5806     : public ConstStmtVisitor<LoopCounterRefChecker, bool> {
5807   Sema &SemaRef;
5808   DSAStackTy &Stack;
5809   const ValueDecl *CurLCDecl = nullptr;
5810   const ValueDecl *DepDecl = nullptr;
5811   const ValueDecl *PrevDepDecl = nullptr;
5812   bool IsInitializer = true;
5813   unsigned BaseLoopId = 0;
5814   bool checkDecl(const Expr *E, const ValueDecl *VD) {
5815     if (getCanonicalDecl(VD) == getCanonicalDecl(CurLCDecl)) {
5816       SemaRef.Diag(E->getExprLoc(), diag::err_omp_stmt_depends_on_loop_counter)
5817           << (IsInitializer ? 0 : 1);
5818       return false;
5819     }
5820     const auto &&Data = Stack.isLoopControlVariable(VD);
5821     // OpenMP, 2.9.1 Canonical Loop Form, Restrictions.
5822     // The type of the loop iterator on which we depend may not have a random
5823     // access iterator type.
5824     if (Data.first && VD->getType()->isRecordType()) {
5825       SmallString<128> Name;
5826       llvm::raw_svector_ostream OS(Name);
5827       VD->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(),
5828                                /*Qualified=*/true);
5829       SemaRef.Diag(E->getExprLoc(),
5830                    diag::err_omp_wrong_dependency_iterator_type)
5831           << OS.str();
5832       SemaRef.Diag(VD->getLocation(), diag::note_previous_decl) << VD;
5833       return false;
5834     }
5835     if (Data.first &&
5836         (DepDecl || (PrevDepDecl &&
5837                      getCanonicalDecl(VD) != getCanonicalDecl(PrevDepDecl)))) {
5838       if (!DepDecl && PrevDepDecl)
5839         DepDecl = PrevDepDecl;
5840       SmallString<128> Name;
5841       llvm::raw_svector_ostream OS(Name);
5842       DepDecl->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(),
5843                                     /*Qualified=*/true);
5844       SemaRef.Diag(E->getExprLoc(),
5845                    diag::err_omp_invariant_or_linear_dependency)
5846           << OS.str();
5847       return false;
5848     }
5849     if (Data.first) {
5850       DepDecl = VD;
5851       BaseLoopId = Data.first;
5852     }
5853     return Data.first;
5854   }
5855 
5856 public:
5857   bool VisitDeclRefExpr(const DeclRefExpr *E) {
5858     const ValueDecl *VD = E->getDecl();
5859     if (isa<VarDecl>(VD))
5860       return checkDecl(E, VD);
5861     return false;
5862   }
5863   bool VisitMemberExpr(const MemberExpr *E) {
5864     if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
5865       const ValueDecl *VD = E->getMemberDecl();
5866       if (isa<VarDecl>(VD) || isa<FieldDecl>(VD))
5867         return checkDecl(E, VD);
5868     }
5869     return false;
5870   }
5871   bool VisitStmt(const Stmt *S) {
5872     bool Res = false;
5873     for (const Stmt *Child : S->children())
5874       Res = (Child && Visit(Child)) || Res;
5875     return Res;
5876   }
5877   explicit LoopCounterRefChecker(Sema &SemaRef, DSAStackTy &Stack,
5878                                  const ValueDecl *CurLCDecl, bool IsInitializer,
5879                                  const ValueDecl *PrevDepDecl = nullptr)
5880       : SemaRef(SemaRef), Stack(Stack), CurLCDecl(CurLCDecl),
5881         PrevDepDecl(PrevDepDecl), IsInitializer(IsInitializer) {}
5882   unsigned getBaseLoopId() const {
5883     assert(CurLCDecl && "Expected loop dependency.");
5884     return BaseLoopId;
5885   }
5886   const ValueDecl *getDepDecl() const {
5887     assert(CurLCDecl && "Expected loop dependency.");
5888     return DepDecl;
5889   }
5890 };
5891 } // namespace
5892 
5893 Optional<unsigned>
5894 OpenMPIterationSpaceChecker::doesDependOnLoopCounter(const Stmt *S,
5895                                                      bool IsInitializer) {
5896   // Check for the non-rectangular loops.
5897   LoopCounterRefChecker LoopStmtChecker(SemaRef, Stack, LCDecl, IsInitializer,
5898                                         DepDecl);
5899   if (LoopStmtChecker.Visit(S)) {
5900     DepDecl = LoopStmtChecker.getDepDecl();
5901     return LoopStmtChecker.getBaseLoopId();
5902   }
5903   return llvm::None;
5904 }
5905 
5906 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
5907   // Check init-expr for canonical loop form and save loop counter
5908   // variable - #Var and its initialization value - #LB.
5909   // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
5910   //   var = lb
5911   //   integer-type var = lb
5912   //   random-access-iterator-type var = lb
5913   //   pointer-type var = lb
5914   //
5915   if (!S) {
5916     if (EmitDiags) {
5917       SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
5918     }
5919     return true;
5920   }
5921   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
5922     if (!ExprTemp->cleanupsHaveSideEffects())
5923       S = ExprTemp->getSubExpr();
5924 
5925   InitSrcRange = S->getSourceRange();
5926   if (Expr *E = dyn_cast<Expr>(S))
5927     S = E->IgnoreParens();
5928   if (auto *BO = dyn_cast<BinaryOperator>(S)) {
5929     if (BO->getOpcode() == BO_Assign) {
5930       Expr *LHS = BO->getLHS()->IgnoreParens();
5931       if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
5932         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
5933           if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
5934             return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
5935                                   EmitDiags);
5936         return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS(), EmitDiags);
5937       }
5938       if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
5939         if (ME->isArrow() &&
5940             isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
5941           return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
5942                                 EmitDiags);
5943       }
5944     }
5945   } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
5946     if (DS->isSingleDecl()) {
5947       if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
5948         if (Var->hasInit() && !Var->getType()->isReferenceType()) {
5949           // Accept non-canonical init form here but emit ext. warning.
5950           if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
5951             SemaRef.Diag(S->getBeginLoc(),
5952                          diag::ext_omp_loop_not_canonical_init)
5953                 << S->getSourceRange();
5954           return setLCDeclAndLB(
5955               Var,
5956               buildDeclRefExpr(SemaRef, Var,
5957                                Var->getType().getNonReferenceType(),
5958                                DS->getBeginLoc()),
5959               Var->getInit(), EmitDiags);
5960         }
5961       }
5962     }
5963   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
5964     if (CE->getOperator() == OO_Equal) {
5965       Expr *LHS = CE->getArg(0);
5966       if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
5967         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
5968           if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
5969             return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
5970                                   EmitDiags);
5971         return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1), EmitDiags);
5972       }
5973       if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
5974         if (ME->isArrow() &&
5975             isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
5976           return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
5977                                 EmitDiags);
5978       }
5979     }
5980   }
5981 
5982   if (dependent() || SemaRef.CurContext->isDependentContext())
5983     return false;
5984   if (EmitDiags) {
5985     SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init)
5986         << S->getSourceRange();
5987   }
5988   return true;
5989 }
5990 
5991 /// Ignore parenthesizes, implicit casts, copy constructor and return the
5992 /// variable (which may be the loop variable) if possible.
5993 static const ValueDecl *getInitLCDecl(const Expr *E) {
5994   if (!E)
5995     return nullptr;
5996   E = getExprAsWritten(E);
5997   if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
5998     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
5999       if ((Ctor->isCopyOrMoveConstructor() ||
6000            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
6001           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
6002         E = CE->getArg(0)->IgnoreParenImpCasts();
6003   if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
6004     if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
6005       return getCanonicalDecl(VD);
6006   }
6007   if (const auto *ME = dyn_cast_or_null<MemberExpr>(E))
6008     if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
6009       return getCanonicalDecl(ME->getMemberDecl());
6010   return nullptr;
6011 }
6012 
6013 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
6014   // Check test-expr for canonical form, save upper-bound UB, flags for
6015   // less/greater and for strict/non-strict comparison.
6016   // OpenMP [2.9] Canonical loop form. Test-expr may be one of the following:
6017   //   var relational-op b
6018   //   b relational-op var
6019   //
6020   bool IneqCondIsCanonical = SemaRef.getLangOpts().OpenMP >= 50;
6021   if (!S) {
6022     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond)
6023         << (IneqCondIsCanonical ? 1 : 0) << LCDecl;
6024     return true;
6025   }
6026   Condition = S;
6027   S = getExprAsWritten(S);
6028   SourceLocation CondLoc = S->getBeginLoc();
6029   if (auto *BO = dyn_cast<BinaryOperator>(S)) {
6030     if (BO->isRelationalOp()) {
6031       if (getInitLCDecl(BO->getLHS()) == LCDecl)
6032         return setUB(BO->getRHS(),
6033                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
6034                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
6035                      BO->getSourceRange(), BO->getOperatorLoc());
6036       if (getInitLCDecl(BO->getRHS()) == LCDecl)
6037         return setUB(BO->getLHS(),
6038                      (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
6039                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
6040                      BO->getSourceRange(), BO->getOperatorLoc());
6041     } else if (IneqCondIsCanonical && BO->getOpcode() == BO_NE)
6042       return setUB(
6043           getInitLCDecl(BO->getLHS()) == LCDecl ? BO->getRHS() : BO->getLHS(),
6044           /*LessOp=*/llvm::None,
6045           /*StrictOp=*/true, BO->getSourceRange(), BO->getOperatorLoc());
6046   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
6047     if (CE->getNumArgs() == 2) {
6048       auto Op = CE->getOperator();
6049       switch (Op) {
6050       case OO_Greater:
6051       case OO_GreaterEqual:
6052       case OO_Less:
6053       case OO_LessEqual:
6054         if (getInitLCDecl(CE->getArg(0)) == LCDecl)
6055           return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
6056                        Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
6057                        CE->getOperatorLoc());
6058         if (getInitLCDecl(CE->getArg(1)) == LCDecl)
6059           return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
6060                        Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
6061                        CE->getOperatorLoc());
6062         break;
6063       case OO_ExclaimEqual:
6064         if (IneqCondIsCanonical)
6065           return setUB(getInitLCDecl(CE->getArg(0)) == LCDecl ? CE->getArg(1)
6066                                                               : CE->getArg(0),
6067                        /*LessOp=*/llvm::None,
6068                        /*StrictOp=*/true, CE->getSourceRange(),
6069                        CE->getOperatorLoc());
6070         break;
6071       default:
6072         break;
6073       }
6074     }
6075   }
6076   if (dependent() || SemaRef.CurContext->isDependentContext())
6077     return false;
6078   SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
6079       << (IneqCondIsCanonical ? 1 : 0) << S->getSourceRange() << LCDecl;
6080   return true;
6081 }
6082 
6083 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
6084   // RHS of canonical loop form increment can be:
6085   //   var + incr
6086   //   incr + var
6087   //   var - incr
6088   //
6089   RHS = RHS->IgnoreParenImpCasts();
6090   if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
6091     if (BO->isAdditiveOp()) {
6092       bool IsAdd = BO->getOpcode() == BO_Add;
6093       if (getInitLCDecl(BO->getLHS()) == LCDecl)
6094         return setStep(BO->getRHS(), !IsAdd);
6095       if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl)
6096         return setStep(BO->getLHS(), /*Subtract=*/false);
6097     }
6098   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
6099     bool IsAdd = CE->getOperator() == OO_Plus;
6100     if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
6101       if (getInitLCDecl(CE->getArg(0)) == LCDecl)
6102         return setStep(CE->getArg(1), !IsAdd);
6103       if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl)
6104         return setStep(CE->getArg(0), /*Subtract=*/false);
6105     }
6106   }
6107   if (dependent() || SemaRef.CurContext->isDependentContext())
6108     return false;
6109   SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
6110       << RHS->getSourceRange() << LCDecl;
6111   return true;
6112 }
6113 
6114 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
6115   // Check incr-expr for canonical loop form and return true if it
6116   // does not conform.
6117   // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
6118   //   ++var
6119   //   var++
6120   //   --var
6121   //   var--
6122   //   var += incr
6123   //   var -= incr
6124   //   var = var + incr
6125   //   var = incr + var
6126   //   var = var - incr
6127   //
6128   if (!S) {
6129     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
6130     return true;
6131   }
6132   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
6133     if (!ExprTemp->cleanupsHaveSideEffects())
6134       S = ExprTemp->getSubExpr();
6135 
6136   IncrementSrcRange = S->getSourceRange();
6137   S = S->IgnoreParens();
6138   if (auto *UO = dyn_cast<UnaryOperator>(S)) {
6139     if (UO->isIncrementDecrementOp() &&
6140         getInitLCDecl(UO->getSubExpr()) == LCDecl)
6141       return setStep(SemaRef
6142                          .ActOnIntegerConstant(UO->getBeginLoc(),
6143                                                (UO->isDecrementOp() ? -1 : 1))
6144                          .get(),
6145                      /*Subtract=*/false);
6146   } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
6147     switch (BO->getOpcode()) {
6148     case BO_AddAssign:
6149     case BO_SubAssign:
6150       if (getInitLCDecl(BO->getLHS()) == LCDecl)
6151         return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
6152       break;
6153     case BO_Assign:
6154       if (getInitLCDecl(BO->getLHS()) == LCDecl)
6155         return checkAndSetIncRHS(BO->getRHS());
6156       break;
6157     default:
6158       break;
6159     }
6160   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
6161     switch (CE->getOperator()) {
6162     case OO_PlusPlus:
6163     case OO_MinusMinus:
6164       if (getInitLCDecl(CE->getArg(0)) == LCDecl)
6165         return setStep(SemaRef
6166                            .ActOnIntegerConstant(
6167                                CE->getBeginLoc(),
6168                                ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
6169                            .get(),
6170                        /*Subtract=*/false);
6171       break;
6172     case OO_PlusEqual:
6173     case OO_MinusEqual:
6174       if (getInitLCDecl(CE->getArg(0)) == LCDecl)
6175         return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
6176       break;
6177     case OO_Equal:
6178       if (getInitLCDecl(CE->getArg(0)) == LCDecl)
6179         return checkAndSetIncRHS(CE->getArg(1));
6180       break;
6181     default:
6182       break;
6183     }
6184   }
6185   if (dependent() || SemaRef.CurContext->isDependentContext())
6186     return false;
6187   SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
6188       << S->getSourceRange() << LCDecl;
6189   return true;
6190 }
6191 
6192 static ExprResult
6193 tryBuildCapture(Sema &SemaRef, Expr *Capture,
6194                 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
6195   if (SemaRef.CurContext->isDependentContext())
6196     return ExprResult(Capture);
6197   if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
6198     return SemaRef.PerformImplicitConversion(
6199         Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
6200         /*AllowExplicit=*/true);
6201   auto I = Captures.find(Capture);
6202   if (I != Captures.end())
6203     return buildCapture(SemaRef, Capture, I->second);
6204   DeclRefExpr *Ref = nullptr;
6205   ExprResult Res = buildCapture(SemaRef, Capture, Ref);
6206   Captures[Capture] = Ref;
6207   return Res;
6208 }
6209 
6210 /// Build the expression to calculate the number of iterations.
6211 Expr *OpenMPIterationSpaceChecker::buildNumIterations(
6212     Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType,
6213     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
6214   ExprResult Diff;
6215   QualType VarType = LCDecl->getType().getNonReferenceType();
6216   if (VarType->isIntegerType() || VarType->isPointerType() ||
6217       SemaRef.getLangOpts().CPlusPlus) {
6218     Expr *LBVal = LB;
6219     Expr *UBVal = UB;
6220     // LB = TestIsLessOp.getValue() ? min(LB(MinVal), LB(MaxVal)) :
6221     // max(LB(MinVal), LB(MaxVal))
6222     if (InitDependOnLC) {
6223       const LoopIterationSpace &IS =
6224           ResultIterSpaces[ResultIterSpaces.size() - 1 -
6225                            InitDependOnLC.getValueOr(
6226                                CondDependOnLC.getValueOr(0))];
6227       if (!IS.MinValue || !IS.MaxValue)
6228         return nullptr;
6229       // OuterVar = Min
6230       ExprResult MinValue =
6231           SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue);
6232       if (!MinValue.isUsable())
6233         return nullptr;
6234 
6235       ExprResult LBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
6236                                                IS.CounterVar, MinValue.get());
6237       if (!LBMinVal.isUsable())
6238         return nullptr;
6239       // OuterVar = Min, LBVal
6240       LBMinVal =
6241           SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMinVal.get(), LBVal);
6242       if (!LBMinVal.isUsable())
6243         return nullptr;
6244       // (OuterVar = Min, LBVal)
6245       LBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMinVal.get());
6246       if (!LBMinVal.isUsable())
6247         return nullptr;
6248 
6249       // OuterVar = Max
6250       ExprResult MaxValue =
6251           SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue);
6252       if (!MaxValue.isUsable())
6253         return nullptr;
6254 
6255       ExprResult LBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
6256                                                IS.CounterVar, MaxValue.get());
6257       if (!LBMaxVal.isUsable())
6258         return nullptr;
6259       // OuterVar = Max, LBVal
6260       LBMaxVal =
6261           SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMaxVal.get(), LBVal);
6262       if (!LBMaxVal.isUsable())
6263         return nullptr;
6264       // (OuterVar = Max, LBVal)
6265       LBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMaxVal.get());
6266       if (!LBMaxVal.isUsable())
6267         return nullptr;
6268 
6269       Expr *LBMin = tryBuildCapture(SemaRef, LBMinVal.get(), Captures).get();
6270       Expr *LBMax = tryBuildCapture(SemaRef, LBMaxVal.get(), Captures).get();
6271       if (!LBMin || !LBMax)
6272         return nullptr;
6273       // LB(MinVal) < LB(MaxVal)
6274       ExprResult MinLessMaxRes =
6275           SemaRef.BuildBinOp(S, DefaultLoc, BO_LT, LBMin, LBMax);
6276       if (!MinLessMaxRes.isUsable())
6277         return nullptr;
6278       Expr *MinLessMax =
6279           tryBuildCapture(SemaRef, MinLessMaxRes.get(), Captures).get();
6280       if (!MinLessMax)
6281         return nullptr;
6282       if (TestIsLessOp.getValue()) {
6283         // LB(MinVal) < LB(MaxVal) ? LB(MinVal) : LB(MaxVal) - min(LB(MinVal),
6284         // LB(MaxVal))
6285         ExprResult MinLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc,
6286                                                       MinLessMax, LBMin, LBMax);
6287         if (!MinLB.isUsable())
6288           return nullptr;
6289         LBVal = MinLB.get();
6290       } else {
6291         // LB(MinVal) < LB(MaxVal) ? LB(MaxVal) : LB(MinVal) - max(LB(MinVal),
6292         // LB(MaxVal))
6293         ExprResult MaxLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc,
6294                                                       MinLessMax, LBMax, LBMin);
6295         if (!MaxLB.isUsable())
6296           return nullptr;
6297         LBVal = MaxLB.get();
6298       }
6299     }
6300     // UB = TestIsLessOp.getValue() ? max(UB(MinVal), UB(MaxVal)) :
6301     // min(UB(MinVal), UB(MaxVal))
6302     if (CondDependOnLC) {
6303       const LoopIterationSpace &IS =
6304           ResultIterSpaces[ResultIterSpaces.size() - 1 -
6305                            InitDependOnLC.getValueOr(
6306                                CondDependOnLC.getValueOr(0))];
6307       if (!IS.MinValue || !IS.MaxValue)
6308         return nullptr;
6309       // OuterVar = Min
6310       ExprResult MinValue =
6311           SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue);
6312       if (!MinValue.isUsable())
6313         return nullptr;
6314 
6315       ExprResult UBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
6316                                                IS.CounterVar, MinValue.get());
6317       if (!UBMinVal.isUsable())
6318         return nullptr;
6319       // OuterVar = Min, UBVal
6320       UBMinVal =
6321           SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMinVal.get(), UBVal);
6322       if (!UBMinVal.isUsable())
6323         return nullptr;
6324       // (OuterVar = Min, UBVal)
6325       UBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMinVal.get());
6326       if (!UBMinVal.isUsable())
6327         return nullptr;
6328 
6329       // OuterVar = Max
6330       ExprResult MaxValue =
6331           SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue);
6332       if (!MaxValue.isUsable())
6333         return nullptr;
6334 
6335       ExprResult UBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
6336                                                IS.CounterVar, MaxValue.get());
6337       if (!UBMaxVal.isUsable())
6338         return nullptr;
6339       // OuterVar = Max, UBVal
6340       UBMaxVal =
6341           SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMaxVal.get(), UBVal);
6342       if (!UBMaxVal.isUsable())
6343         return nullptr;
6344       // (OuterVar = Max, UBVal)
6345       UBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMaxVal.get());
6346       if (!UBMaxVal.isUsable())
6347         return nullptr;
6348 
6349       Expr *UBMin = tryBuildCapture(SemaRef, UBMinVal.get(), Captures).get();
6350       Expr *UBMax = tryBuildCapture(SemaRef, UBMaxVal.get(), Captures).get();
6351       if (!UBMin || !UBMax)
6352         return nullptr;
6353       // UB(MinVal) > UB(MaxVal)
6354       ExprResult MinGreaterMaxRes =
6355           SemaRef.BuildBinOp(S, DefaultLoc, BO_GT, UBMin, UBMax);
6356       if (!MinGreaterMaxRes.isUsable())
6357         return nullptr;
6358       Expr *MinGreaterMax =
6359           tryBuildCapture(SemaRef, MinGreaterMaxRes.get(), Captures).get();
6360       if (!MinGreaterMax)
6361         return nullptr;
6362       if (TestIsLessOp.getValue()) {
6363         // UB(MinVal) > UB(MaxVal) ? UB(MinVal) : UB(MaxVal) - max(UB(MinVal),
6364         // UB(MaxVal))
6365         ExprResult MaxUB = SemaRef.ActOnConditionalOp(
6366             DefaultLoc, DefaultLoc, MinGreaterMax, UBMin, UBMax);
6367         if (!MaxUB.isUsable())
6368           return nullptr;
6369         UBVal = MaxUB.get();
6370       } else {
6371         // UB(MinVal) > UB(MaxVal) ? UB(MaxVal) : UB(MinVal) - min(UB(MinVal),
6372         // UB(MaxVal))
6373         ExprResult MinUB = SemaRef.ActOnConditionalOp(
6374             DefaultLoc, DefaultLoc, MinGreaterMax, UBMax, UBMin);
6375         if (!MinUB.isUsable())
6376           return nullptr;
6377         UBVal = MinUB.get();
6378       }
6379     }
6380     // Upper - Lower
6381     Expr *UBExpr = TestIsLessOp.getValue() ? UBVal : LBVal;
6382     Expr *LBExpr = TestIsLessOp.getValue() ? LBVal : UBVal;
6383     Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
6384     Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
6385     if (!Upper || !Lower)
6386       return nullptr;
6387 
6388     Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
6389 
6390     if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
6391       // BuildBinOp already emitted error, this one is to point user to upper
6392       // and lower bound, and to tell what is passed to 'operator-'.
6393       SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
6394           << Upper->getSourceRange() << Lower->getSourceRange();
6395       return nullptr;
6396     }
6397   }
6398 
6399   if (!Diff.isUsable())
6400     return nullptr;
6401 
6402   // Upper - Lower [- 1]
6403   if (TestIsStrictOp)
6404     Diff = SemaRef.BuildBinOp(
6405         S, DefaultLoc, BO_Sub, Diff.get(),
6406         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
6407   if (!Diff.isUsable())
6408     return nullptr;
6409 
6410   // Upper - Lower [- 1] + Step
6411   ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
6412   if (!NewStep.isUsable())
6413     return nullptr;
6414   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
6415   if (!Diff.isUsable())
6416     return nullptr;
6417 
6418   // Parentheses (for dumping/debugging purposes only).
6419   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
6420   if (!Diff.isUsable())
6421     return nullptr;
6422 
6423   // (Upper - Lower [- 1] + Step) / Step
6424   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
6425   if (!Diff.isUsable())
6426     return nullptr;
6427 
6428   // OpenMP runtime requires 32-bit or 64-bit loop variables.
6429   QualType Type = Diff.get()->getType();
6430   ASTContext &C = SemaRef.Context;
6431   bool UseVarType = VarType->hasIntegerRepresentation() &&
6432                     C.getTypeSize(Type) > C.getTypeSize(VarType);
6433   if (!Type->isIntegerType() || UseVarType) {
6434     unsigned NewSize =
6435         UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
6436     bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
6437                                : Type->hasSignedIntegerRepresentation();
6438     Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
6439     if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
6440       Diff = SemaRef.PerformImplicitConversion(
6441           Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
6442       if (!Diff.isUsable())
6443         return nullptr;
6444     }
6445   }
6446   if (LimitedType) {
6447     unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
6448     if (NewSize != C.getTypeSize(Type)) {
6449       if (NewSize < C.getTypeSize(Type)) {
6450         assert(NewSize == 64 && "incorrect loop var size");
6451         SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
6452             << InitSrcRange << ConditionSrcRange;
6453       }
6454       QualType NewType = C.getIntTypeForBitwidth(
6455           NewSize, Type->hasSignedIntegerRepresentation() ||
6456                        C.getTypeSize(Type) < NewSize);
6457       if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
6458         Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
6459                                                  Sema::AA_Converting, true);
6460         if (!Diff.isUsable())
6461           return nullptr;
6462       }
6463     }
6464   }
6465 
6466   return Diff.get();
6467 }
6468 
6469 std::pair<Expr *, Expr *> OpenMPIterationSpaceChecker::buildMinMaxValues(
6470     Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
6471   // Do not build for iterators, they cannot be used in non-rectangular loop
6472   // nests.
6473   if (LCDecl->getType()->isRecordType())
6474     return std::make_pair(nullptr, nullptr);
6475   // If we subtract, the min is in the condition, otherwise the min is in the
6476   // init value.
6477   Expr *MinExpr = nullptr;
6478   Expr *MaxExpr = nullptr;
6479   Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB;
6480   Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB;
6481   bool LBNonRect = TestIsLessOp.getValue() ? InitDependOnLC.hasValue()
6482                                            : CondDependOnLC.hasValue();
6483   bool UBNonRect = TestIsLessOp.getValue() ? CondDependOnLC.hasValue()
6484                                            : InitDependOnLC.hasValue();
6485   Expr *Lower =
6486       LBNonRect ? LBExpr : tryBuildCapture(SemaRef, LBExpr, Captures).get();
6487   Expr *Upper =
6488       UBNonRect ? UBExpr : tryBuildCapture(SemaRef, UBExpr, Captures).get();
6489   if (!Upper || !Lower)
6490     return std::make_pair(nullptr, nullptr);
6491 
6492   if (TestIsLessOp.getValue())
6493     MinExpr = Lower;
6494   else
6495     MaxExpr = Upper;
6496 
6497   // Build minimum/maximum value based on number of iterations.
6498   ExprResult Diff;
6499   QualType VarType = LCDecl->getType().getNonReferenceType();
6500 
6501   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
6502   if (!Diff.isUsable())
6503     return std::make_pair(nullptr, nullptr);
6504 
6505   // Upper - Lower [- 1]
6506   if (TestIsStrictOp)
6507     Diff = SemaRef.BuildBinOp(
6508         S, DefaultLoc, BO_Sub, Diff.get(),
6509         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
6510   if (!Diff.isUsable())
6511     return std::make_pair(nullptr, nullptr);
6512 
6513   // Upper - Lower [- 1] + Step
6514   ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
6515   if (!NewStep.isUsable())
6516     return std::make_pair(nullptr, nullptr);
6517 
6518   // Parentheses (for dumping/debugging purposes only).
6519   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
6520   if (!Diff.isUsable())
6521     return std::make_pair(nullptr, nullptr);
6522 
6523   // (Upper - Lower [- 1]) / Step
6524   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
6525   if (!Diff.isUsable())
6526     return std::make_pair(nullptr, nullptr);
6527 
6528   // ((Upper - Lower [- 1]) / Step) * Step
6529   // Parentheses (for dumping/debugging purposes only).
6530   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
6531   if (!Diff.isUsable())
6532     return std::make_pair(nullptr, nullptr);
6533 
6534   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Mul, Diff.get(), NewStep.get());
6535   if (!Diff.isUsable())
6536     return std::make_pair(nullptr, nullptr);
6537 
6538   // Convert to the original type or ptrdiff_t, if original type is pointer.
6539   if (!VarType->isAnyPointerType() &&
6540       !SemaRef.Context.hasSameType(Diff.get()->getType(), VarType)) {
6541     Diff = SemaRef.PerformImplicitConversion(
6542         Diff.get(), VarType, Sema::AA_Converting, /*AllowExplicit=*/true);
6543   } else if (VarType->isAnyPointerType() &&
6544              !SemaRef.Context.hasSameType(
6545                  Diff.get()->getType(),
6546                  SemaRef.Context.getUnsignedPointerDiffType())) {
6547     Diff = SemaRef.PerformImplicitConversion(
6548         Diff.get(), SemaRef.Context.getUnsignedPointerDiffType(),
6549         Sema::AA_Converting, /*AllowExplicit=*/true);
6550   }
6551   if (!Diff.isUsable())
6552     return std::make_pair(nullptr, nullptr);
6553 
6554   // Parentheses (for dumping/debugging purposes only).
6555   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
6556   if (!Diff.isUsable())
6557     return std::make_pair(nullptr, nullptr);
6558 
6559   if (TestIsLessOp.getValue()) {
6560     // MinExpr = Lower;
6561     // MaxExpr = Lower + (((Upper - Lower [- 1]) / Step) * Step)
6562     Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Lower, Diff.get());
6563     if (!Diff.isUsable())
6564       return std::make_pair(nullptr, nullptr);
6565     Diff = SemaRef.ActOnFinishFullExpr(Diff.get(), /*DiscardedValue*/ false);
6566     if (!Diff.isUsable())
6567       return std::make_pair(nullptr, nullptr);
6568     MaxExpr = Diff.get();
6569   } else {
6570     // MaxExpr = Upper;
6571     // MinExpr = Upper - (((Upper - Lower [- 1]) / Step) * Step)
6572     Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Diff.get());
6573     if (!Diff.isUsable())
6574       return std::make_pair(nullptr, nullptr);
6575     Diff = SemaRef.ActOnFinishFullExpr(Diff.get(), /*DiscardedValue*/ false);
6576     if (!Diff.isUsable())
6577       return std::make_pair(nullptr, nullptr);
6578     MinExpr = Diff.get();
6579   }
6580 
6581   return std::make_pair(MinExpr, MaxExpr);
6582 }
6583 
6584 Expr *OpenMPIterationSpaceChecker::buildFinalCondition(Scope *S) const {
6585   if (InitDependOnLC || CondDependOnLC)
6586     return Condition;
6587   return nullptr;
6588 }
6589 
6590 Expr *OpenMPIterationSpaceChecker::buildPreCond(
6591     Scope *S, Expr *Cond,
6592     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
6593   // Do not build a precondition when the condition/initialization is dependent
6594   // to prevent pessimistic early loop exit.
6595   // TODO: this can be improved by calculating min/max values but not sure that
6596   // it will be very effective.
6597   if (CondDependOnLC || InitDependOnLC)
6598     return SemaRef.PerformImplicitConversion(
6599         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(),
6600         SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
6601         /*AllowExplicit=*/true).get();
6602 
6603   // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
6604   Sema::TentativeAnalysisScope Trap(SemaRef);
6605 
6606   ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures);
6607   ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures);
6608   if (!NewLB.isUsable() || !NewUB.isUsable())
6609     return nullptr;
6610 
6611   ExprResult CondExpr =
6612       SemaRef.BuildBinOp(S, DefaultLoc,
6613                          TestIsLessOp.getValue() ?
6614                            (TestIsStrictOp ? BO_LT : BO_LE) :
6615                            (TestIsStrictOp ? BO_GT : BO_GE),
6616                          NewLB.get(), NewUB.get());
6617   if (CondExpr.isUsable()) {
6618     if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
6619                                                 SemaRef.Context.BoolTy))
6620       CondExpr = SemaRef.PerformImplicitConversion(
6621           CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
6622           /*AllowExplicit=*/true);
6623   }
6624 
6625   // Otherwise use original loop condition and evaluate it in runtime.
6626   return CondExpr.isUsable() ? CondExpr.get() : Cond;
6627 }
6628 
6629 /// Build reference expression to the counter be used for codegen.
6630 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
6631     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
6632     DSAStackTy &DSA) const {
6633   auto *VD = dyn_cast<VarDecl>(LCDecl);
6634   if (!VD) {
6635     VD = SemaRef.isOpenMPCapturedDecl(LCDecl);
6636     DeclRefExpr *Ref = buildDeclRefExpr(
6637         SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
6638     const DSAStackTy::DSAVarData Data =
6639         DSA.getTopDSA(LCDecl, /*FromParent=*/false);
6640     // If the loop control decl is explicitly marked as private, do not mark it
6641     // as captured again.
6642     if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
6643       Captures.insert(std::make_pair(LCRef, Ref));
6644     return Ref;
6645   }
6646   return cast<DeclRefExpr>(LCRef);
6647 }
6648 
6649 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
6650   if (LCDecl && !LCDecl->isInvalidDecl()) {
6651     QualType Type = LCDecl->getType().getNonReferenceType();
6652     VarDecl *PrivateVar = buildVarDecl(
6653         SemaRef, DefaultLoc, Type, LCDecl->getName(),
6654         LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
6655         isa<VarDecl>(LCDecl)
6656             ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc)
6657             : nullptr);
6658     if (PrivateVar->isInvalidDecl())
6659       return nullptr;
6660     return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
6661   }
6662   return nullptr;
6663 }
6664 
6665 /// Build initialization of the counter to be used for codegen.
6666 Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
6667 
6668 /// Build step of the counter be used for codegen.
6669 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
6670 
6671 Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
6672     Scope *S, Expr *Counter,
6673     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
6674     Expr *Inc, OverloadedOperatorKind OOK) {
6675   Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get();
6676   if (!Cnt)
6677     return nullptr;
6678   if (Inc) {
6679     assert((OOK == OO_Plus || OOK == OO_Minus) &&
6680            "Expected only + or - operations for depend clauses.");
6681     BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
6682     Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get();
6683     if (!Cnt)
6684       return nullptr;
6685   }
6686   ExprResult Diff;
6687   QualType VarType = LCDecl->getType().getNonReferenceType();
6688   if (VarType->isIntegerType() || VarType->isPointerType() ||
6689       SemaRef.getLangOpts().CPlusPlus) {
6690     // Upper - Lower
6691     Expr *Upper = TestIsLessOp.getValue()
6692                       ? Cnt
6693                       : tryBuildCapture(SemaRef, UB, Captures).get();
6694     Expr *Lower = TestIsLessOp.getValue()
6695                       ? tryBuildCapture(SemaRef, LB, Captures).get()
6696                       : Cnt;
6697     if (!Upper || !Lower)
6698       return nullptr;
6699 
6700     Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
6701 
6702     if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
6703       // BuildBinOp already emitted error, this one is to point user to upper
6704       // and lower bound, and to tell what is passed to 'operator-'.
6705       SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
6706           << Upper->getSourceRange() << Lower->getSourceRange();
6707       return nullptr;
6708     }
6709   }
6710 
6711   if (!Diff.isUsable())
6712     return nullptr;
6713 
6714   // Parentheses (for dumping/debugging purposes only).
6715   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
6716   if (!Diff.isUsable())
6717     return nullptr;
6718 
6719   ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
6720   if (!NewStep.isUsable())
6721     return nullptr;
6722   // (Upper - Lower) / Step
6723   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
6724   if (!Diff.isUsable())
6725     return nullptr;
6726 
6727   return Diff.get();
6728 }
6729 } // namespace
6730 
6731 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
6732   assert(getLangOpts().OpenMP && "OpenMP is not active.");
6733   assert(Init && "Expected loop in canonical form.");
6734   unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
6735   if (AssociatedLoops > 0 &&
6736       isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
6737     DSAStack->loopStart();
6738     OpenMPIterationSpaceChecker ISC(*this, *DSAStack, ForLoc);
6739     if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) {
6740       if (ValueDecl *D = ISC.getLoopDecl()) {
6741         auto *VD = dyn_cast<VarDecl>(D);
6742         DeclRefExpr *PrivateRef = nullptr;
6743         if (!VD) {
6744           if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
6745             VD = Private;
6746           } else {
6747             PrivateRef = buildCapture(*this, D, ISC.getLoopDeclRefExpr(),
6748                                       /*WithInit=*/false);
6749             VD = cast<VarDecl>(PrivateRef->getDecl());
6750           }
6751         }
6752         DSAStack->addLoopControlVariable(D, VD);
6753         const Decl *LD = DSAStack->getPossiblyLoopCunter();
6754         if (LD != D->getCanonicalDecl()) {
6755           DSAStack->resetPossibleLoopCounter();
6756           if (auto *Var = dyn_cast_or_null<VarDecl>(LD))
6757             MarkDeclarationsReferencedInExpr(
6758                 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var),
6759                                  Var->getType().getNonLValueExprType(Context),
6760                                  ForLoc, /*RefersToCapture=*/true));
6761         }
6762         OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
6763         // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables
6764         // Referenced in a Construct, C/C++]. The loop iteration variable in the
6765         // associated for-loop of a simd construct with just one associated
6766         // for-loop may be listed in a linear clause with a constant-linear-step
6767         // that is the increment of the associated for-loop. The loop iteration
6768         // variable(s) in the associated for-loop(s) of a for or parallel for
6769         // construct may be listed in a private or lastprivate clause.
6770         DSAStackTy::DSAVarData DVar =
6771             DSAStack->getTopDSA(D, /*FromParent=*/false);
6772         // If LoopVarRefExpr is nullptr it means the corresponding loop variable
6773         // is declared in the loop and it is predetermined as a private.
6774         Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
6775         OpenMPClauseKind PredeterminedCKind =
6776             isOpenMPSimdDirective(DKind)
6777                 ? (DSAStack->hasMutipleLoops() ? OMPC_lastprivate : OMPC_linear)
6778                 : OMPC_private;
6779         if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
6780               DVar.CKind != PredeterminedCKind && DVar.RefExpr &&
6781               (LangOpts.OpenMP <= 45 || (DVar.CKind != OMPC_lastprivate &&
6782                                          DVar.CKind != OMPC_private))) ||
6783              ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
6784                DKind == OMPD_master_taskloop ||
6785                DKind == OMPD_parallel_master_taskloop ||
6786                isOpenMPDistributeDirective(DKind)) &&
6787               !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
6788               DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
6789             (DVar.CKind != OMPC_private || DVar.RefExpr)) {
6790           Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa)
6791               << getOpenMPClauseName(DVar.CKind)
6792               << getOpenMPDirectiveName(DKind)
6793               << getOpenMPClauseName(PredeterminedCKind);
6794           if (DVar.RefExpr == nullptr)
6795             DVar.CKind = PredeterminedCKind;
6796           reportOriginalDsa(*this, DSAStack, D, DVar,
6797                             /*IsLoopIterVar=*/true);
6798         } else if (LoopDeclRefExpr) {
6799           // Make the loop iteration variable private (for worksharing
6800           // constructs), linear (for simd directives with the only one
6801           // associated loop) or lastprivate (for simd directives with several
6802           // collapsed or ordered loops).
6803           if (DVar.CKind == OMPC_unknown)
6804             DSAStack->addDSA(D, LoopDeclRefExpr, PredeterminedCKind,
6805                              PrivateRef);
6806         }
6807       }
6808     }
6809     DSAStack->setAssociatedLoops(AssociatedLoops - 1);
6810   }
6811 }
6812 
6813 /// Called on a for stmt to check and extract its iteration space
6814 /// for further processing (such as collapsing).
6815 static bool checkOpenMPIterationSpace(
6816     OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
6817     unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
6818     unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
6819     Expr *OrderedLoopCountExpr,
6820     Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
6821     llvm::MutableArrayRef<LoopIterationSpace> ResultIterSpaces,
6822     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
6823   // OpenMP [2.9.1, Canonical Loop Form]
6824   //   for (init-expr; test-expr; incr-expr) structured-block
6825   //   for (range-decl: range-expr) structured-block
6826   auto *For = dyn_cast_or_null<ForStmt>(S);
6827   auto *CXXFor = dyn_cast_or_null<CXXForRangeStmt>(S);
6828   // Ranged for is supported only in OpenMP 5.0.
6829   if (!For && (SemaRef.LangOpts.OpenMP <= 45 || !CXXFor)) {
6830     SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for)
6831         << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
6832         << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount
6833         << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
6834     if (TotalNestedLoopCount > 1) {
6835       if (CollapseLoopCountExpr && OrderedLoopCountExpr)
6836         SemaRef.Diag(DSA.getConstructLoc(),
6837                      diag::note_omp_collapse_ordered_expr)
6838             << 2 << CollapseLoopCountExpr->getSourceRange()
6839             << OrderedLoopCountExpr->getSourceRange();
6840       else if (CollapseLoopCountExpr)
6841         SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
6842                      diag::note_omp_collapse_ordered_expr)
6843             << 0 << CollapseLoopCountExpr->getSourceRange();
6844       else
6845         SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
6846                      diag::note_omp_collapse_ordered_expr)
6847             << 1 << OrderedLoopCountExpr->getSourceRange();
6848     }
6849     return true;
6850   }
6851   assert(((For && For->getBody()) || (CXXFor && CXXFor->getBody())) &&
6852          "No loop body.");
6853 
6854   OpenMPIterationSpaceChecker ISC(SemaRef, DSA,
6855                                   For ? For->getForLoc() : CXXFor->getForLoc());
6856 
6857   // Check init.
6858   Stmt *Init = For ? For->getInit() : CXXFor->getBeginStmt();
6859   if (ISC.checkAndSetInit(Init))
6860     return true;
6861 
6862   bool HasErrors = false;
6863 
6864   // Check loop variable's type.
6865   if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
6866     // OpenMP [2.6, Canonical Loop Form]
6867     // Var is one of the following:
6868     //   A variable of signed or unsigned integer type.
6869     //   For C++, a variable of a random access iterator type.
6870     //   For C, a variable of a pointer type.
6871     QualType VarType = LCDecl->getType().getNonReferenceType();
6872     if (!VarType->isDependentType() && !VarType->isIntegerType() &&
6873         !VarType->isPointerType() &&
6874         !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
6875       SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type)
6876           << SemaRef.getLangOpts().CPlusPlus;
6877       HasErrors = true;
6878     }
6879 
6880     // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
6881     // a Construct
6882     // The loop iteration variable(s) in the associated for-loop(s) of a for or
6883     // parallel for construct is (are) private.
6884     // The loop iteration variable in the associated for-loop of a simd
6885     // construct with just one associated for-loop is linear with a
6886     // constant-linear-step that is the increment of the associated for-loop.
6887     // Exclude loop var from the list of variables with implicitly defined data
6888     // sharing attributes.
6889     VarsWithImplicitDSA.erase(LCDecl);
6890 
6891     assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
6892 
6893     // Check test-expr.
6894     HasErrors |= ISC.checkAndSetCond(For ? For->getCond() : CXXFor->getCond());
6895 
6896     // Check incr-expr.
6897     HasErrors |= ISC.checkAndSetInc(For ? For->getInc() : CXXFor->getInc());
6898   }
6899 
6900   if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
6901     return HasErrors;
6902 
6903   // Build the loop's iteration space representation.
6904   ResultIterSpaces[CurrentNestedLoopCount].PreCond = ISC.buildPreCond(
6905       DSA.getCurScope(), For ? For->getCond() : CXXFor->getCond(), Captures);
6906   ResultIterSpaces[CurrentNestedLoopCount].NumIterations =
6907       ISC.buildNumIterations(DSA.getCurScope(), ResultIterSpaces,
6908                              (isOpenMPWorksharingDirective(DKind) ||
6909                               isOpenMPTaskLoopDirective(DKind) ||
6910                               isOpenMPDistributeDirective(DKind)),
6911                              Captures);
6912   ResultIterSpaces[CurrentNestedLoopCount].CounterVar =
6913       ISC.buildCounterVar(Captures, DSA);
6914   ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar =
6915       ISC.buildPrivateCounterVar();
6916   ResultIterSpaces[CurrentNestedLoopCount].CounterInit = ISC.buildCounterInit();
6917   ResultIterSpaces[CurrentNestedLoopCount].CounterStep = ISC.buildCounterStep();
6918   ResultIterSpaces[CurrentNestedLoopCount].InitSrcRange = ISC.getInitSrcRange();
6919   ResultIterSpaces[CurrentNestedLoopCount].CondSrcRange =
6920       ISC.getConditionSrcRange();
6921   ResultIterSpaces[CurrentNestedLoopCount].IncSrcRange =
6922       ISC.getIncrementSrcRange();
6923   ResultIterSpaces[CurrentNestedLoopCount].Subtract = ISC.shouldSubtractStep();
6924   ResultIterSpaces[CurrentNestedLoopCount].IsStrictCompare =
6925       ISC.isStrictTestOp();
6926   std::tie(ResultIterSpaces[CurrentNestedLoopCount].MinValue,
6927            ResultIterSpaces[CurrentNestedLoopCount].MaxValue) =
6928       ISC.buildMinMaxValues(DSA.getCurScope(), Captures);
6929   ResultIterSpaces[CurrentNestedLoopCount].FinalCondition =
6930       ISC.buildFinalCondition(DSA.getCurScope());
6931   ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularLB =
6932       ISC.doesInitDependOnLC();
6933   ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularUB =
6934       ISC.doesCondDependOnLC();
6935   ResultIterSpaces[CurrentNestedLoopCount].LoopDependentIdx =
6936       ISC.getLoopDependentIdx();
6937 
6938   HasErrors |=
6939       (ResultIterSpaces[CurrentNestedLoopCount].PreCond == nullptr ||
6940        ResultIterSpaces[CurrentNestedLoopCount].NumIterations == nullptr ||
6941        ResultIterSpaces[CurrentNestedLoopCount].CounterVar == nullptr ||
6942        ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar == nullptr ||
6943        ResultIterSpaces[CurrentNestedLoopCount].CounterInit == nullptr ||
6944        ResultIterSpaces[CurrentNestedLoopCount].CounterStep == nullptr);
6945   if (!HasErrors && DSA.isOrderedRegion()) {
6946     if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
6947       if (CurrentNestedLoopCount <
6948           DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
6949         DSA.getOrderedRegionParam().second->setLoopNumIterations(
6950             CurrentNestedLoopCount,
6951             ResultIterSpaces[CurrentNestedLoopCount].NumIterations);
6952         DSA.getOrderedRegionParam().second->setLoopCounter(
6953             CurrentNestedLoopCount,
6954             ResultIterSpaces[CurrentNestedLoopCount].CounterVar);
6955       }
6956     }
6957     for (auto &Pair : DSA.getDoacrossDependClauses()) {
6958       if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) {
6959         // Erroneous case - clause has some problems.
6960         continue;
6961       }
6962       if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink &&
6963           Pair.second.size() <= CurrentNestedLoopCount) {
6964         // Erroneous case - clause has some problems.
6965         Pair.first->setLoopData(CurrentNestedLoopCount, nullptr);
6966         continue;
6967       }
6968       Expr *CntValue;
6969       if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
6970         CntValue = ISC.buildOrderedLoopData(
6971             DSA.getCurScope(),
6972             ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures,
6973             Pair.first->getDependencyLoc());
6974       else
6975         CntValue = ISC.buildOrderedLoopData(
6976             DSA.getCurScope(),
6977             ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures,
6978             Pair.first->getDependencyLoc(),
6979             Pair.second[CurrentNestedLoopCount].first,
6980             Pair.second[CurrentNestedLoopCount].second);
6981       Pair.first->setLoopData(CurrentNestedLoopCount, CntValue);
6982     }
6983   }
6984 
6985   return HasErrors;
6986 }
6987 
6988 /// Build 'VarRef = Start.
6989 static ExprResult
6990 buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
6991                  ExprResult Start, bool IsNonRectangularLB,
6992                  llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
6993   // Build 'VarRef = Start.
6994   ExprResult NewStart = IsNonRectangularLB
6995                             ? Start.get()
6996                             : tryBuildCapture(SemaRef, Start.get(), Captures);
6997   if (!NewStart.isUsable())
6998     return ExprError();
6999   if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
7000                                    VarRef.get()->getType())) {
7001     NewStart = SemaRef.PerformImplicitConversion(
7002         NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
7003         /*AllowExplicit=*/true);
7004     if (!NewStart.isUsable())
7005       return ExprError();
7006   }
7007 
7008   ExprResult Init =
7009       SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
7010   return Init;
7011 }
7012 
7013 /// Build 'VarRef = Start + Iter * Step'.
7014 static ExprResult buildCounterUpdate(
7015     Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
7016     ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
7017     bool IsNonRectangularLB,
7018     llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
7019   // Add parentheses (for debugging purposes only).
7020   Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
7021   if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
7022       !Step.isUsable())
7023     return ExprError();
7024 
7025   ExprResult NewStep = Step;
7026   if (Captures)
7027     NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
7028   if (NewStep.isInvalid())
7029     return ExprError();
7030   ExprResult Update =
7031       SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
7032   if (!Update.isUsable())
7033     return ExprError();
7034 
7035   // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
7036   // 'VarRef = Start (+|-) Iter * Step'.
7037   if (!Start.isUsable())
7038     return ExprError();
7039   ExprResult NewStart = SemaRef.ActOnParenExpr(Loc, Loc, Start.get());
7040   if (!NewStart.isUsable())
7041     return ExprError();
7042   if (Captures && !IsNonRectangularLB)
7043     NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
7044   if (NewStart.isInvalid())
7045     return ExprError();
7046 
7047   // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
7048   ExprResult SavedUpdate = Update;
7049   ExprResult UpdateVal;
7050   if (VarRef.get()->getType()->isOverloadableType() ||
7051       NewStart.get()->getType()->isOverloadableType() ||
7052       Update.get()->getType()->isOverloadableType()) {
7053     Sema::TentativeAnalysisScope Trap(SemaRef);
7054 
7055     Update =
7056         SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
7057     if (Update.isUsable()) {
7058       UpdateVal =
7059           SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
7060                              VarRef.get(), SavedUpdate.get());
7061       if (UpdateVal.isUsable()) {
7062         Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
7063                                             UpdateVal.get());
7064       }
7065     }
7066   }
7067 
7068   // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
7069   if (!Update.isUsable() || !UpdateVal.isUsable()) {
7070     Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
7071                                 NewStart.get(), SavedUpdate.get());
7072     if (!Update.isUsable())
7073       return ExprError();
7074 
7075     if (!SemaRef.Context.hasSameType(Update.get()->getType(),
7076                                      VarRef.get()->getType())) {
7077       Update = SemaRef.PerformImplicitConversion(
7078           Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
7079       if (!Update.isUsable())
7080         return ExprError();
7081     }
7082 
7083     Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
7084   }
7085   return Update;
7086 }
7087 
7088 /// Convert integer expression \a E to make it have at least \a Bits
7089 /// bits.
7090 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
7091   if (E == nullptr)
7092     return ExprError();
7093   ASTContext &C = SemaRef.Context;
7094   QualType OldType = E->getType();
7095   unsigned HasBits = C.getTypeSize(OldType);
7096   if (HasBits >= Bits)
7097     return ExprResult(E);
7098   // OK to convert to signed, because new type has more bits than old.
7099   QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
7100   return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
7101                                            true);
7102 }
7103 
7104 /// Check if the given expression \a E is a constant integer that fits
7105 /// into \a Bits bits.
7106 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
7107   if (E == nullptr)
7108     return false;
7109   llvm::APSInt Result;
7110   if (E->isIntegerConstantExpr(Result, SemaRef.Context))
7111     return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
7112   return false;
7113 }
7114 
7115 /// Build preinits statement for the given declarations.
7116 static Stmt *buildPreInits(ASTContext &Context,
7117                            MutableArrayRef<Decl *> PreInits) {
7118   if (!PreInits.empty()) {
7119     return new (Context) DeclStmt(
7120         DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
7121         SourceLocation(), SourceLocation());
7122   }
7123   return nullptr;
7124 }
7125 
7126 /// Build preinits statement for the given declarations.
7127 static Stmt *
7128 buildPreInits(ASTContext &Context,
7129               const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
7130   if (!Captures.empty()) {
7131     SmallVector<Decl *, 16> PreInits;
7132     for (const auto &Pair : Captures)
7133       PreInits.push_back(Pair.second->getDecl());
7134     return buildPreInits(Context, PreInits);
7135   }
7136   return nullptr;
7137 }
7138 
7139 /// Build postupdate expression for the given list of postupdates expressions.
7140 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
7141   Expr *PostUpdate = nullptr;
7142   if (!PostUpdates.empty()) {
7143     for (Expr *E : PostUpdates) {
7144       Expr *ConvE = S.BuildCStyleCastExpr(
7145                          E->getExprLoc(),
7146                          S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
7147                          E->getExprLoc(), E)
7148                         .get();
7149       PostUpdate = PostUpdate
7150                        ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
7151                                               PostUpdate, ConvE)
7152                              .get()
7153                        : ConvE;
7154     }
7155   }
7156   return PostUpdate;
7157 }
7158 
7159 /// Called on a for stmt to check itself and nested loops (if any).
7160 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
7161 /// number of collapsed loops otherwise.
7162 static unsigned
7163 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
7164                 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
7165                 DSAStackTy &DSA,
7166                 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
7167                 OMPLoopDirective::HelperExprs &Built) {
7168   unsigned NestedLoopCount = 1;
7169   if (CollapseLoopCountExpr) {
7170     // Found 'collapse' clause - calculate collapse number.
7171     Expr::EvalResult Result;
7172     if (!CollapseLoopCountExpr->isValueDependent() &&
7173         CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
7174       NestedLoopCount = Result.Val.getInt().getLimitedValue();
7175     } else {
7176       Built.clear(/*Size=*/1);
7177       return 1;
7178     }
7179   }
7180   unsigned OrderedLoopCount = 1;
7181   if (OrderedLoopCountExpr) {
7182     // Found 'ordered' clause - calculate collapse number.
7183     Expr::EvalResult EVResult;
7184     if (!OrderedLoopCountExpr->isValueDependent() &&
7185         OrderedLoopCountExpr->EvaluateAsInt(EVResult,
7186                                             SemaRef.getASTContext())) {
7187       llvm::APSInt Result = EVResult.Val.getInt();
7188       if (Result.getLimitedValue() < NestedLoopCount) {
7189         SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
7190                      diag::err_omp_wrong_ordered_loop_count)
7191             << OrderedLoopCountExpr->getSourceRange();
7192         SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
7193                      diag::note_collapse_loop_count)
7194             << CollapseLoopCountExpr->getSourceRange();
7195       }
7196       OrderedLoopCount = Result.getLimitedValue();
7197     } else {
7198       Built.clear(/*Size=*/1);
7199       return 1;
7200     }
7201   }
7202   // This is helper routine for loop directives (e.g., 'for', 'simd',
7203   // 'for simd', etc.).
7204   llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
7205   SmallVector<LoopIterationSpace, 4> IterSpaces(
7206       std::max(OrderedLoopCount, NestedLoopCount));
7207   Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
7208   for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
7209     if (checkOpenMPIterationSpace(
7210             DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
7211             std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
7212             OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces, Captures))
7213       return 0;
7214     // Move on to the next nested for loop, or to the loop body.
7215     // OpenMP [2.8.1, simd construct, Restrictions]
7216     // All loops associated with the construct must be perfectly nested; that
7217     // is, there must be no intervening code nor any OpenMP directive between
7218     // any two loops.
7219     if (auto *For = dyn_cast<ForStmt>(CurStmt)) {
7220       CurStmt = For->getBody();
7221     } else {
7222       assert(isa<CXXForRangeStmt>(CurStmt) &&
7223              "Expected canonical for or range-based for loops.");
7224       CurStmt = cast<CXXForRangeStmt>(CurStmt)->getBody();
7225     }
7226     CurStmt = OMPLoopDirective::tryToFindNextInnerLoop(
7227         CurStmt, SemaRef.LangOpts.OpenMP >= 50);
7228   }
7229   for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) {
7230     if (checkOpenMPIterationSpace(
7231             DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
7232             std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
7233             OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces, Captures))
7234       return 0;
7235     if (Cnt > 0 && IterSpaces[Cnt].CounterVar) {
7236       // Handle initialization of captured loop iterator variables.
7237       auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar);
7238       if (isa<OMPCapturedExprDecl>(DRE->getDecl())) {
7239         Captures[DRE] = DRE;
7240       }
7241     }
7242     // Move on to the next nested for loop, or to the loop body.
7243     // OpenMP [2.8.1, simd construct, Restrictions]
7244     // All loops associated with the construct must be perfectly nested; that
7245     // is, there must be no intervening code nor any OpenMP directive between
7246     // any two loops.
7247     if (auto *For = dyn_cast<ForStmt>(CurStmt)) {
7248       CurStmt = For->getBody();
7249     } else {
7250       assert(isa<CXXForRangeStmt>(CurStmt) &&
7251              "Expected canonical for or range-based for loops.");
7252       CurStmt = cast<CXXForRangeStmt>(CurStmt)->getBody();
7253     }
7254     CurStmt = OMPLoopDirective::tryToFindNextInnerLoop(
7255         CurStmt, SemaRef.LangOpts.OpenMP >= 50);
7256   }
7257 
7258   Built.clear(/* size */ NestedLoopCount);
7259 
7260   if (SemaRef.CurContext->isDependentContext())
7261     return NestedLoopCount;
7262 
7263   // An example of what is generated for the following code:
7264   //
7265   //   #pragma omp simd collapse(2) ordered(2)
7266   //   for (i = 0; i < NI; ++i)
7267   //     for (k = 0; k < NK; ++k)
7268   //       for (j = J0; j < NJ; j+=2) {
7269   //         <loop body>
7270   //       }
7271   //
7272   // We generate the code below.
7273   // Note: the loop body may be outlined in CodeGen.
7274   // Note: some counters may be C++ classes, operator- is used to find number of
7275   // iterations and operator+= to calculate counter value.
7276   // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
7277   // or i64 is currently supported).
7278   //
7279   //   #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
7280   //   for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
7281   //     .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
7282   //     .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
7283   //     // similar updates for vars in clauses (e.g. 'linear')
7284   //     <loop body (using local i and j)>
7285   //   }
7286   //   i = NI; // assign final values of counters
7287   //   j = NJ;
7288   //
7289 
7290   // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
7291   // the iteration counts of the collapsed for loops.
7292   // Precondition tests if there is at least one iteration (all conditions are
7293   // true).
7294   auto PreCond = ExprResult(IterSpaces[0].PreCond);
7295   Expr *N0 = IterSpaces[0].NumIterations;
7296   ExprResult LastIteration32 =
7297       widenIterationCount(/*Bits=*/32,
7298                           SemaRef
7299                               .PerformImplicitConversion(
7300                                   N0->IgnoreImpCasts(), N0->getType(),
7301                                   Sema::AA_Converting, /*AllowExplicit=*/true)
7302                               .get(),
7303                           SemaRef);
7304   ExprResult LastIteration64 = widenIterationCount(
7305       /*Bits=*/64,
7306       SemaRef
7307           .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(),
7308                                      Sema::AA_Converting,
7309                                      /*AllowExplicit=*/true)
7310           .get(),
7311       SemaRef);
7312 
7313   if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
7314     return NestedLoopCount;
7315 
7316   ASTContext &C = SemaRef.Context;
7317   bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
7318 
7319   Scope *CurScope = DSA.getCurScope();
7320   for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
7321     if (PreCond.isUsable()) {
7322       PreCond =
7323           SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
7324                              PreCond.get(), IterSpaces[Cnt].PreCond);
7325     }
7326     Expr *N = IterSpaces[Cnt].NumIterations;
7327     SourceLocation Loc = N->getExprLoc();
7328     AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
7329     if (LastIteration32.isUsable())
7330       LastIteration32 = SemaRef.BuildBinOp(
7331           CurScope, Loc, BO_Mul, LastIteration32.get(),
7332           SemaRef
7333               .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
7334                                          Sema::AA_Converting,
7335                                          /*AllowExplicit=*/true)
7336               .get());
7337     if (LastIteration64.isUsable())
7338       LastIteration64 = SemaRef.BuildBinOp(
7339           CurScope, Loc, BO_Mul, LastIteration64.get(),
7340           SemaRef
7341               .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
7342                                          Sema::AA_Converting,
7343                                          /*AllowExplicit=*/true)
7344               .get());
7345   }
7346 
7347   // Choose either the 32-bit or 64-bit version.
7348   ExprResult LastIteration = LastIteration64;
7349   if (SemaRef.getLangOpts().OpenMPOptimisticCollapse ||
7350       (LastIteration32.isUsable() &&
7351        C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
7352        (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
7353         fitsInto(
7354             /*Bits=*/32,
7355             LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
7356             LastIteration64.get(), SemaRef))))
7357     LastIteration = LastIteration32;
7358   QualType VType = LastIteration.get()->getType();
7359   QualType RealVType = VType;
7360   QualType StrideVType = VType;
7361   if (isOpenMPTaskLoopDirective(DKind)) {
7362     VType =
7363         SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
7364     StrideVType =
7365         SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
7366   }
7367 
7368   if (!LastIteration.isUsable())
7369     return 0;
7370 
7371   // Save the number of iterations.
7372   ExprResult NumIterations = LastIteration;
7373   {
7374     LastIteration = SemaRef.BuildBinOp(
7375         CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
7376         LastIteration.get(),
7377         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
7378     if (!LastIteration.isUsable())
7379       return 0;
7380   }
7381 
7382   // Calculate the last iteration number beforehand instead of doing this on
7383   // each iteration. Do not do this if the number of iterations may be kfold-ed.
7384   llvm::APSInt Result;
7385   bool IsConstant =
7386       LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
7387   ExprResult CalcLastIteration;
7388   if (!IsConstant) {
7389     ExprResult SaveRef =
7390         tryBuildCapture(SemaRef, LastIteration.get(), Captures);
7391     LastIteration = SaveRef;
7392 
7393     // Prepare SaveRef + 1.
7394     NumIterations = SemaRef.BuildBinOp(
7395         CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
7396         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
7397     if (!NumIterations.isUsable())
7398       return 0;
7399   }
7400 
7401   SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
7402 
7403   // Build variables passed into runtime, necessary for worksharing directives.
7404   ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
7405   if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
7406       isOpenMPDistributeDirective(DKind)) {
7407     // Lower bound variable, initialized with zero.
7408     VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
7409     LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
7410     SemaRef.AddInitializerToDecl(LBDecl,
7411                                  SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
7412                                  /*DirectInit*/ false);
7413 
7414     // Upper bound variable, initialized with last iteration number.
7415     VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
7416     UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
7417     SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
7418                                  /*DirectInit*/ false);
7419 
7420     // A 32-bit variable-flag where runtime returns 1 for the last iteration.
7421     // This will be used to implement clause 'lastprivate'.
7422     QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
7423     VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
7424     IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
7425     SemaRef.AddInitializerToDecl(ILDecl,
7426                                  SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
7427                                  /*DirectInit*/ false);
7428 
7429     // Stride variable returned by runtime (we initialize it to 1 by default).
7430     VarDecl *STDecl =
7431         buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
7432     ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
7433     SemaRef.AddInitializerToDecl(STDecl,
7434                                  SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
7435                                  /*DirectInit*/ false);
7436 
7437     // Build expression: UB = min(UB, LastIteration)
7438     // It is necessary for CodeGen of directives with static scheduling.
7439     ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
7440                                                 UB.get(), LastIteration.get());
7441     ExprResult CondOp = SemaRef.ActOnConditionalOp(
7442         LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(),
7443         LastIteration.get(), UB.get());
7444     EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
7445                              CondOp.get());
7446     EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false);
7447 
7448     // If we have a combined directive that combines 'distribute', 'for' or
7449     // 'simd' we need to be able to access the bounds of the schedule of the
7450     // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
7451     // by scheduling 'distribute' have to be passed to the schedule of 'for'.
7452     if (isOpenMPLoopBoundSharingDirective(DKind)) {
7453       // Lower bound variable, initialized with zero.
7454       VarDecl *CombLBDecl =
7455           buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
7456       CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
7457       SemaRef.AddInitializerToDecl(
7458           CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
7459           /*DirectInit*/ false);
7460 
7461       // Upper bound variable, initialized with last iteration number.
7462       VarDecl *CombUBDecl =
7463           buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
7464       CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
7465       SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
7466                                    /*DirectInit*/ false);
7467 
7468       ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
7469           CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
7470       ExprResult CombCondOp =
7471           SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
7472                                      LastIteration.get(), CombUB.get());
7473       CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
7474                                    CombCondOp.get());
7475       CombEUB =
7476           SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false);
7477 
7478       const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
7479       // We expect to have at least 2 more parameters than the 'parallel'
7480       // directive does - the lower and upper bounds of the previous schedule.
7481       assert(CD->getNumParams() >= 4 &&
7482              "Unexpected number of parameters in loop combined directive");
7483 
7484       // Set the proper type for the bounds given what we learned from the
7485       // enclosed loops.
7486       ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
7487       ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
7488 
7489       // Previous lower and upper bounds are obtained from the region
7490       // parameters.
7491       PrevLB =
7492           buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
7493       PrevUB =
7494           buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
7495     }
7496   }
7497 
7498   // Build the iteration variable and its initialization before loop.
7499   ExprResult IV;
7500   ExprResult Init, CombInit;
7501   {
7502     VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
7503     IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
7504     Expr *RHS =
7505         (isOpenMPWorksharingDirective(DKind) ||
7506          isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
7507             ? LB.get()
7508             : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
7509     Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
7510     Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false);
7511 
7512     if (isOpenMPLoopBoundSharingDirective(DKind)) {
7513       Expr *CombRHS =
7514           (isOpenMPWorksharingDirective(DKind) ||
7515            isOpenMPTaskLoopDirective(DKind) ||
7516            isOpenMPDistributeDirective(DKind))
7517               ? CombLB.get()
7518               : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
7519       CombInit =
7520           SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
7521       CombInit =
7522           SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false);
7523     }
7524   }
7525 
7526   bool UseStrictCompare =
7527       RealVType->hasUnsignedIntegerRepresentation() &&
7528       llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) {
7529         return LIS.IsStrictCompare;
7530       });
7531   // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for
7532   // unsigned IV)) for worksharing loops.
7533   SourceLocation CondLoc = AStmt->getBeginLoc();
7534   Expr *BoundUB = UB.get();
7535   if (UseStrictCompare) {
7536     BoundUB =
7537         SemaRef
7538             .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB,
7539                         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
7540             .get();
7541     BoundUB =
7542         SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get();
7543   }
7544   ExprResult Cond =
7545       (isOpenMPWorksharingDirective(DKind) ||
7546        isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
7547           ? SemaRef.BuildBinOp(CurScope, CondLoc,
7548                                UseStrictCompare ? BO_LT : BO_LE, IV.get(),
7549                                BoundUB)
7550           : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
7551                                NumIterations.get());
7552   ExprResult CombDistCond;
7553   if (isOpenMPLoopBoundSharingDirective(DKind)) {
7554     CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
7555                                       NumIterations.get());
7556   }
7557 
7558   ExprResult CombCond;
7559   if (isOpenMPLoopBoundSharingDirective(DKind)) {
7560     Expr *BoundCombUB = CombUB.get();
7561     if (UseStrictCompare) {
7562       BoundCombUB =
7563           SemaRef
7564               .BuildBinOp(
7565                   CurScope, CondLoc, BO_Add, BoundCombUB,
7566                   SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
7567               .get();
7568       BoundCombUB =
7569           SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false)
7570               .get();
7571     }
7572     CombCond =
7573         SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE,
7574                            IV.get(), BoundCombUB);
7575   }
7576   // Loop increment (IV = IV + 1)
7577   SourceLocation IncLoc = AStmt->getBeginLoc();
7578   ExprResult Inc =
7579       SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
7580                          SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
7581   if (!Inc.isUsable())
7582     return 0;
7583   Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
7584   Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false);
7585   if (!Inc.isUsable())
7586     return 0;
7587 
7588   // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
7589   // Used for directives with static scheduling.
7590   // In combined construct, add combined version that use CombLB and CombUB
7591   // base variables for the update
7592   ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
7593   if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
7594       isOpenMPDistributeDirective(DKind)) {
7595     // LB + ST
7596     NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
7597     if (!NextLB.isUsable())
7598       return 0;
7599     // LB = LB + ST
7600     NextLB =
7601         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
7602     NextLB =
7603         SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false);
7604     if (!NextLB.isUsable())
7605       return 0;
7606     // UB + ST
7607     NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
7608     if (!NextUB.isUsable())
7609       return 0;
7610     // UB = UB + ST
7611     NextUB =
7612         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
7613     NextUB =
7614         SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false);
7615     if (!NextUB.isUsable())
7616       return 0;
7617     if (isOpenMPLoopBoundSharingDirective(DKind)) {
7618       CombNextLB =
7619           SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
7620       if (!NextLB.isUsable())
7621         return 0;
7622       // LB = LB + ST
7623       CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
7624                                       CombNextLB.get());
7625       CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(),
7626                                                /*DiscardedValue*/ false);
7627       if (!CombNextLB.isUsable())
7628         return 0;
7629       // UB + ST
7630       CombNextUB =
7631           SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
7632       if (!CombNextUB.isUsable())
7633         return 0;
7634       // UB = UB + ST
7635       CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
7636                                       CombNextUB.get());
7637       CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(),
7638                                                /*DiscardedValue*/ false);
7639       if (!CombNextUB.isUsable())
7640         return 0;
7641     }
7642   }
7643 
7644   // Create increment expression for distribute loop when combined in a same
7645   // directive with for as IV = IV + ST; ensure upper bound expression based
7646   // on PrevUB instead of NumIterations - used to implement 'for' when found
7647   // in combination with 'distribute', like in 'distribute parallel for'
7648   SourceLocation DistIncLoc = AStmt->getBeginLoc();
7649   ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond;
7650   if (isOpenMPLoopBoundSharingDirective(DKind)) {
7651     DistCond = SemaRef.BuildBinOp(
7652         CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB);
7653     assert(DistCond.isUsable() && "distribute cond expr was not built");
7654 
7655     DistInc =
7656         SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
7657     assert(DistInc.isUsable() && "distribute inc expr was not built");
7658     DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
7659                                  DistInc.get());
7660     DistInc =
7661         SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false);
7662     assert(DistInc.isUsable() && "distribute inc expr was not built");
7663 
7664     // Build expression: UB = min(UB, prevUB) for #for in composite or combined
7665     // construct
7666     SourceLocation DistEUBLoc = AStmt->getBeginLoc();
7667     ExprResult IsUBGreater =
7668         SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
7669     ExprResult CondOp = SemaRef.ActOnConditionalOp(
7670         DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
7671     PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
7672                                  CondOp.get());
7673     PrevEUB =
7674         SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false);
7675 
7676     // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in
7677     // parallel for is in combination with a distribute directive with
7678     // schedule(static, 1)
7679     Expr *BoundPrevUB = PrevUB.get();
7680     if (UseStrictCompare) {
7681       BoundPrevUB =
7682           SemaRef
7683               .BuildBinOp(
7684                   CurScope, CondLoc, BO_Add, BoundPrevUB,
7685                   SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
7686               .get();
7687       BoundPrevUB =
7688           SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false)
7689               .get();
7690     }
7691     ParForInDistCond =
7692         SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE,
7693                            IV.get(), BoundPrevUB);
7694   }
7695 
7696   // Build updates and final values of the loop counters.
7697   bool HasErrors = false;
7698   Built.Counters.resize(NestedLoopCount);
7699   Built.Inits.resize(NestedLoopCount);
7700   Built.Updates.resize(NestedLoopCount);
7701   Built.Finals.resize(NestedLoopCount);
7702   Built.DependentCounters.resize(NestedLoopCount);
7703   Built.DependentInits.resize(NestedLoopCount);
7704   Built.FinalsConditions.resize(NestedLoopCount);
7705   {
7706     // We implement the following algorithm for obtaining the
7707     // original loop iteration variable values based on the
7708     // value of the collapsed loop iteration variable IV.
7709     //
7710     // Let n+1 be the number of collapsed loops in the nest.
7711     // Iteration variables (I0, I1, .... In)
7712     // Iteration counts (N0, N1, ... Nn)
7713     //
7714     // Acc = IV;
7715     //
7716     // To compute Ik for loop k, 0 <= k <= n, generate:
7717     //    Prod = N(k+1) * N(k+2) * ... * Nn;
7718     //    Ik = Acc / Prod;
7719     //    Acc -= Ik * Prod;
7720     //
7721     ExprResult Acc = IV;
7722     for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
7723       LoopIterationSpace &IS = IterSpaces[Cnt];
7724       SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
7725       ExprResult Iter;
7726 
7727       // Compute prod
7728       ExprResult Prod =
7729           SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
7730       for (unsigned int K = Cnt+1; K < NestedLoopCount; ++K)
7731         Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(),
7732                                   IterSpaces[K].NumIterations);
7733 
7734       // Iter = Acc / Prod
7735       // If there is at least one more inner loop to avoid
7736       // multiplication by 1.
7737       if (Cnt + 1 < NestedLoopCount)
7738         Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div,
7739                                   Acc.get(), Prod.get());
7740       else
7741         Iter = Acc;
7742       if (!Iter.isUsable()) {
7743         HasErrors = true;
7744         break;
7745       }
7746 
7747       // Update Acc:
7748       // Acc -= Iter * Prod
7749       // Check if there is at least one more inner loop to avoid
7750       // multiplication by 1.
7751       if (Cnt + 1 < NestedLoopCount)
7752         Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul,
7753                                   Iter.get(), Prod.get());
7754       else
7755         Prod = Iter;
7756       Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub,
7757                                Acc.get(), Prod.get());
7758 
7759       // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
7760       auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
7761       DeclRefExpr *CounterVar = buildDeclRefExpr(
7762           SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
7763           /*RefersToCapture=*/true);
7764       ExprResult Init =
7765           buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
7766                            IS.CounterInit, IS.IsNonRectangularLB, Captures);
7767       if (!Init.isUsable()) {
7768         HasErrors = true;
7769         break;
7770       }
7771       ExprResult Update = buildCounterUpdate(
7772           SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
7773           IS.CounterStep, IS.Subtract, IS.IsNonRectangularLB, &Captures);
7774       if (!Update.isUsable()) {
7775         HasErrors = true;
7776         break;
7777       }
7778 
7779       // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
7780       ExprResult Final =
7781           buildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
7782                              IS.CounterInit, IS.NumIterations, IS.CounterStep,
7783                              IS.Subtract, IS.IsNonRectangularLB, &Captures);
7784       if (!Final.isUsable()) {
7785         HasErrors = true;
7786         break;
7787       }
7788 
7789       if (!Update.isUsable() || !Final.isUsable()) {
7790         HasErrors = true;
7791         break;
7792       }
7793       // Save results
7794       Built.Counters[Cnt] = IS.CounterVar;
7795       Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
7796       Built.Inits[Cnt] = Init.get();
7797       Built.Updates[Cnt] = Update.get();
7798       Built.Finals[Cnt] = Final.get();
7799       Built.DependentCounters[Cnt] = nullptr;
7800       Built.DependentInits[Cnt] = nullptr;
7801       Built.FinalsConditions[Cnt] = nullptr;
7802       if (IS.IsNonRectangularLB || IS.IsNonRectangularUB) {
7803         Built.DependentCounters[Cnt] =
7804             Built.Counters[NestedLoopCount - 1 - IS.LoopDependentIdx];
7805         Built.DependentInits[Cnt] =
7806             Built.Inits[NestedLoopCount - 1 - IS.LoopDependentIdx];
7807         Built.FinalsConditions[Cnt] = IS.FinalCondition;
7808       }
7809     }
7810   }
7811 
7812   if (HasErrors)
7813     return 0;
7814 
7815   // Save results
7816   Built.IterationVarRef = IV.get();
7817   Built.LastIteration = LastIteration.get();
7818   Built.NumIterations = NumIterations.get();
7819   Built.CalcLastIteration = SemaRef
7820                                 .ActOnFinishFullExpr(CalcLastIteration.get(),
7821                                                      /*DiscardedValue=*/false)
7822                                 .get();
7823   Built.PreCond = PreCond.get();
7824   Built.PreInits = buildPreInits(C, Captures);
7825   Built.Cond = Cond.get();
7826   Built.Init = Init.get();
7827   Built.Inc = Inc.get();
7828   Built.LB = LB.get();
7829   Built.UB = UB.get();
7830   Built.IL = IL.get();
7831   Built.ST = ST.get();
7832   Built.EUB = EUB.get();
7833   Built.NLB = NextLB.get();
7834   Built.NUB = NextUB.get();
7835   Built.PrevLB = PrevLB.get();
7836   Built.PrevUB = PrevUB.get();
7837   Built.DistInc = DistInc.get();
7838   Built.PrevEUB = PrevEUB.get();
7839   Built.DistCombinedFields.LB = CombLB.get();
7840   Built.DistCombinedFields.UB = CombUB.get();
7841   Built.DistCombinedFields.EUB = CombEUB.get();
7842   Built.DistCombinedFields.Init = CombInit.get();
7843   Built.DistCombinedFields.Cond = CombCond.get();
7844   Built.DistCombinedFields.NLB = CombNextLB.get();
7845   Built.DistCombinedFields.NUB = CombNextUB.get();
7846   Built.DistCombinedFields.DistCond = CombDistCond.get();
7847   Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get();
7848 
7849   return NestedLoopCount;
7850 }
7851 
7852 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
7853   auto CollapseClauses =
7854       OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
7855   if (CollapseClauses.begin() != CollapseClauses.end())
7856     return (*CollapseClauses.begin())->getNumForLoops();
7857   return nullptr;
7858 }
7859 
7860 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
7861   auto OrderedClauses =
7862       OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
7863   if (OrderedClauses.begin() != OrderedClauses.end())
7864     return (*OrderedClauses.begin())->getNumForLoops();
7865   return nullptr;
7866 }
7867 
7868 static bool checkSimdlenSafelenSpecified(Sema &S,
7869                                          const ArrayRef<OMPClause *> Clauses) {
7870   const OMPSafelenClause *Safelen = nullptr;
7871   const OMPSimdlenClause *Simdlen = nullptr;
7872 
7873   for (const OMPClause *Clause : Clauses) {
7874     if (Clause->getClauseKind() == OMPC_safelen)
7875       Safelen = cast<OMPSafelenClause>(Clause);
7876     else if (Clause->getClauseKind() == OMPC_simdlen)
7877       Simdlen = cast<OMPSimdlenClause>(Clause);
7878     if (Safelen && Simdlen)
7879       break;
7880   }
7881 
7882   if (Simdlen && Safelen) {
7883     const Expr *SimdlenLength = Simdlen->getSimdlen();
7884     const Expr *SafelenLength = Safelen->getSafelen();
7885     if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
7886         SimdlenLength->isInstantiationDependent() ||
7887         SimdlenLength->containsUnexpandedParameterPack())
7888       return false;
7889     if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
7890         SafelenLength->isInstantiationDependent() ||
7891         SafelenLength->containsUnexpandedParameterPack())
7892       return false;
7893     Expr::EvalResult SimdlenResult, SafelenResult;
7894     SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context);
7895     SafelenLength->EvaluateAsInt(SafelenResult, S.Context);
7896     llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt();
7897     llvm::APSInt SafelenRes = SafelenResult.Val.getInt();
7898     // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
7899     // If both simdlen and safelen clauses are specified, the value of the
7900     // simdlen parameter must be less than or equal to the value of the safelen
7901     // parameter.
7902     if (SimdlenRes > SafelenRes) {
7903       S.Diag(SimdlenLength->getExprLoc(),
7904              diag::err_omp_wrong_simdlen_safelen_values)
7905           << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
7906       return true;
7907     }
7908   }
7909   return false;
7910 }
7911 
7912 StmtResult
7913 Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
7914                                SourceLocation StartLoc, SourceLocation EndLoc,
7915                                VarsWithInheritedDSAType &VarsWithImplicitDSA) {
7916   if (!AStmt)
7917     return StmtError();
7918 
7919   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7920   OMPLoopDirective::HelperExprs B;
7921   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7922   // define the nested loops number.
7923   unsigned NestedLoopCount = checkOpenMPLoop(
7924       OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
7925       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
7926   if (NestedLoopCount == 0)
7927     return StmtError();
7928 
7929   assert((CurContext->isDependentContext() || B.builtAll()) &&
7930          "omp simd loop exprs were not built");
7931 
7932   if (!CurContext->isDependentContext()) {
7933     // Finalize the clauses that need pre-built expressions for CodeGen.
7934     for (OMPClause *C : Clauses) {
7935       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7936         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7937                                      B.NumIterations, *this, CurScope,
7938                                      DSAStack))
7939           return StmtError();
7940     }
7941   }
7942 
7943   if (checkSimdlenSafelenSpecified(*this, Clauses))
7944     return StmtError();
7945 
7946   setFunctionHasBranchProtectedScope();
7947   return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
7948                                   Clauses, AStmt, B);
7949 }
7950 
7951 StmtResult
7952 Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
7953                               SourceLocation StartLoc, SourceLocation EndLoc,
7954                               VarsWithInheritedDSAType &VarsWithImplicitDSA) {
7955   if (!AStmt)
7956     return StmtError();
7957 
7958   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7959   OMPLoopDirective::HelperExprs B;
7960   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7961   // define the nested loops number.
7962   unsigned NestedLoopCount = checkOpenMPLoop(
7963       OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
7964       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
7965   if (NestedLoopCount == 0)
7966     return StmtError();
7967 
7968   assert((CurContext->isDependentContext() || B.builtAll()) &&
7969          "omp for loop exprs were not built");
7970 
7971   if (!CurContext->isDependentContext()) {
7972     // Finalize the clauses that need pre-built expressions for CodeGen.
7973     for (OMPClause *C : Clauses) {
7974       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7975         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7976                                      B.NumIterations, *this, CurScope,
7977                                      DSAStack))
7978           return StmtError();
7979     }
7980   }
7981 
7982   setFunctionHasBranchProtectedScope();
7983   return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
7984                                  Clauses, AStmt, B, DSAStack->isCancelRegion());
7985 }
7986 
7987 StmtResult Sema::ActOnOpenMPForSimdDirective(
7988     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7989     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
7990   if (!AStmt)
7991     return StmtError();
7992 
7993   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7994   OMPLoopDirective::HelperExprs B;
7995   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7996   // define the nested loops number.
7997   unsigned NestedLoopCount =
7998       checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
7999                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
8000                       VarsWithImplicitDSA, B);
8001   if (NestedLoopCount == 0)
8002     return StmtError();
8003 
8004   assert((CurContext->isDependentContext() || B.builtAll()) &&
8005          "omp for simd loop exprs were not built");
8006 
8007   if (!CurContext->isDependentContext()) {
8008     // Finalize the clauses that need pre-built expressions for CodeGen.
8009     for (OMPClause *C : Clauses) {
8010       if (auto *LC = dyn_cast<OMPLinearClause>(C))
8011         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8012                                      B.NumIterations, *this, CurScope,
8013                                      DSAStack))
8014           return StmtError();
8015     }
8016   }
8017 
8018   if (checkSimdlenSafelenSpecified(*this, Clauses))
8019     return StmtError();
8020 
8021   setFunctionHasBranchProtectedScope();
8022   return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
8023                                      Clauses, AStmt, B);
8024 }
8025 
8026 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
8027                                               Stmt *AStmt,
8028                                               SourceLocation StartLoc,
8029                                               SourceLocation EndLoc) {
8030   if (!AStmt)
8031     return StmtError();
8032 
8033   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8034   auto BaseStmt = AStmt;
8035   while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
8036     BaseStmt = CS->getCapturedStmt();
8037   if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
8038     auto S = C->children();
8039     if (S.begin() == S.end())
8040       return StmtError();
8041     // All associated statements must be '#pragma omp section' except for
8042     // the first one.
8043     for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
8044       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
8045         if (SectionStmt)
8046           Diag(SectionStmt->getBeginLoc(),
8047                diag::err_omp_sections_substmt_not_section);
8048         return StmtError();
8049       }
8050       cast<OMPSectionDirective>(SectionStmt)
8051           ->setHasCancel(DSAStack->isCancelRegion());
8052     }
8053   } else {
8054     Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt);
8055     return StmtError();
8056   }
8057 
8058   setFunctionHasBranchProtectedScope();
8059 
8060   return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
8061                                       DSAStack->isCancelRegion());
8062 }
8063 
8064 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
8065                                              SourceLocation StartLoc,
8066                                              SourceLocation EndLoc) {
8067   if (!AStmt)
8068     return StmtError();
8069 
8070   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8071 
8072   setFunctionHasBranchProtectedScope();
8073   DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
8074 
8075   return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
8076                                      DSAStack->isCancelRegion());
8077 }
8078 
8079 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
8080                                             Stmt *AStmt,
8081                                             SourceLocation StartLoc,
8082                                             SourceLocation EndLoc) {
8083   if (!AStmt)
8084     return StmtError();
8085 
8086   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8087 
8088   setFunctionHasBranchProtectedScope();
8089 
8090   // OpenMP [2.7.3, single Construct, Restrictions]
8091   // The copyprivate clause must not be used with the nowait clause.
8092   const OMPClause *Nowait = nullptr;
8093   const OMPClause *Copyprivate = nullptr;
8094   for (const OMPClause *Clause : Clauses) {
8095     if (Clause->getClauseKind() == OMPC_nowait)
8096       Nowait = Clause;
8097     else if (Clause->getClauseKind() == OMPC_copyprivate)
8098       Copyprivate = Clause;
8099     if (Copyprivate && Nowait) {
8100       Diag(Copyprivate->getBeginLoc(),
8101            diag::err_omp_single_copyprivate_with_nowait);
8102       Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here);
8103       return StmtError();
8104     }
8105   }
8106 
8107   return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
8108 }
8109 
8110 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
8111                                             SourceLocation StartLoc,
8112                                             SourceLocation EndLoc) {
8113   if (!AStmt)
8114     return StmtError();
8115 
8116   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8117 
8118   setFunctionHasBranchProtectedScope();
8119 
8120   return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
8121 }
8122 
8123 StmtResult Sema::ActOnOpenMPCriticalDirective(
8124     const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
8125     Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
8126   if (!AStmt)
8127     return StmtError();
8128 
8129   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8130 
8131   bool ErrorFound = false;
8132   llvm::APSInt Hint;
8133   SourceLocation HintLoc;
8134   bool DependentHint = false;
8135   for (const OMPClause *C : Clauses) {
8136     if (C->getClauseKind() == OMPC_hint) {
8137       if (!DirName.getName()) {
8138         Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name);
8139         ErrorFound = true;
8140       }
8141       Expr *E = cast<OMPHintClause>(C)->getHint();
8142       if (E->isTypeDependent() || E->isValueDependent() ||
8143           E->isInstantiationDependent()) {
8144         DependentHint = true;
8145       } else {
8146         Hint = E->EvaluateKnownConstInt(Context);
8147         HintLoc = C->getBeginLoc();
8148       }
8149     }
8150   }
8151   if (ErrorFound)
8152     return StmtError();
8153   const auto Pair = DSAStack->getCriticalWithHint(DirName);
8154   if (Pair.first && DirName.getName() && !DependentHint) {
8155     if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
8156       Diag(StartLoc, diag::err_omp_critical_with_hint);
8157       if (HintLoc.isValid())
8158         Diag(HintLoc, diag::note_omp_critical_hint_here)
8159             << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
8160       else
8161         Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
8162       if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
8163         Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here)
8164             << 1
8165             << C->getHint()->EvaluateKnownConstInt(Context).toString(
8166                    /*Radix=*/10, /*Signed=*/false);
8167       } else {
8168         Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1;
8169       }
8170     }
8171   }
8172 
8173   setFunctionHasBranchProtectedScope();
8174 
8175   auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
8176                                            Clauses, AStmt);
8177   if (!Pair.first && DirName.getName() && !DependentHint)
8178     DSAStack->addCriticalWithHint(Dir, Hint);
8179   return Dir;
8180 }
8181 
8182 StmtResult Sema::ActOnOpenMPParallelForDirective(
8183     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
8184     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
8185   if (!AStmt)
8186     return StmtError();
8187 
8188   auto *CS = cast<CapturedStmt>(AStmt);
8189   // 1.2.2 OpenMP Language Terminology
8190   // Structured block - An executable statement with a single entry at the
8191   // top and a single exit at the bottom.
8192   // The point of exit cannot be a branch out of the structured block.
8193   // longjmp() and throw() must not violate the entry/exit criteria.
8194   CS->getCapturedDecl()->setNothrow();
8195 
8196   OMPLoopDirective::HelperExprs B;
8197   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
8198   // define the nested loops number.
8199   unsigned NestedLoopCount =
8200       checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
8201                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
8202                       VarsWithImplicitDSA, B);
8203   if (NestedLoopCount == 0)
8204     return StmtError();
8205 
8206   assert((CurContext->isDependentContext() || B.builtAll()) &&
8207          "omp parallel for loop exprs were not built");
8208 
8209   if (!CurContext->isDependentContext()) {
8210     // Finalize the clauses that need pre-built expressions for CodeGen.
8211     for (OMPClause *C : Clauses) {
8212       if (auto *LC = dyn_cast<OMPLinearClause>(C))
8213         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8214                                      B.NumIterations, *this, CurScope,
8215                                      DSAStack))
8216           return StmtError();
8217     }
8218   }
8219 
8220   setFunctionHasBranchProtectedScope();
8221   return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
8222                                          NestedLoopCount, Clauses, AStmt, B,
8223                                          DSAStack->isCancelRegion());
8224 }
8225 
8226 StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
8227     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
8228     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
8229   if (!AStmt)
8230     return StmtError();
8231 
8232   auto *CS = cast<CapturedStmt>(AStmt);
8233   // 1.2.2 OpenMP Language Terminology
8234   // Structured block - An executable statement with a single entry at the
8235   // top and a single exit at the bottom.
8236   // The point of exit cannot be a branch out of the structured block.
8237   // longjmp() and throw() must not violate the entry/exit criteria.
8238   CS->getCapturedDecl()->setNothrow();
8239 
8240   OMPLoopDirective::HelperExprs B;
8241   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
8242   // define the nested loops number.
8243   unsigned NestedLoopCount =
8244       checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
8245                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
8246                       VarsWithImplicitDSA, B);
8247   if (NestedLoopCount == 0)
8248     return StmtError();
8249 
8250   if (!CurContext->isDependentContext()) {
8251     // Finalize the clauses that need pre-built expressions for CodeGen.
8252     for (OMPClause *C : Clauses) {
8253       if (auto *LC = dyn_cast<OMPLinearClause>(C))
8254         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8255                                      B.NumIterations, *this, CurScope,
8256                                      DSAStack))
8257           return StmtError();
8258     }
8259   }
8260 
8261   if (checkSimdlenSafelenSpecified(*this, Clauses))
8262     return StmtError();
8263 
8264   setFunctionHasBranchProtectedScope();
8265   return OMPParallelForSimdDirective::Create(
8266       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8267 }
8268 
8269 StmtResult
8270 Sema::ActOnOpenMPParallelMasterDirective(ArrayRef<OMPClause *> Clauses,
8271                                          Stmt *AStmt, SourceLocation StartLoc,
8272                                          SourceLocation EndLoc) {
8273   if (!AStmt)
8274     return StmtError();
8275 
8276   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8277   auto *CS = cast<CapturedStmt>(AStmt);
8278   // 1.2.2 OpenMP Language Terminology
8279   // Structured block - An executable statement with a single entry at the
8280   // top and a single exit at the bottom.
8281   // The point of exit cannot be a branch out of the structured block.
8282   // longjmp() and throw() must not violate the entry/exit criteria.
8283   CS->getCapturedDecl()->setNothrow();
8284 
8285   setFunctionHasBranchProtectedScope();
8286 
8287   return OMPParallelMasterDirective::Create(Context, StartLoc, EndLoc, Clauses,
8288                                             AStmt);
8289 }
8290 
8291 StmtResult
8292 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
8293                                            Stmt *AStmt, SourceLocation StartLoc,
8294                                            SourceLocation EndLoc) {
8295   if (!AStmt)
8296     return StmtError();
8297 
8298   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8299   auto BaseStmt = AStmt;
8300   while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
8301     BaseStmt = CS->getCapturedStmt();
8302   if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
8303     auto S = C->children();
8304     if (S.begin() == S.end())
8305       return StmtError();
8306     // All associated statements must be '#pragma omp section' except for
8307     // the first one.
8308     for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
8309       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
8310         if (SectionStmt)
8311           Diag(SectionStmt->getBeginLoc(),
8312                diag::err_omp_parallel_sections_substmt_not_section);
8313         return StmtError();
8314       }
8315       cast<OMPSectionDirective>(SectionStmt)
8316           ->setHasCancel(DSAStack->isCancelRegion());
8317     }
8318   } else {
8319     Diag(AStmt->getBeginLoc(),
8320          diag::err_omp_parallel_sections_not_compound_stmt);
8321     return StmtError();
8322   }
8323 
8324   setFunctionHasBranchProtectedScope();
8325 
8326   return OMPParallelSectionsDirective::Create(
8327       Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
8328 }
8329 
8330 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
8331                                           Stmt *AStmt, SourceLocation StartLoc,
8332                                           SourceLocation EndLoc) {
8333   if (!AStmt)
8334     return StmtError();
8335 
8336   auto *CS = cast<CapturedStmt>(AStmt);
8337   // 1.2.2 OpenMP Language Terminology
8338   // Structured block - An executable statement with a single entry at the
8339   // top and a single exit at the bottom.
8340   // The point of exit cannot be a branch out of the structured block.
8341   // longjmp() and throw() must not violate the entry/exit criteria.
8342   CS->getCapturedDecl()->setNothrow();
8343 
8344   setFunctionHasBranchProtectedScope();
8345 
8346   return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
8347                                   DSAStack->isCancelRegion());
8348 }
8349 
8350 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
8351                                                SourceLocation EndLoc) {
8352   return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
8353 }
8354 
8355 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
8356                                              SourceLocation EndLoc) {
8357   return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
8358 }
8359 
8360 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
8361                                               SourceLocation EndLoc) {
8362   return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
8363 }
8364 
8365 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
8366                                                Stmt *AStmt,
8367                                                SourceLocation StartLoc,
8368                                                SourceLocation EndLoc) {
8369   if (!AStmt)
8370     return StmtError();
8371 
8372   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8373 
8374   setFunctionHasBranchProtectedScope();
8375 
8376   return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
8377                                        AStmt,
8378                                        DSAStack->getTaskgroupReductionRef());
8379 }
8380 
8381 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
8382                                            SourceLocation StartLoc,
8383                                            SourceLocation EndLoc) {
8384   assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
8385   return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
8386 }
8387 
8388 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
8389                                              Stmt *AStmt,
8390                                              SourceLocation StartLoc,
8391                                              SourceLocation EndLoc) {
8392   const OMPClause *DependFound = nullptr;
8393   const OMPClause *DependSourceClause = nullptr;
8394   const OMPClause *DependSinkClause = nullptr;
8395   bool ErrorFound = false;
8396   const OMPThreadsClause *TC = nullptr;
8397   const OMPSIMDClause *SC = nullptr;
8398   for (const OMPClause *C : Clauses) {
8399     if (auto *DC = dyn_cast<OMPDependClause>(C)) {
8400       DependFound = C;
8401       if (DC->getDependencyKind() == OMPC_DEPEND_source) {
8402         if (DependSourceClause) {
8403           Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
8404               << getOpenMPDirectiveName(OMPD_ordered)
8405               << getOpenMPClauseName(OMPC_depend) << 2;
8406           ErrorFound = true;
8407         } else {
8408           DependSourceClause = C;
8409         }
8410         if (DependSinkClause) {
8411           Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
8412               << 0;
8413           ErrorFound = true;
8414         }
8415       } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
8416         if (DependSourceClause) {
8417           Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
8418               << 1;
8419           ErrorFound = true;
8420         }
8421         DependSinkClause = C;
8422       }
8423     } else if (C->getClauseKind() == OMPC_threads) {
8424       TC = cast<OMPThreadsClause>(C);
8425     } else if (C->getClauseKind() == OMPC_simd) {
8426       SC = cast<OMPSIMDClause>(C);
8427     }
8428   }
8429   if (!ErrorFound && !SC &&
8430       isOpenMPSimdDirective(DSAStack->getParentDirective())) {
8431     // OpenMP [2.8.1,simd Construct, Restrictions]
8432     // An ordered construct with the simd clause is the only OpenMP construct
8433     // that can appear in the simd region.
8434     Diag(StartLoc, diag::err_omp_prohibited_region_simd)
8435         << (LangOpts.OpenMP >= 50 ? 1 : 0);
8436     ErrorFound = true;
8437   } else if (DependFound && (TC || SC)) {
8438     Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd)
8439         << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
8440     ErrorFound = true;
8441   } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) {
8442     Diag(DependFound->getBeginLoc(),
8443          diag::err_omp_ordered_directive_without_param);
8444     ErrorFound = true;
8445   } else if (TC || Clauses.empty()) {
8446     if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
8447       SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
8448       Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
8449           << (TC != nullptr);
8450       Diag(Param->getBeginLoc(), diag::note_omp_ordered_param);
8451       ErrorFound = true;
8452     }
8453   }
8454   if ((!AStmt && !DependFound) || ErrorFound)
8455     return StmtError();
8456 
8457   if (AStmt) {
8458     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8459 
8460     setFunctionHasBranchProtectedScope();
8461   }
8462 
8463   return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
8464 }
8465 
8466 namespace {
8467 /// Helper class for checking expression in 'omp atomic [update]'
8468 /// construct.
8469 class OpenMPAtomicUpdateChecker {
8470   /// Error results for atomic update expressions.
8471   enum ExprAnalysisErrorCode {
8472     /// A statement is not an expression statement.
8473     NotAnExpression,
8474     /// Expression is not builtin binary or unary operation.
8475     NotABinaryOrUnaryExpression,
8476     /// Unary operation is not post-/pre- increment/decrement operation.
8477     NotAnUnaryIncDecExpression,
8478     /// An expression is not of scalar type.
8479     NotAScalarType,
8480     /// A binary operation is not an assignment operation.
8481     NotAnAssignmentOp,
8482     /// RHS part of the binary operation is not a binary expression.
8483     NotABinaryExpression,
8484     /// RHS part is not additive/multiplicative/shift/biwise binary
8485     /// expression.
8486     NotABinaryOperator,
8487     /// RHS binary operation does not have reference to the updated LHS
8488     /// part.
8489     NotAnUpdateExpression,
8490     /// No errors is found.
8491     NoError
8492   };
8493   /// Reference to Sema.
8494   Sema &SemaRef;
8495   /// A location for note diagnostics (when error is found).
8496   SourceLocation NoteLoc;
8497   /// 'x' lvalue part of the source atomic expression.
8498   Expr *X;
8499   /// 'expr' rvalue part of the source atomic expression.
8500   Expr *E;
8501   /// Helper expression of the form
8502   /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
8503   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
8504   Expr *UpdateExpr;
8505   /// Is 'x' a LHS in a RHS part of full update expression. It is
8506   /// important for non-associative operations.
8507   bool IsXLHSInRHSPart;
8508   BinaryOperatorKind Op;
8509   SourceLocation OpLoc;
8510   /// true if the source expression is a postfix unary operation, false
8511   /// if it is a prefix unary operation.
8512   bool IsPostfixUpdate;
8513 
8514 public:
8515   OpenMPAtomicUpdateChecker(Sema &SemaRef)
8516       : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
8517         IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
8518   /// Check specified statement that it is suitable for 'atomic update'
8519   /// constructs and extract 'x', 'expr' and Operation from the original
8520   /// expression. If DiagId and NoteId == 0, then only check is performed
8521   /// without error notification.
8522   /// \param DiagId Diagnostic which should be emitted if error is found.
8523   /// \param NoteId Diagnostic note for the main error message.
8524   /// \return true if statement is not an update expression, false otherwise.
8525   bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
8526   /// Return the 'x' lvalue part of the source atomic expression.
8527   Expr *getX() const { return X; }
8528   /// Return the 'expr' rvalue part of the source atomic expression.
8529   Expr *getExpr() const { return E; }
8530   /// Return the update expression used in calculation of the updated
8531   /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
8532   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
8533   Expr *getUpdateExpr() const { return UpdateExpr; }
8534   /// Return true if 'x' is LHS in RHS part of full update expression,
8535   /// false otherwise.
8536   bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
8537 
8538   /// true if the source expression is a postfix unary operation, false
8539   /// if it is a prefix unary operation.
8540   bool isPostfixUpdate() const { return IsPostfixUpdate; }
8541 
8542 private:
8543   bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
8544                             unsigned NoteId = 0);
8545 };
8546 } // namespace
8547 
8548 bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
8549     BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
8550   ExprAnalysisErrorCode ErrorFound = NoError;
8551   SourceLocation ErrorLoc, NoteLoc;
8552   SourceRange ErrorRange, NoteRange;
8553   // Allowed constructs are:
8554   //  x = x binop expr;
8555   //  x = expr binop x;
8556   if (AtomicBinOp->getOpcode() == BO_Assign) {
8557     X = AtomicBinOp->getLHS();
8558     if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
8559             AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
8560       if (AtomicInnerBinOp->isMultiplicativeOp() ||
8561           AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
8562           AtomicInnerBinOp->isBitwiseOp()) {
8563         Op = AtomicInnerBinOp->getOpcode();
8564         OpLoc = AtomicInnerBinOp->getOperatorLoc();
8565         Expr *LHS = AtomicInnerBinOp->getLHS();
8566         Expr *RHS = AtomicInnerBinOp->getRHS();
8567         llvm::FoldingSetNodeID XId, LHSId, RHSId;
8568         X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
8569                                           /*Canonical=*/true);
8570         LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
8571                                             /*Canonical=*/true);
8572         RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
8573                                             /*Canonical=*/true);
8574         if (XId == LHSId) {
8575           E = RHS;
8576           IsXLHSInRHSPart = true;
8577         } else if (XId == RHSId) {
8578           E = LHS;
8579           IsXLHSInRHSPart = false;
8580         } else {
8581           ErrorLoc = AtomicInnerBinOp->getExprLoc();
8582           ErrorRange = AtomicInnerBinOp->getSourceRange();
8583           NoteLoc = X->getExprLoc();
8584           NoteRange = X->getSourceRange();
8585           ErrorFound = NotAnUpdateExpression;
8586         }
8587       } else {
8588         ErrorLoc = AtomicInnerBinOp->getExprLoc();
8589         ErrorRange = AtomicInnerBinOp->getSourceRange();
8590         NoteLoc = AtomicInnerBinOp->getOperatorLoc();
8591         NoteRange = SourceRange(NoteLoc, NoteLoc);
8592         ErrorFound = NotABinaryOperator;
8593       }
8594     } else {
8595       NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
8596       NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
8597       ErrorFound = NotABinaryExpression;
8598     }
8599   } else {
8600     ErrorLoc = AtomicBinOp->getExprLoc();
8601     ErrorRange = AtomicBinOp->getSourceRange();
8602     NoteLoc = AtomicBinOp->getOperatorLoc();
8603     NoteRange = SourceRange(NoteLoc, NoteLoc);
8604     ErrorFound = NotAnAssignmentOp;
8605   }
8606   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
8607     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
8608     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
8609     return true;
8610   }
8611   if (SemaRef.CurContext->isDependentContext())
8612     E = X = UpdateExpr = nullptr;
8613   return ErrorFound != NoError;
8614 }
8615 
8616 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
8617                                                unsigned NoteId) {
8618   ExprAnalysisErrorCode ErrorFound = NoError;
8619   SourceLocation ErrorLoc, NoteLoc;
8620   SourceRange ErrorRange, NoteRange;
8621   // Allowed constructs are:
8622   //  x++;
8623   //  x--;
8624   //  ++x;
8625   //  --x;
8626   //  x binop= expr;
8627   //  x = x binop expr;
8628   //  x = expr binop x;
8629   if (auto *AtomicBody = dyn_cast<Expr>(S)) {
8630     AtomicBody = AtomicBody->IgnoreParenImpCasts();
8631     if (AtomicBody->getType()->isScalarType() ||
8632         AtomicBody->isInstantiationDependent()) {
8633       if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
8634               AtomicBody->IgnoreParenImpCasts())) {
8635         // Check for Compound Assignment Operation
8636         Op = BinaryOperator::getOpForCompoundAssignment(
8637             AtomicCompAssignOp->getOpcode());
8638         OpLoc = AtomicCompAssignOp->getOperatorLoc();
8639         E = AtomicCompAssignOp->getRHS();
8640         X = AtomicCompAssignOp->getLHS()->IgnoreParens();
8641         IsXLHSInRHSPart = true;
8642       } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
8643                      AtomicBody->IgnoreParenImpCasts())) {
8644         // Check for Binary Operation
8645         if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
8646           return true;
8647       } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
8648                      AtomicBody->IgnoreParenImpCasts())) {
8649         // Check for Unary Operation
8650         if (AtomicUnaryOp->isIncrementDecrementOp()) {
8651           IsPostfixUpdate = AtomicUnaryOp->isPostfix();
8652           Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
8653           OpLoc = AtomicUnaryOp->getOperatorLoc();
8654           X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
8655           E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
8656           IsXLHSInRHSPart = true;
8657         } else {
8658           ErrorFound = NotAnUnaryIncDecExpression;
8659           ErrorLoc = AtomicUnaryOp->getExprLoc();
8660           ErrorRange = AtomicUnaryOp->getSourceRange();
8661           NoteLoc = AtomicUnaryOp->getOperatorLoc();
8662           NoteRange = SourceRange(NoteLoc, NoteLoc);
8663         }
8664       } else if (!AtomicBody->isInstantiationDependent()) {
8665         ErrorFound = NotABinaryOrUnaryExpression;
8666         NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
8667         NoteRange = ErrorRange = AtomicBody->getSourceRange();
8668       }
8669     } else {
8670       ErrorFound = NotAScalarType;
8671       NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
8672       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
8673     }
8674   } else {
8675     ErrorFound = NotAnExpression;
8676     NoteLoc = ErrorLoc = S->getBeginLoc();
8677     NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
8678   }
8679   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
8680     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
8681     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
8682     return true;
8683   }
8684   if (SemaRef.CurContext->isDependentContext())
8685     E = X = UpdateExpr = nullptr;
8686   if (ErrorFound == NoError && E && X) {
8687     // Build an update expression of form 'OpaqueValueExpr(x) binop
8688     // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
8689     // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
8690     auto *OVEX = new (SemaRef.getASTContext())
8691         OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
8692     auto *OVEExpr = new (SemaRef.getASTContext())
8693         OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
8694     ExprResult Update =
8695         SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
8696                                    IsXLHSInRHSPart ? OVEExpr : OVEX);
8697     if (Update.isInvalid())
8698       return true;
8699     Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
8700                                                Sema::AA_Casting);
8701     if (Update.isInvalid())
8702       return true;
8703     UpdateExpr = Update.get();
8704   }
8705   return ErrorFound != NoError;
8706 }
8707 
8708 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
8709                                             Stmt *AStmt,
8710                                             SourceLocation StartLoc,
8711                                             SourceLocation EndLoc) {
8712   if (!AStmt)
8713     return StmtError();
8714 
8715   auto *CS = cast<CapturedStmt>(AStmt);
8716   // 1.2.2 OpenMP Language Terminology
8717   // Structured block - An executable statement with a single entry at the
8718   // top and a single exit at the bottom.
8719   // The point of exit cannot be a branch out of the structured block.
8720   // longjmp() and throw() must not violate the entry/exit criteria.
8721   OpenMPClauseKind AtomicKind = OMPC_unknown;
8722   SourceLocation AtomicKindLoc;
8723   for (const OMPClause *C : Clauses) {
8724     if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
8725         C->getClauseKind() == OMPC_update ||
8726         C->getClauseKind() == OMPC_capture) {
8727       if (AtomicKind != OMPC_unknown) {
8728         Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses)
8729             << SourceRange(C->getBeginLoc(), C->getEndLoc());
8730         Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
8731             << getOpenMPClauseName(AtomicKind);
8732       } else {
8733         AtomicKind = C->getClauseKind();
8734         AtomicKindLoc = C->getBeginLoc();
8735       }
8736     }
8737   }
8738 
8739   Stmt *Body = CS->getCapturedStmt();
8740   if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
8741     Body = EWC->getSubExpr();
8742 
8743   Expr *X = nullptr;
8744   Expr *V = nullptr;
8745   Expr *E = nullptr;
8746   Expr *UE = nullptr;
8747   bool IsXLHSInRHSPart = false;
8748   bool IsPostfixUpdate = false;
8749   // OpenMP [2.12.6, atomic Construct]
8750   // In the next expressions:
8751   // * x and v (as applicable) are both l-value expressions with scalar type.
8752   // * During the execution of an atomic region, multiple syntactic
8753   // occurrences of x must designate the same storage location.
8754   // * Neither of v and expr (as applicable) may access the storage location
8755   // designated by x.
8756   // * Neither of x and expr (as applicable) may access the storage location
8757   // designated by v.
8758   // * expr is an expression with scalar type.
8759   // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
8760   // * binop, binop=, ++, and -- are not overloaded operators.
8761   // * The expression x binop expr must be numerically equivalent to x binop
8762   // (expr). This requirement is satisfied if the operators in expr have
8763   // precedence greater than binop, or by using parentheses around expr or
8764   // subexpressions of expr.
8765   // * The expression expr binop x must be numerically equivalent to (expr)
8766   // binop x. This requirement is satisfied if the operators in expr have
8767   // precedence equal to or greater than binop, or by using parentheses around
8768   // expr or subexpressions of expr.
8769   // * For forms that allow multiple occurrences of x, the number of times
8770   // that x is evaluated is unspecified.
8771   if (AtomicKind == OMPC_read) {
8772     enum {
8773       NotAnExpression,
8774       NotAnAssignmentOp,
8775       NotAScalarType,
8776       NotAnLValue,
8777       NoError
8778     } ErrorFound = NoError;
8779     SourceLocation ErrorLoc, NoteLoc;
8780     SourceRange ErrorRange, NoteRange;
8781     // If clause is read:
8782     //  v = x;
8783     if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
8784       const auto *AtomicBinOp =
8785           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
8786       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
8787         X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
8788         V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
8789         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
8790             (V->isInstantiationDependent() || V->getType()->isScalarType())) {
8791           if (!X->isLValue() || !V->isLValue()) {
8792             const Expr *NotLValueExpr = X->isLValue() ? V : X;
8793             ErrorFound = NotAnLValue;
8794             ErrorLoc = AtomicBinOp->getExprLoc();
8795             ErrorRange = AtomicBinOp->getSourceRange();
8796             NoteLoc = NotLValueExpr->getExprLoc();
8797             NoteRange = NotLValueExpr->getSourceRange();
8798           }
8799         } else if (!X->isInstantiationDependent() ||
8800                    !V->isInstantiationDependent()) {
8801           const Expr *NotScalarExpr =
8802               (X->isInstantiationDependent() || X->getType()->isScalarType())
8803                   ? V
8804                   : X;
8805           ErrorFound = NotAScalarType;
8806           ErrorLoc = AtomicBinOp->getExprLoc();
8807           ErrorRange = AtomicBinOp->getSourceRange();
8808           NoteLoc = NotScalarExpr->getExprLoc();
8809           NoteRange = NotScalarExpr->getSourceRange();
8810         }
8811       } else if (!AtomicBody->isInstantiationDependent()) {
8812         ErrorFound = NotAnAssignmentOp;
8813         ErrorLoc = AtomicBody->getExprLoc();
8814         ErrorRange = AtomicBody->getSourceRange();
8815         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
8816                               : AtomicBody->getExprLoc();
8817         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
8818                                 : AtomicBody->getSourceRange();
8819       }
8820     } else {
8821       ErrorFound = NotAnExpression;
8822       NoteLoc = ErrorLoc = Body->getBeginLoc();
8823       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
8824     }
8825     if (ErrorFound != NoError) {
8826       Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
8827           << ErrorRange;
8828       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
8829                                                       << NoteRange;
8830       return StmtError();
8831     }
8832     if (CurContext->isDependentContext())
8833       V = X = nullptr;
8834   } else if (AtomicKind == OMPC_write) {
8835     enum {
8836       NotAnExpression,
8837       NotAnAssignmentOp,
8838       NotAScalarType,
8839       NotAnLValue,
8840       NoError
8841     } ErrorFound = NoError;
8842     SourceLocation ErrorLoc, NoteLoc;
8843     SourceRange ErrorRange, NoteRange;
8844     // If clause is write:
8845     //  x = expr;
8846     if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
8847       const auto *AtomicBinOp =
8848           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
8849       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
8850         X = AtomicBinOp->getLHS();
8851         E = AtomicBinOp->getRHS();
8852         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
8853             (E->isInstantiationDependent() || E->getType()->isScalarType())) {
8854           if (!X->isLValue()) {
8855             ErrorFound = NotAnLValue;
8856             ErrorLoc = AtomicBinOp->getExprLoc();
8857             ErrorRange = AtomicBinOp->getSourceRange();
8858             NoteLoc = X->getExprLoc();
8859             NoteRange = X->getSourceRange();
8860           }
8861         } else if (!X->isInstantiationDependent() ||
8862                    !E->isInstantiationDependent()) {
8863           const Expr *NotScalarExpr =
8864               (X->isInstantiationDependent() || X->getType()->isScalarType())
8865                   ? E
8866                   : X;
8867           ErrorFound = NotAScalarType;
8868           ErrorLoc = AtomicBinOp->getExprLoc();
8869           ErrorRange = AtomicBinOp->getSourceRange();
8870           NoteLoc = NotScalarExpr->getExprLoc();
8871           NoteRange = NotScalarExpr->getSourceRange();
8872         }
8873       } else if (!AtomicBody->isInstantiationDependent()) {
8874         ErrorFound = NotAnAssignmentOp;
8875         ErrorLoc = AtomicBody->getExprLoc();
8876         ErrorRange = AtomicBody->getSourceRange();
8877         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
8878                               : AtomicBody->getExprLoc();
8879         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
8880                                 : AtomicBody->getSourceRange();
8881       }
8882     } else {
8883       ErrorFound = NotAnExpression;
8884       NoteLoc = ErrorLoc = Body->getBeginLoc();
8885       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
8886     }
8887     if (ErrorFound != NoError) {
8888       Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
8889           << ErrorRange;
8890       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
8891                                                       << NoteRange;
8892       return StmtError();
8893     }
8894     if (CurContext->isDependentContext())
8895       E = X = nullptr;
8896   } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
8897     // If clause is update:
8898     //  x++;
8899     //  x--;
8900     //  ++x;
8901     //  --x;
8902     //  x binop= expr;
8903     //  x = x binop expr;
8904     //  x = expr binop x;
8905     OpenMPAtomicUpdateChecker Checker(*this);
8906     if (Checker.checkStatement(
8907             Body, (AtomicKind == OMPC_update)
8908                       ? diag::err_omp_atomic_update_not_expression_statement
8909                       : diag::err_omp_atomic_not_expression_statement,
8910             diag::note_omp_atomic_update))
8911       return StmtError();
8912     if (!CurContext->isDependentContext()) {
8913       E = Checker.getExpr();
8914       X = Checker.getX();
8915       UE = Checker.getUpdateExpr();
8916       IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
8917     }
8918   } else if (AtomicKind == OMPC_capture) {
8919     enum {
8920       NotAnAssignmentOp,
8921       NotACompoundStatement,
8922       NotTwoSubstatements,
8923       NotASpecificExpression,
8924       NoError
8925     } ErrorFound = NoError;
8926     SourceLocation ErrorLoc, NoteLoc;
8927     SourceRange ErrorRange, NoteRange;
8928     if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
8929       // If clause is a capture:
8930       //  v = x++;
8931       //  v = x--;
8932       //  v = ++x;
8933       //  v = --x;
8934       //  v = x binop= expr;
8935       //  v = x = x binop expr;
8936       //  v = x = expr binop x;
8937       const auto *AtomicBinOp =
8938           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
8939       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
8940         V = AtomicBinOp->getLHS();
8941         Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
8942         OpenMPAtomicUpdateChecker Checker(*this);
8943         if (Checker.checkStatement(
8944                 Body, diag::err_omp_atomic_capture_not_expression_statement,
8945                 diag::note_omp_atomic_update))
8946           return StmtError();
8947         E = Checker.getExpr();
8948         X = Checker.getX();
8949         UE = Checker.getUpdateExpr();
8950         IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
8951         IsPostfixUpdate = Checker.isPostfixUpdate();
8952       } else if (!AtomicBody->isInstantiationDependent()) {
8953         ErrorLoc = AtomicBody->getExprLoc();
8954         ErrorRange = AtomicBody->getSourceRange();
8955         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
8956                               : AtomicBody->getExprLoc();
8957         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
8958                                 : AtomicBody->getSourceRange();
8959         ErrorFound = NotAnAssignmentOp;
8960       }
8961       if (ErrorFound != NoError) {
8962         Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
8963             << ErrorRange;
8964         Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
8965         return StmtError();
8966       }
8967       if (CurContext->isDependentContext())
8968         UE = V = E = X = nullptr;
8969     } else {
8970       // If clause is a capture:
8971       //  { v = x; x = expr; }
8972       //  { v = x; x++; }
8973       //  { v = x; x--; }
8974       //  { v = x; ++x; }
8975       //  { v = x; --x; }
8976       //  { v = x; x binop= expr; }
8977       //  { v = x; x = x binop expr; }
8978       //  { v = x; x = expr binop x; }
8979       //  { x++; v = x; }
8980       //  { x--; v = x; }
8981       //  { ++x; v = x; }
8982       //  { --x; v = x; }
8983       //  { x binop= expr; v = x; }
8984       //  { x = x binop expr; v = x; }
8985       //  { x = expr binop x; v = x; }
8986       if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
8987         // Check that this is { expr1; expr2; }
8988         if (CS->size() == 2) {
8989           Stmt *First = CS->body_front();
8990           Stmt *Second = CS->body_back();
8991           if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
8992             First = EWC->getSubExpr()->IgnoreParenImpCasts();
8993           if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
8994             Second = EWC->getSubExpr()->IgnoreParenImpCasts();
8995           // Need to find what subexpression is 'v' and what is 'x'.
8996           OpenMPAtomicUpdateChecker Checker(*this);
8997           bool IsUpdateExprFound = !Checker.checkStatement(Second);
8998           BinaryOperator *BinOp = nullptr;
8999           if (IsUpdateExprFound) {
9000             BinOp = dyn_cast<BinaryOperator>(First);
9001             IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
9002           }
9003           if (IsUpdateExprFound && !CurContext->isDependentContext()) {
9004             //  { v = x; x++; }
9005             //  { v = x; x--; }
9006             //  { v = x; ++x; }
9007             //  { v = x; --x; }
9008             //  { v = x; x binop= expr; }
9009             //  { v = x; x = x binop expr; }
9010             //  { v = x; x = expr binop x; }
9011             // Check that the first expression has form v = x.
9012             Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
9013             llvm::FoldingSetNodeID XId, PossibleXId;
9014             Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
9015             PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
9016             IsUpdateExprFound = XId == PossibleXId;
9017             if (IsUpdateExprFound) {
9018               V = BinOp->getLHS();
9019               X = Checker.getX();
9020               E = Checker.getExpr();
9021               UE = Checker.getUpdateExpr();
9022               IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
9023               IsPostfixUpdate = true;
9024             }
9025           }
9026           if (!IsUpdateExprFound) {
9027             IsUpdateExprFound = !Checker.checkStatement(First);
9028             BinOp = nullptr;
9029             if (IsUpdateExprFound) {
9030               BinOp = dyn_cast<BinaryOperator>(Second);
9031               IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
9032             }
9033             if (IsUpdateExprFound && !CurContext->isDependentContext()) {
9034               //  { x++; v = x; }
9035               //  { x--; v = x; }
9036               //  { ++x; v = x; }
9037               //  { --x; v = x; }
9038               //  { x binop= expr; v = x; }
9039               //  { x = x binop expr; v = x; }
9040               //  { x = expr binop x; v = x; }
9041               // Check that the second expression has form v = x.
9042               Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
9043               llvm::FoldingSetNodeID XId, PossibleXId;
9044               Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
9045               PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
9046               IsUpdateExprFound = XId == PossibleXId;
9047               if (IsUpdateExprFound) {
9048                 V = BinOp->getLHS();
9049                 X = Checker.getX();
9050                 E = Checker.getExpr();
9051                 UE = Checker.getUpdateExpr();
9052                 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
9053                 IsPostfixUpdate = false;
9054               }
9055             }
9056           }
9057           if (!IsUpdateExprFound) {
9058             //  { v = x; x = expr; }
9059             auto *FirstExpr = dyn_cast<Expr>(First);
9060             auto *SecondExpr = dyn_cast<Expr>(Second);
9061             if (!FirstExpr || !SecondExpr ||
9062                 !(FirstExpr->isInstantiationDependent() ||
9063                   SecondExpr->isInstantiationDependent())) {
9064               auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
9065               if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
9066                 ErrorFound = NotAnAssignmentOp;
9067                 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
9068                                                 : First->getBeginLoc();
9069                 NoteRange = ErrorRange = FirstBinOp
9070                                              ? FirstBinOp->getSourceRange()
9071                                              : SourceRange(ErrorLoc, ErrorLoc);
9072               } else {
9073                 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
9074                 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
9075                   ErrorFound = NotAnAssignmentOp;
9076                   NoteLoc = ErrorLoc = SecondBinOp
9077                                            ? SecondBinOp->getOperatorLoc()
9078                                            : Second->getBeginLoc();
9079                   NoteRange = ErrorRange =
9080                       SecondBinOp ? SecondBinOp->getSourceRange()
9081                                   : SourceRange(ErrorLoc, ErrorLoc);
9082                 } else {
9083                   Expr *PossibleXRHSInFirst =
9084                       FirstBinOp->getRHS()->IgnoreParenImpCasts();
9085                   Expr *PossibleXLHSInSecond =
9086                       SecondBinOp->getLHS()->IgnoreParenImpCasts();
9087                   llvm::FoldingSetNodeID X1Id, X2Id;
9088                   PossibleXRHSInFirst->Profile(X1Id, Context,
9089                                                /*Canonical=*/true);
9090                   PossibleXLHSInSecond->Profile(X2Id, Context,
9091                                                 /*Canonical=*/true);
9092                   IsUpdateExprFound = X1Id == X2Id;
9093                   if (IsUpdateExprFound) {
9094                     V = FirstBinOp->getLHS();
9095                     X = SecondBinOp->getLHS();
9096                     E = SecondBinOp->getRHS();
9097                     UE = nullptr;
9098                     IsXLHSInRHSPart = false;
9099                     IsPostfixUpdate = true;
9100                   } else {
9101                     ErrorFound = NotASpecificExpression;
9102                     ErrorLoc = FirstBinOp->getExprLoc();
9103                     ErrorRange = FirstBinOp->getSourceRange();
9104                     NoteLoc = SecondBinOp->getLHS()->getExprLoc();
9105                     NoteRange = SecondBinOp->getRHS()->getSourceRange();
9106                   }
9107                 }
9108               }
9109             }
9110           }
9111         } else {
9112           NoteLoc = ErrorLoc = Body->getBeginLoc();
9113           NoteRange = ErrorRange =
9114               SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
9115           ErrorFound = NotTwoSubstatements;
9116         }
9117       } else {
9118         NoteLoc = ErrorLoc = Body->getBeginLoc();
9119         NoteRange = ErrorRange =
9120             SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
9121         ErrorFound = NotACompoundStatement;
9122       }
9123       if (ErrorFound != NoError) {
9124         Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
9125             << ErrorRange;
9126         Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
9127         return StmtError();
9128       }
9129       if (CurContext->isDependentContext())
9130         UE = V = E = X = nullptr;
9131     }
9132   }
9133 
9134   setFunctionHasBranchProtectedScope();
9135 
9136   return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
9137                                     X, V, E, UE, IsXLHSInRHSPart,
9138                                     IsPostfixUpdate);
9139 }
9140 
9141 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
9142                                             Stmt *AStmt,
9143                                             SourceLocation StartLoc,
9144                                             SourceLocation EndLoc) {
9145   if (!AStmt)
9146     return StmtError();
9147 
9148   auto *CS = cast<CapturedStmt>(AStmt);
9149   // 1.2.2 OpenMP Language Terminology
9150   // Structured block - An executable statement with a single entry at the
9151   // top and a single exit at the bottom.
9152   // The point of exit cannot be a branch out of the structured block.
9153   // longjmp() and throw() must not violate the entry/exit criteria.
9154   CS->getCapturedDecl()->setNothrow();
9155   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target);
9156        ThisCaptureLevel > 1; --ThisCaptureLevel) {
9157     CS = cast<CapturedStmt>(CS->getCapturedStmt());
9158     // 1.2.2 OpenMP Language Terminology
9159     // Structured block - An executable statement with a single entry at the
9160     // top and a single exit at the bottom.
9161     // The point of exit cannot be a branch out of the structured block.
9162     // longjmp() and throw() must not violate the entry/exit criteria.
9163     CS->getCapturedDecl()->setNothrow();
9164   }
9165 
9166   // OpenMP [2.16, Nesting of Regions]
9167   // If specified, a teams construct must be contained within a target
9168   // construct. That target construct must contain no statements or directives
9169   // outside of the teams construct.
9170   if (DSAStack->hasInnerTeamsRegion()) {
9171     const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
9172     bool OMPTeamsFound = true;
9173     if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
9174       auto I = CS->body_begin();
9175       while (I != CS->body_end()) {
9176         const auto *OED = dyn_cast<OMPExecutableDirective>(*I);
9177         if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) ||
9178             OMPTeamsFound) {
9179 
9180           OMPTeamsFound = false;
9181           break;
9182         }
9183         ++I;
9184       }
9185       assert(I != CS->body_end() && "Not found statement");
9186       S = *I;
9187     } else {
9188       const auto *OED = dyn_cast<OMPExecutableDirective>(S);
9189       OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
9190     }
9191     if (!OMPTeamsFound) {
9192       Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
9193       Diag(DSAStack->getInnerTeamsRegionLoc(),
9194            diag::note_omp_nested_teams_construct_here);
9195       Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here)
9196           << isa<OMPExecutableDirective>(S);
9197       return StmtError();
9198     }
9199   }
9200 
9201   setFunctionHasBranchProtectedScope();
9202 
9203   return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
9204 }
9205 
9206 StmtResult
9207 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
9208                                          Stmt *AStmt, SourceLocation StartLoc,
9209                                          SourceLocation EndLoc) {
9210   if (!AStmt)
9211     return StmtError();
9212 
9213   auto *CS = cast<CapturedStmt>(AStmt);
9214   // 1.2.2 OpenMP Language Terminology
9215   // Structured block - An executable statement with a single entry at the
9216   // top and a single exit at the bottom.
9217   // The point of exit cannot be a branch out of the structured block.
9218   // longjmp() and throw() must not violate the entry/exit criteria.
9219   CS->getCapturedDecl()->setNothrow();
9220   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel);
9221        ThisCaptureLevel > 1; --ThisCaptureLevel) {
9222     CS = cast<CapturedStmt>(CS->getCapturedStmt());
9223     // 1.2.2 OpenMP Language Terminology
9224     // Structured block - An executable statement with a single entry at the
9225     // top and a single exit at the bottom.
9226     // The point of exit cannot be a branch out of the structured block.
9227     // longjmp() and throw() must not violate the entry/exit criteria.
9228     CS->getCapturedDecl()->setNothrow();
9229   }
9230 
9231   setFunctionHasBranchProtectedScope();
9232 
9233   return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
9234                                             AStmt);
9235 }
9236 
9237 StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
9238     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9239     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9240   if (!AStmt)
9241     return StmtError();
9242 
9243   auto *CS = cast<CapturedStmt>(AStmt);
9244   // 1.2.2 OpenMP Language Terminology
9245   // Structured block - An executable statement with a single entry at the
9246   // top and a single exit at the bottom.
9247   // The point of exit cannot be a branch out of the structured block.
9248   // longjmp() and throw() must not violate the entry/exit criteria.
9249   CS->getCapturedDecl()->setNothrow();
9250   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
9251        ThisCaptureLevel > 1; --ThisCaptureLevel) {
9252     CS = cast<CapturedStmt>(CS->getCapturedStmt());
9253     // 1.2.2 OpenMP Language Terminology
9254     // Structured block - An executable statement with a single entry at the
9255     // top and a single exit at the bottom.
9256     // The point of exit cannot be a branch out of the structured block.
9257     // longjmp() and throw() must not violate the entry/exit criteria.
9258     CS->getCapturedDecl()->setNothrow();
9259   }
9260 
9261   OMPLoopDirective::HelperExprs B;
9262   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
9263   // define the nested loops number.
9264   unsigned NestedLoopCount =
9265       checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
9266                       getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
9267                       VarsWithImplicitDSA, B);
9268   if (NestedLoopCount == 0)
9269     return StmtError();
9270 
9271   assert((CurContext->isDependentContext() || B.builtAll()) &&
9272          "omp target parallel for loop exprs were not built");
9273 
9274   if (!CurContext->isDependentContext()) {
9275     // Finalize the clauses that need pre-built expressions for CodeGen.
9276     for (OMPClause *C : Clauses) {
9277       if (auto *LC = dyn_cast<OMPLinearClause>(C))
9278         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9279                                      B.NumIterations, *this, CurScope,
9280                                      DSAStack))
9281           return StmtError();
9282     }
9283   }
9284 
9285   setFunctionHasBranchProtectedScope();
9286   return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
9287                                                NestedLoopCount, Clauses, AStmt,
9288                                                B, DSAStack->isCancelRegion());
9289 }
9290 
9291 /// Check for existence of a map clause in the list of clauses.
9292 static bool hasClauses(ArrayRef<OMPClause *> Clauses,
9293                        const OpenMPClauseKind K) {
9294   return llvm::any_of(
9295       Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
9296 }
9297 
9298 template <typename... Params>
9299 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
9300                        const Params... ClauseTypes) {
9301   return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
9302 }
9303 
9304 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
9305                                                 Stmt *AStmt,
9306                                                 SourceLocation StartLoc,
9307                                                 SourceLocation EndLoc) {
9308   if (!AStmt)
9309     return StmtError();
9310 
9311   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9312 
9313   // OpenMP [2.10.1, Restrictions, p. 97]
9314   // At least one map clause must appear on the directive.
9315   if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
9316     Diag(StartLoc, diag::err_omp_no_clause_for_directive)
9317         << "'map' or 'use_device_ptr'"
9318         << getOpenMPDirectiveName(OMPD_target_data);
9319     return StmtError();
9320   }
9321 
9322   setFunctionHasBranchProtectedScope();
9323 
9324   return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
9325                                         AStmt);
9326 }
9327 
9328 StmtResult
9329 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
9330                                           SourceLocation StartLoc,
9331                                           SourceLocation EndLoc, Stmt *AStmt) {
9332   if (!AStmt)
9333     return StmtError();
9334 
9335   auto *CS = cast<CapturedStmt>(AStmt);
9336   // 1.2.2 OpenMP Language Terminology
9337   // Structured block - An executable statement with a single entry at the
9338   // top and a single exit at the bottom.
9339   // The point of exit cannot be a branch out of the structured block.
9340   // longjmp() and throw() must not violate the entry/exit criteria.
9341   CS->getCapturedDecl()->setNothrow();
9342   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
9343        ThisCaptureLevel > 1; --ThisCaptureLevel) {
9344     CS = cast<CapturedStmt>(CS->getCapturedStmt());
9345     // 1.2.2 OpenMP Language Terminology
9346     // Structured block - An executable statement with a single entry at the
9347     // top and a single exit at the bottom.
9348     // The point of exit cannot be a branch out of the structured block.
9349     // longjmp() and throw() must not violate the entry/exit criteria.
9350     CS->getCapturedDecl()->setNothrow();
9351   }
9352 
9353   // OpenMP [2.10.2, Restrictions, p. 99]
9354   // At least one map clause must appear on the directive.
9355   if (!hasClauses(Clauses, OMPC_map)) {
9356     Diag(StartLoc, diag::err_omp_no_clause_for_directive)
9357         << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
9358     return StmtError();
9359   }
9360 
9361   return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
9362                                              AStmt);
9363 }
9364 
9365 StmtResult
9366 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
9367                                          SourceLocation StartLoc,
9368                                          SourceLocation EndLoc, Stmt *AStmt) {
9369   if (!AStmt)
9370     return StmtError();
9371 
9372   auto *CS = cast<CapturedStmt>(AStmt);
9373   // 1.2.2 OpenMP Language Terminology
9374   // Structured block - An executable statement with a single entry at the
9375   // top and a single exit at the bottom.
9376   // The point of exit cannot be a branch out of the structured block.
9377   // longjmp() and throw() must not violate the entry/exit criteria.
9378   CS->getCapturedDecl()->setNothrow();
9379   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
9380        ThisCaptureLevel > 1; --ThisCaptureLevel) {
9381     CS = cast<CapturedStmt>(CS->getCapturedStmt());
9382     // 1.2.2 OpenMP Language Terminology
9383     // Structured block - An executable statement with a single entry at the
9384     // top and a single exit at the bottom.
9385     // The point of exit cannot be a branch out of the structured block.
9386     // longjmp() and throw() must not violate the entry/exit criteria.
9387     CS->getCapturedDecl()->setNothrow();
9388   }
9389 
9390   // OpenMP [2.10.3, Restrictions, p. 102]
9391   // At least one map clause must appear on the directive.
9392   if (!hasClauses(Clauses, OMPC_map)) {
9393     Diag(StartLoc, diag::err_omp_no_clause_for_directive)
9394         << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
9395     return StmtError();
9396   }
9397 
9398   return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
9399                                             AStmt);
9400 }
9401 
9402 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
9403                                                   SourceLocation StartLoc,
9404                                                   SourceLocation EndLoc,
9405                                                   Stmt *AStmt) {
9406   if (!AStmt)
9407     return StmtError();
9408 
9409   auto *CS = cast<CapturedStmt>(AStmt);
9410   // 1.2.2 OpenMP Language Terminology
9411   // Structured block - An executable statement with a single entry at the
9412   // top and a single exit at the bottom.
9413   // The point of exit cannot be a branch out of the structured block.
9414   // longjmp() and throw() must not violate the entry/exit criteria.
9415   CS->getCapturedDecl()->setNothrow();
9416   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
9417        ThisCaptureLevel > 1; --ThisCaptureLevel) {
9418     CS = cast<CapturedStmt>(CS->getCapturedStmt());
9419     // 1.2.2 OpenMP Language Terminology
9420     // Structured block - An executable statement with a single entry at the
9421     // top and a single exit at the bottom.
9422     // The point of exit cannot be a branch out of the structured block.
9423     // longjmp() and throw() must not violate the entry/exit criteria.
9424     CS->getCapturedDecl()->setNothrow();
9425   }
9426 
9427   if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
9428     Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
9429     return StmtError();
9430   }
9431   return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
9432                                           AStmt);
9433 }
9434 
9435 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
9436                                            Stmt *AStmt, SourceLocation StartLoc,
9437                                            SourceLocation EndLoc) {
9438   if (!AStmt)
9439     return StmtError();
9440 
9441   auto *CS = cast<CapturedStmt>(AStmt);
9442   // 1.2.2 OpenMP Language Terminology
9443   // Structured block - An executable statement with a single entry at the
9444   // top and a single exit at the bottom.
9445   // The point of exit cannot be a branch out of the structured block.
9446   // longjmp() and throw() must not violate the entry/exit criteria.
9447   CS->getCapturedDecl()->setNothrow();
9448 
9449   setFunctionHasBranchProtectedScope();
9450 
9451   DSAStack->setParentTeamsRegionLoc(StartLoc);
9452 
9453   return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
9454 }
9455 
9456 StmtResult
9457 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
9458                                             SourceLocation EndLoc,
9459                                             OpenMPDirectiveKind CancelRegion) {
9460   if (DSAStack->isParentNowaitRegion()) {
9461     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
9462     return StmtError();
9463   }
9464   if (DSAStack->isParentOrderedRegion()) {
9465     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
9466     return StmtError();
9467   }
9468   return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
9469                                                CancelRegion);
9470 }
9471 
9472 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
9473                                             SourceLocation StartLoc,
9474                                             SourceLocation EndLoc,
9475                                             OpenMPDirectiveKind CancelRegion) {
9476   if (DSAStack->isParentNowaitRegion()) {
9477     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
9478     return StmtError();
9479   }
9480   if (DSAStack->isParentOrderedRegion()) {
9481     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
9482     return StmtError();
9483   }
9484   DSAStack->setParentCancelRegion(/*Cancel=*/true);
9485   return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
9486                                     CancelRegion);
9487 }
9488 
9489 static bool checkGrainsizeNumTasksClauses(Sema &S,
9490                                           ArrayRef<OMPClause *> Clauses) {
9491   const OMPClause *PrevClause = nullptr;
9492   bool ErrorFound = false;
9493   for (const OMPClause *C : Clauses) {
9494     if (C->getClauseKind() == OMPC_grainsize ||
9495         C->getClauseKind() == OMPC_num_tasks) {
9496       if (!PrevClause)
9497         PrevClause = C;
9498       else if (PrevClause->getClauseKind() != C->getClauseKind()) {
9499         S.Diag(C->getBeginLoc(),
9500                diag::err_omp_grainsize_num_tasks_mutually_exclusive)
9501             << getOpenMPClauseName(C->getClauseKind())
9502             << getOpenMPClauseName(PrevClause->getClauseKind());
9503         S.Diag(PrevClause->getBeginLoc(),
9504                diag::note_omp_previous_grainsize_num_tasks)
9505             << getOpenMPClauseName(PrevClause->getClauseKind());
9506         ErrorFound = true;
9507       }
9508     }
9509   }
9510   return ErrorFound;
9511 }
9512 
9513 static bool checkReductionClauseWithNogroup(Sema &S,
9514                                             ArrayRef<OMPClause *> Clauses) {
9515   const OMPClause *ReductionClause = nullptr;
9516   const OMPClause *NogroupClause = nullptr;
9517   for (const OMPClause *C : Clauses) {
9518     if (C->getClauseKind() == OMPC_reduction) {
9519       ReductionClause = C;
9520       if (NogroupClause)
9521         break;
9522       continue;
9523     }
9524     if (C->getClauseKind() == OMPC_nogroup) {
9525       NogroupClause = C;
9526       if (ReductionClause)
9527         break;
9528       continue;
9529     }
9530   }
9531   if (ReductionClause && NogroupClause) {
9532     S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup)
9533         << SourceRange(NogroupClause->getBeginLoc(),
9534                        NogroupClause->getEndLoc());
9535     return true;
9536   }
9537   return false;
9538 }
9539 
9540 StmtResult Sema::ActOnOpenMPTaskLoopDirective(
9541     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9542     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9543   if (!AStmt)
9544     return StmtError();
9545 
9546   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9547   OMPLoopDirective::HelperExprs B;
9548   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
9549   // define the nested loops number.
9550   unsigned NestedLoopCount =
9551       checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
9552                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
9553                       VarsWithImplicitDSA, B);
9554   if (NestedLoopCount == 0)
9555     return StmtError();
9556 
9557   assert((CurContext->isDependentContext() || B.builtAll()) &&
9558          "omp for loop exprs were not built");
9559 
9560   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
9561   // The grainsize clause and num_tasks clause are mutually exclusive and may
9562   // not appear on the same taskloop directive.
9563   if (checkGrainsizeNumTasksClauses(*this, Clauses))
9564     return StmtError();
9565   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
9566   // If a reduction clause is present on the taskloop directive, the nogroup
9567   // clause must not be specified.
9568   if (checkReductionClauseWithNogroup(*this, Clauses))
9569     return StmtError();
9570 
9571   setFunctionHasBranchProtectedScope();
9572   return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
9573                                       NestedLoopCount, Clauses, AStmt, B);
9574 }
9575 
9576 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
9577     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9578     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9579   if (!AStmt)
9580     return StmtError();
9581 
9582   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9583   OMPLoopDirective::HelperExprs B;
9584   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
9585   // define the nested loops number.
9586   unsigned NestedLoopCount =
9587       checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
9588                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
9589                       VarsWithImplicitDSA, B);
9590   if (NestedLoopCount == 0)
9591     return StmtError();
9592 
9593   assert((CurContext->isDependentContext() || B.builtAll()) &&
9594          "omp for loop exprs were not built");
9595 
9596   if (!CurContext->isDependentContext()) {
9597     // Finalize the clauses that need pre-built expressions for CodeGen.
9598     for (OMPClause *C : Clauses) {
9599       if (auto *LC = dyn_cast<OMPLinearClause>(C))
9600         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9601                                      B.NumIterations, *this, CurScope,
9602                                      DSAStack))
9603           return StmtError();
9604     }
9605   }
9606 
9607   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
9608   // The grainsize clause and num_tasks clause are mutually exclusive and may
9609   // not appear on the same taskloop directive.
9610   if (checkGrainsizeNumTasksClauses(*this, Clauses))
9611     return StmtError();
9612   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
9613   // If a reduction clause is present on the taskloop directive, the nogroup
9614   // clause must not be specified.
9615   if (checkReductionClauseWithNogroup(*this, Clauses))
9616     return StmtError();
9617   if (checkSimdlenSafelenSpecified(*this, Clauses))
9618     return StmtError();
9619 
9620   setFunctionHasBranchProtectedScope();
9621   return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
9622                                           NestedLoopCount, Clauses, AStmt, B);
9623 }
9624 
9625 StmtResult Sema::ActOnOpenMPMasterTaskLoopDirective(
9626     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9627     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9628   if (!AStmt)
9629     return StmtError();
9630 
9631   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9632   OMPLoopDirective::HelperExprs B;
9633   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
9634   // define the nested loops number.
9635   unsigned NestedLoopCount =
9636       checkOpenMPLoop(OMPD_master_taskloop, getCollapseNumberExpr(Clauses),
9637                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
9638                       VarsWithImplicitDSA, B);
9639   if (NestedLoopCount == 0)
9640     return StmtError();
9641 
9642   assert((CurContext->isDependentContext() || B.builtAll()) &&
9643          "omp for loop exprs were not built");
9644 
9645   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
9646   // The grainsize clause and num_tasks clause are mutually exclusive and may
9647   // not appear on the same taskloop directive.
9648   if (checkGrainsizeNumTasksClauses(*this, Clauses))
9649     return StmtError();
9650   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
9651   // If a reduction clause is present on the taskloop directive, the nogroup
9652   // clause must not be specified.
9653   if (checkReductionClauseWithNogroup(*this, Clauses))
9654     return StmtError();
9655 
9656   setFunctionHasBranchProtectedScope();
9657   return OMPMasterTaskLoopDirective::Create(Context, StartLoc, EndLoc,
9658                                             NestedLoopCount, Clauses, AStmt, B);
9659 }
9660 
9661 StmtResult Sema::ActOnOpenMPMasterTaskLoopSimdDirective(
9662     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9663     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9664   if (!AStmt)
9665     return StmtError();
9666 
9667   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9668   OMPLoopDirective::HelperExprs B;
9669   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
9670   // define the nested loops number.
9671   unsigned NestedLoopCount =
9672       checkOpenMPLoop(OMPD_master_taskloop_simd, getCollapseNumberExpr(Clauses),
9673                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
9674                       VarsWithImplicitDSA, B);
9675   if (NestedLoopCount == 0)
9676     return StmtError();
9677 
9678   assert((CurContext->isDependentContext() || B.builtAll()) &&
9679          "omp for loop exprs were not built");
9680 
9681   if (!CurContext->isDependentContext()) {
9682     // Finalize the clauses that need pre-built expressions for CodeGen.
9683     for (OMPClause *C : Clauses) {
9684       if (auto *LC = dyn_cast<OMPLinearClause>(C))
9685         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9686                                      B.NumIterations, *this, CurScope,
9687                                      DSAStack))
9688           return StmtError();
9689     }
9690   }
9691 
9692   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
9693   // The grainsize clause and num_tasks clause are mutually exclusive and may
9694   // not appear on the same taskloop directive.
9695   if (checkGrainsizeNumTasksClauses(*this, Clauses))
9696     return StmtError();
9697   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
9698   // If a reduction clause is present on the taskloop directive, the nogroup
9699   // clause must not be specified.
9700   if (checkReductionClauseWithNogroup(*this, Clauses))
9701     return StmtError();
9702   if (checkSimdlenSafelenSpecified(*this, Clauses))
9703     return StmtError();
9704 
9705   setFunctionHasBranchProtectedScope();
9706   return OMPMasterTaskLoopSimdDirective::Create(
9707       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
9708 }
9709 
9710 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopDirective(
9711     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9712     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9713   if (!AStmt)
9714     return StmtError();
9715 
9716   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9717   auto *CS = cast<CapturedStmt>(AStmt);
9718   // 1.2.2 OpenMP Language Terminology
9719   // Structured block - An executable statement with a single entry at the
9720   // top and a single exit at the bottom.
9721   // The point of exit cannot be a branch out of the structured block.
9722   // longjmp() and throw() must not violate the entry/exit criteria.
9723   CS->getCapturedDecl()->setNothrow();
9724   for (int ThisCaptureLevel =
9725            getOpenMPCaptureLevels(OMPD_parallel_master_taskloop);
9726        ThisCaptureLevel > 1; --ThisCaptureLevel) {
9727     CS = cast<CapturedStmt>(CS->getCapturedStmt());
9728     // 1.2.2 OpenMP Language Terminology
9729     // Structured block - An executable statement with a single entry at the
9730     // top and a single exit at the bottom.
9731     // The point of exit cannot be a branch out of the structured block.
9732     // longjmp() and throw() must not violate the entry/exit criteria.
9733     CS->getCapturedDecl()->setNothrow();
9734   }
9735 
9736   OMPLoopDirective::HelperExprs B;
9737   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
9738   // define the nested loops number.
9739   unsigned NestedLoopCount = checkOpenMPLoop(
9740       OMPD_parallel_master_taskloop, getCollapseNumberExpr(Clauses),
9741       /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack,
9742       VarsWithImplicitDSA, B);
9743   if (NestedLoopCount == 0)
9744     return StmtError();
9745 
9746   assert((CurContext->isDependentContext() || B.builtAll()) &&
9747          "omp for loop exprs were not built");
9748 
9749   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
9750   // The grainsize clause and num_tasks clause are mutually exclusive and may
9751   // not appear on the same taskloop directive.
9752   if (checkGrainsizeNumTasksClauses(*this, Clauses))
9753     return StmtError();
9754   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
9755   // If a reduction clause is present on the taskloop directive, the nogroup
9756   // clause must not be specified.
9757   if (checkReductionClauseWithNogroup(*this, Clauses))
9758     return StmtError();
9759 
9760   setFunctionHasBranchProtectedScope();
9761   return OMPParallelMasterTaskLoopDirective::Create(
9762       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
9763 }
9764 
9765 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopSimdDirective(
9766     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9767     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9768   if (!AStmt)
9769     return StmtError();
9770 
9771   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9772   auto *CS = cast<CapturedStmt>(AStmt);
9773   // 1.2.2 OpenMP Language Terminology
9774   // Structured block - An executable statement with a single entry at the
9775   // top and a single exit at the bottom.
9776   // The point of exit cannot be a branch out of the structured block.
9777   // longjmp() and throw() must not violate the entry/exit criteria.
9778   CS->getCapturedDecl()->setNothrow();
9779   for (int ThisCaptureLevel =
9780            getOpenMPCaptureLevels(OMPD_parallel_master_taskloop_simd);
9781        ThisCaptureLevel > 1; --ThisCaptureLevel) {
9782     CS = cast<CapturedStmt>(CS->getCapturedStmt());
9783     // 1.2.2 OpenMP Language Terminology
9784     // Structured block - An executable statement with a single entry at the
9785     // top and a single exit at the bottom.
9786     // The point of exit cannot be a branch out of the structured block.
9787     // longjmp() and throw() must not violate the entry/exit criteria.
9788     CS->getCapturedDecl()->setNothrow();
9789   }
9790 
9791   OMPLoopDirective::HelperExprs B;
9792   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
9793   // define the nested loops number.
9794   unsigned NestedLoopCount = checkOpenMPLoop(
9795       OMPD_parallel_master_taskloop_simd, getCollapseNumberExpr(Clauses),
9796       /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack,
9797       VarsWithImplicitDSA, B);
9798   if (NestedLoopCount == 0)
9799     return StmtError();
9800 
9801   assert((CurContext->isDependentContext() || B.builtAll()) &&
9802          "omp for loop exprs were not built");
9803 
9804   if (!CurContext->isDependentContext()) {
9805     // Finalize the clauses that need pre-built expressions for CodeGen.
9806     for (OMPClause *C : Clauses) {
9807       if (auto *LC = dyn_cast<OMPLinearClause>(C))
9808         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9809                                      B.NumIterations, *this, CurScope,
9810                                      DSAStack))
9811           return StmtError();
9812     }
9813   }
9814 
9815   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
9816   // The grainsize clause and num_tasks clause are mutually exclusive and may
9817   // not appear on the same taskloop directive.
9818   if (checkGrainsizeNumTasksClauses(*this, Clauses))
9819     return StmtError();
9820   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
9821   // If a reduction clause is present on the taskloop directive, the nogroup
9822   // clause must not be specified.
9823   if (checkReductionClauseWithNogroup(*this, Clauses))
9824     return StmtError();
9825   if (checkSimdlenSafelenSpecified(*this, Clauses))
9826     return StmtError();
9827 
9828   setFunctionHasBranchProtectedScope();
9829   return OMPParallelMasterTaskLoopSimdDirective::Create(
9830       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
9831 }
9832 
9833 StmtResult Sema::ActOnOpenMPDistributeDirective(
9834     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9835     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9836   if (!AStmt)
9837     return StmtError();
9838 
9839   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9840   OMPLoopDirective::HelperExprs B;
9841   // In presence of clause 'collapse' with number of loops, it will
9842   // define the nested loops number.
9843   unsigned NestedLoopCount =
9844       checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
9845                       nullptr /*ordered not a clause on distribute*/, AStmt,
9846                       *this, *DSAStack, VarsWithImplicitDSA, B);
9847   if (NestedLoopCount == 0)
9848     return StmtError();
9849 
9850   assert((CurContext->isDependentContext() || B.builtAll()) &&
9851          "omp for loop exprs were not built");
9852 
9853   setFunctionHasBranchProtectedScope();
9854   return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
9855                                         NestedLoopCount, Clauses, AStmt, B);
9856 }
9857 
9858 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
9859     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9860     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9861   if (!AStmt)
9862     return StmtError();
9863 
9864   auto *CS = cast<CapturedStmt>(AStmt);
9865   // 1.2.2 OpenMP Language Terminology
9866   // Structured block - An executable statement with a single entry at the
9867   // top and a single exit at the bottom.
9868   // The point of exit cannot be a branch out of the structured block.
9869   // longjmp() and throw() must not violate the entry/exit criteria.
9870   CS->getCapturedDecl()->setNothrow();
9871   for (int ThisCaptureLevel =
9872            getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
9873        ThisCaptureLevel > 1; --ThisCaptureLevel) {
9874     CS = cast<CapturedStmt>(CS->getCapturedStmt());
9875     // 1.2.2 OpenMP Language Terminology
9876     // Structured block - An executable statement with a single entry at the
9877     // top and a single exit at the bottom.
9878     // The point of exit cannot be a branch out of the structured block.
9879     // longjmp() and throw() must not violate the entry/exit criteria.
9880     CS->getCapturedDecl()->setNothrow();
9881   }
9882 
9883   OMPLoopDirective::HelperExprs B;
9884   // In presence of clause 'collapse' with number of loops, it will
9885   // define the nested loops number.
9886   unsigned NestedLoopCount = checkOpenMPLoop(
9887       OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
9888       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
9889       VarsWithImplicitDSA, B);
9890   if (NestedLoopCount == 0)
9891     return StmtError();
9892 
9893   assert((CurContext->isDependentContext() || B.builtAll()) &&
9894          "omp for loop exprs were not built");
9895 
9896   setFunctionHasBranchProtectedScope();
9897   return OMPDistributeParallelForDirective::Create(
9898       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
9899       DSAStack->isCancelRegion());
9900 }
9901 
9902 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
9903     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9904     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9905   if (!AStmt)
9906     return StmtError();
9907 
9908   auto *CS = cast<CapturedStmt>(AStmt);
9909   // 1.2.2 OpenMP Language Terminology
9910   // Structured block - An executable statement with a single entry at the
9911   // top and a single exit at the bottom.
9912   // The point of exit cannot be a branch out of the structured block.
9913   // longjmp() and throw() must not violate the entry/exit criteria.
9914   CS->getCapturedDecl()->setNothrow();
9915   for (int ThisCaptureLevel =
9916            getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
9917        ThisCaptureLevel > 1; --ThisCaptureLevel) {
9918     CS = cast<CapturedStmt>(CS->getCapturedStmt());
9919     // 1.2.2 OpenMP Language Terminology
9920     // Structured block - An executable statement with a single entry at the
9921     // top and a single exit at the bottom.
9922     // The point of exit cannot be a branch out of the structured block.
9923     // longjmp() and throw() must not violate the entry/exit criteria.
9924     CS->getCapturedDecl()->setNothrow();
9925   }
9926 
9927   OMPLoopDirective::HelperExprs B;
9928   // In presence of clause 'collapse' with number of loops, it will
9929   // define the nested loops number.
9930   unsigned NestedLoopCount = checkOpenMPLoop(
9931       OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
9932       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
9933       VarsWithImplicitDSA, B);
9934   if (NestedLoopCount == 0)
9935     return StmtError();
9936 
9937   assert((CurContext->isDependentContext() || B.builtAll()) &&
9938          "omp for loop exprs were not built");
9939 
9940   if (!CurContext->isDependentContext()) {
9941     // Finalize the clauses that need pre-built expressions for CodeGen.
9942     for (OMPClause *C : Clauses) {
9943       if (auto *LC = dyn_cast<OMPLinearClause>(C))
9944         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9945                                      B.NumIterations, *this, CurScope,
9946                                      DSAStack))
9947           return StmtError();
9948     }
9949   }
9950 
9951   if (checkSimdlenSafelenSpecified(*this, Clauses))
9952     return StmtError();
9953 
9954   setFunctionHasBranchProtectedScope();
9955   return OMPDistributeParallelForSimdDirective::Create(
9956       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
9957 }
9958 
9959 StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
9960     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9961     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9962   if (!AStmt)
9963     return StmtError();
9964 
9965   auto *CS = cast<CapturedStmt>(AStmt);
9966   // 1.2.2 OpenMP Language Terminology
9967   // Structured block - An executable statement with a single entry at the
9968   // top and a single exit at the bottom.
9969   // The point of exit cannot be a branch out of the structured block.
9970   // longjmp() and throw() must not violate the entry/exit criteria.
9971   CS->getCapturedDecl()->setNothrow();
9972   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
9973        ThisCaptureLevel > 1; --ThisCaptureLevel) {
9974     CS = cast<CapturedStmt>(CS->getCapturedStmt());
9975     // 1.2.2 OpenMP Language Terminology
9976     // Structured block - An executable statement with a single entry at the
9977     // top and a single exit at the bottom.
9978     // The point of exit cannot be a branch out of the structured block.
9979     // longjmp() and throw() must not violate the entry/exit criteria.
9980     CS->getCapturedDecl()->setNothrow();
9981   }
9982 
9983   OMPLoopDirective::HelperExprs B;
9984   // In presence of clause 'collapse' with number of loops, it will
9985   // define the nested loops number.
9986   unsigned NestedLoopCount =
9987       checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
9988                       nullptr /*ordered not a clause on distribute*/, CS, *this,
9989                       *DSAStack, VarsWithImplicitDSA, B);
9990   if (NestedLoopCount == 0)
9991     return StmtError();
9992 
9993   assert((CurContext->isDependentContext() || B.builtAll()) &&
9994          "omp for loop exprs were not built");
9995 
9996   if (!CurContext->isDependentContext()) {
9997     // Finalize the clauses that need pre-built expressions for CodeGen.
9998     for (OMPClause *C : Clauses) {
9999       if (auto *LC = dyn_cast<OMPLinearClause>(C))
10000         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
10001                                      B.NumIterations, *this, CurScope,
10002                                      DSAStack))
10003           return StmtError();
10004     }
10005   }
10006 
10007   if (checkSimdlenSafelenSpecified(*this, Clauses))
10008     return StmtError();
10009 
10010   setFunctionHasBranchProtectedScope();
10011   return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
10012                                             NestedLoopCount, Clauses, AStmt, B);
10013 }
10014 
10015 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
10016     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10017     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10018   if (!AStmt)
10019     return StmtError();
10020 
10021   auto *CS = cast<CapturedStmt>(AStmt);
10022   // 1.2.2 OpenMP Language Terminology
10023   // Structured block - An executable statement with a single entry at the
10024   // top and a single exit at the bottom.
10025   // The point of exit cannot be a branch out of the structured block.
10026   // longjmp() and throw() must not violate the entry/exit criteria.
10027   CS->getCapturedDecl()->setNothrow();
10028   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
10029        ThisCaptureLevel > 1; --ThisCaptureLevel) {
10030     CS = cast<CapturedStmt>(CS->getCapturedStmt());
10031     // 1.2.2 OpenMP Language Terminology
10032     // Structured block - An executable statement with a single entry at the
10033     // top and a single exit at the bottom.
10034     // The point of exit cannot be a branch out of the structured block.
10035     // longjmp() and throw() must not violate the entry/exit criteria.
10036     CS->getCapturedDecl()->setNothrow();
10037   }
10038 
10039   OMPLoopDirective::HelperExprs B;
10040   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
10041   // define the nested loops number.
10042   unsigned NestedLoopCount = checkOpenMPLoop(
10043       OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
10044       getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
10045       VarsWithImplicitDSA, B);
10046   if (NestedLoopCount == 0)
10047     return StmtError();
10048 
10049   assert((CurContext->isDependentContext() || B.builtAll()) &&
10050          "omp target parallel for simd loop exprs were not built");
10051 
10052   if (!CurContext->isDependentContext()) {
10053     // Finalize the clauses that need pre-built expressions for CodeGen.
10054     for (OMPClause *C : Clauses) {
10055       if (auto *LC = dyn_cast<OMPLinearClause>(C))
10056         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
10057                                      B.NumIterations, *this, CurScope,
10058                                      DSAStack))
10059           return StmtError();
10060     }
10061   }
10062   if (checkSimdlenSafelenSpecified(*this, Clauses))
10063     return StmtError();
10064 
10065   setFunctionHasBranchProtectedScope();
10066   return OMPTargetParallelForSimdDirective::Create(
10067       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
10068 }
10069 
10070 StmtResult Sema::ActOnOpenMPTargetSimdDirective(
10071     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10072     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10073   if (!AStmt)
10074     return StmtError();
10075 
10076   auto *CS = cast<CapturedStmt>(AStmt);
10077   // 1.2.2 OpenMP Language Terminology
10078   // Structured block - An executable statement with a single entry at the
10079   // top and a single exit at the bottom.
10080   // The point of exit cannot be a branch out of the structured block.
10081   // longjmp() and throw() must not violate the entry/exit criteria.
10082   CS->getCapturedDecl()->setNothrow();
10083   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
10084        ThisCaptureLevel > 1; --ThisCaptureLevel) {
10085     CS = cast<CapturedStmt>(CS->getCapturedStmt());
10086     // 1.2.2 OpenMP Language Terminology
10087     // Structured block - An executable statement with a single entry at the
10088     // top and a single exit at the bottom.
10089     // The point of exit cannot be a branch out of the structured block.
10090     // longjmp() and throw() must not violate the entry/exit criteria.
10091     CS->getCapturedDecl()->setNothrow();
10092   }
10093 
10094   OMPLoopDirective::HelperExprs B;
10095   // In presence of clause 'collapse' with number of loops, it will define the
10096   // nested loops number.
10097   unsigned NestedLoopCount =
10098       checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
10099                       getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
10100                       VarsWithImplicitDSA, B);
10101   if (NestedLoopCount == 0)
10102     return StmtError();
10103 
10104   assert((CurContext->isDependentContext() || B.builtAll()) &&
10105          "omp target simd loop exprs were not built");
10106 
10107   if (!CurContext->isDependentContext()) {
10108     // Finalize the clauses that need pre-built expressions for CodeGen.
10109     for (OMPClause *C : Clauses) {
10110       if (auto *LC = dyn_cast<OMPLinearClause>(C))
10111         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
10112                                      B.NumIterations, *this, CurScope,
10113                                      DSAStack))
10114           return StmtError();
10115     }
10116   }
10117 
10118   if (checkSimdlenSafelenSpecified(*this, Clauses))
10119     return StmtError();
10120 
10121   setFunctionHasBranchProtectedScope();
10122   return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
10123                                         NestedLoopCount, Clauses, AStmt, B);
10124 }
10125 
10126 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
10127     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10128     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10129   if (!AStmt)
10130     return StmtError();
10131 
10132   auto *CS = cast<CapturedStmt>(AStmt);
10133   // 1.2.2 OpenMP Language Terminology
10134   // Structured block - An executable statement with a single entry at the
10135   // top and a single exit at the bottom.
10136   // The point of exit cannot be a branch out of the structured block.
10137   // longjmp() and throw() must not violate the entry/exit criteria.
10138   CS->getCapturedDecl()->setNothrow();
10139   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
10140        ThisCaptureLevel > 1; --ThisCaptureLevel) {
10141     CS = cast<CapturedStmt>(CS->getCapturedStmt());
10142     // 1.2.2 OpenMP Language Terminology
10143     // Structured block - An executable statement with a single entry at the
10144     // top and a single exit at the bottom.
10145     // The point of exit cannot be a branch out of the structured block.
10146     // longjmp() and throw() must not violate the entry/exit criteria.
10147     CS->getCapturedDecl()->setNothrow();
10148   }
10149 
10150   OMPLoopDirective::HelperExprs B;
10151   // In presence of clause 'collapse' with number of loops, it will
10152   // define the nested loops number.
10153   unsigned NestedLoopCount =
10154       checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
10155                       nullptr /*ordered not a clause on distribute*/, CS, *this,
10156                       *DSAStack, VarsWithImplicitDSA, B);
10157   if (NestedLoopCount == 0)
10158     return StmtError();
10159 
10160   assert((CurContext->isDependentContext() || B.builtAll()) &&
10161          "omp teams distribute loop exprs were not built");
10162 
10163   setFunctionHasBranchProtectedScope();
10164 
10165   DSAStack->setParentTeamsRegionLoc(StartLoc);
10166 
10167   return OMPTeamsDistributeDirective::Create(
10168       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
10169 }
10170 
10171 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
10172     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10173     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10174   if (!AStmt)
10175     return StmtError();
10176 
10177   auto *CS = cast<CapturedStmt>(AStmt);
10178   // 1.2.2 OpenMP Language Terminology
10179   // Structured block - An executable statement with a single entry at the
10180   // top and a single exit at the bottom.
10181   // The point of exit cannot be a branch out of the structured block.
10182   // longjmp() and throw() must not violate the entry/exit criteria.
10183   CS->getCapturedDecl()->setNothrow();
10184   for (int ThisCaptureLevel =
10185            getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
10186        ThisCaptureLevel > 1; --ThisCaptureLevel) {
10187     CS = cast<CapturedStmt>(CS->getCapturedStmt());
10188     // 1.2.2 OpenMP Language Terminology
10189     // Structured block - An executable statement with a single entry at the
10190     // top and a single exit at the bottom.
10191     // The point of exit cannot be a branch out of the structured block.
10192     // longjmp() and throw() must not violate the entry/exit criteria.
10193     CS->getCapturedDecl()->setNothrow();
10194   }
10195 
10196 
10197   OMPLoopDirective::HelperExprs B;
10198   // In presence of clause 'collapse' with number of loops, it will
10199   // define the nested loops number.
10200   unsigned NestedLoopCount = checkOpenMPLoop(
10201       OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
10202       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
10203       VarsWithImplicitDSA, B);
10204 
10205   if (NestedLoopCount == 0)
10206     return StmtError();
10207 
10208   assert((CurContext->isDependentContext() || B.builtAll()) &&
10209          "omp teams distribute simd loop exprs were not built");
10210 
10211   if (!CurContext->isDependentContext()) {
10212     // Finalize the clauses that need pre-built expressions for CodeGen.
10213     for (OMPClause *C : Clauses) {
10214       if (auto *LC = dyn_cast<OMPLinearClause>(C))
10215         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
10216                                      B.NumIterations, *this, CurScope,
10217                                      DSAStack))
10218           return StmtError();
10219     }
10220   }
10221 
10222   if (checkSimdlenSafelenSpecified(*this, Clauses))
10223     return StmtError();
10224 
10225   setFunctionHasBranchProtectedScope();
10226 
10227   DSAStack->setParentTeamsRegionLoc(StartLoc);
10228 
10229   return OMPTeamsDistributeSimdDirective::Create(
10230       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
10231 }
10232 
10233 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
10234     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10235     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10236   if (!AStmt)
10237     return StmtError();
10238 
10239   auto *CS = cast<CapturedStmt>(AStmt);
10240   // 1.2.2 OpenMP Language Terminology
10241   // Structured block - An executable statement with a single entry at the
10242   // top and a single exit at the bottom.
10243   // The point of exit cannot be a branch out of the structured block.
10244   // longjmp() and throw() must not violate the entry/exit criteria.
10245   CS->getCapturedDecl()->setNothrow();
10246 
10247   for (int ThisCaptureLevel =
10248            getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
10249        ThisCaptureLevel > 1; --ThisCaptureLevel) {
10250     CS = cast<CapturedStmt>(CS->getCapturedStmt());
10251     // 1.2.2 OpenMP Language Terminology
10252     // Structured block - An executable statement with a single entry at the
10253     // top and a single exit at the bottom.
10254     // The point of exit cannot be a branch out of the structured block.
10255     // longjmp() and throw() must not violate the entry/exit criteria.
10256     CS->getCapturedDecl()->setNothrow();
10257   }
10258 
10259   OMPLoopDirective::HelperExprs B;
10260   // In presence of clause 'collapse' with number of loops, it will
10261   // define the nested loops number.
10262   unsigned NestedLoopCount = checkOpenMPLoop(
10263       OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
10264       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
10265       VarsWithImplicitDSA, B);
10266 
10267   if (NestedLoopCount == 0)
10268     return StmtError();
10269 
10270   assert((CurContext->isDependentContext() || B.builtAll()) &&
10271          "omp for loop exprs were not built");
10272 
10273   if (!CurContext->isDependentContext()) {
10274     // Finalize the clauses that need pre-built expressions for CodeGen.
10275     for (OMPClause *C : Clauses) {
10276       if (auto *LC = dyn_cast<OMPLinearClause>(C))
10277         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
10278                                      B.NumIterations, *this, CurScope,
10279                                      DSAStack))
10280           return StmtError();
10281     }
10282   }
10283 
10284   if (checkSimdlenSafelenSpecified(*this, Clauses))
10285     return StmtError();
10286 
10287   setFunctionHasBranchProtectedScope();
10288 
10289   DSAStack->setParentTeamsRegionLoc(StartLoc);
10290 
10291   return OMPTeamsDistributeParallelForSimdDirective::Create(
10292       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
10293 }
10294 
10295 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
10296     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10297     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10298   if (!AStmt)
10299     return StmtError();
10300 
10301   auto *CS = cast<CapturedStmt>(AStmt);
10302   // 1.2.2 OpenMP Language Terminology
10303   // Structured block - An executable statement with a single entry at the
10304   // top and a single exit at the bottom.
10305   // The point of exit cannot be a branch out of the structured block.
10306   // longjmp() and throw() must not violate the entry/exit criteria.
10307   CS->getCapturedDecl()->setNothrow();
10308 
10309   for (int ThisCaptureLevel =
10310            getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
10311        ThisCaptureLevel > 1; --ThisCaptureLevel) {
10312     CS = cast<CapturedStmt>(CS->getCapturedStmt());
10313     // 1.2.2 OpenMP Language Terminology
10314     // Structured block - An executable statement with a single entry at the
10315     // top and a single exit at the bottom.
10316     // The point of exit cannot be a branch out of the structured block.
10317     // longjmp() and throw() must not violate the entry/exit criteria.
10318     CS->getCapturedDecl()->setNothrow();
10319   }
10320 
10321   OMPLoopDirective::HelperExprs B;
10322   // In presence of clause 'collapse' with number of loops, it will
10323   // define the nested loops number.
10324   unsigned NestedLoopCount = checkOpenMPLoop(
10325       OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
10326       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
10327       VarsWithImplicitDSA, B);
10328 
10329   if (NestedLoopCount == 0)
10330     return StmtError();
10331 
10332   assert((CurContext->isDependentContext() || B.builtAll()) &&
10333          "omp for loop exprs were not built");
10334 
10335   setFunctionHasBranchProtectedScope();
10336 
10337   DSAStack->setParentTeamsRegionLoc(StartLoc);
10338 
10339   return OMPTeamsDistributeParallelForDirective::Create(
10340       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
10341       DSAStack->isCancelRegion());
10342 }
10343 
10344 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
10345                                                  Stmt *AStmt,
10346                                                  SourceLocation StartLoc,
10347                                                  SourceLocation EndLoc) {
10348   if (!AStmt)
10349     return StmtError();
10350 
10351   auto *CS = cast<CapturedStmt>(AStmt);
10352   // 1.2.2 OpenMP Language Terminology
10353   // Structured block - An executable statement with a single entry at the
10354   // top and a single exit at the bottom.
10355   // The point of exit cannot be a branch out of the structured block.
10356   // longjmp() and throw() must not violate the entry/exit criteria.
10357   CS->getCapturedDecl()->setNothrow();
10358 
10359   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
10360        ThisCaptureLevel > 1; --ThisCaptureLevel) {
10361     CS = cast<CapturedStmt>(CS->getCapturedStmt());
10362     // 1.2.2 OpenMP Language Terminology
10363     // Structured block - An executable statement with a single entry at the
10364     // top and a single exit at the bottom.
10365     // The point of exit cannot be a branch out of the structured block.
10366     // longjmp() and throw() must not violate the entry/exit criteria.
10367     CS->getCapturedDecl()->setNothrow();
10368   }
10369   setFunctionHasBranchProtectedScope();
10370 
10371   return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
10372                                          AStmt);
10373 }
10374 
10375 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
10376     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10377     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10378   if (!AStmt)
10379     return StmtError();
10380 
10381   auto *CS = cast<CapturedStmt>(AStmt);
10382   // 1.2.2 OpenMP Language Terminology
10383   // Structured block - An executable statement with a single entry at the
10384   // top and a single exit at the bottom.
10385   // The point of exit cannot be a branch out of the structured block.
10386   // longjmp() and throw() must not violate the entry/exit criteria.
10387   CS->getCapturedDecl()->setNothrow();
10388   for (int ThisCaptureLevel =
10389            getOpenMPCaptureLevels(OMPD_target_teams_distribute);
10390        ThisCaptureLevel > 1; --ThisCaptureLevel) {
10391     CS = cast<CapturedStmt>(CS->getCapturedStmt());
10392     // 1.2.2 OpenMP Language Terminology
10393     // Structured block - An executable statement with a single entry at the
10394     // top and a single exit at the bottom.
10395     // The point of exit cannot be a branch out of the structured block.
10396     // longjmp() and throw() must not violate the entry/exit criteria.
10397     CS->getCapturedDecl()->setNothrow();
10398   }
10399 
10400   OMPLoopDirective::HelperExprs B;
10401   // In presence of clause 'collapse' with number of loops, it will
10402   // define the nested loops number.
10403   unsigned NestedLoopCount = checkOpenMPLoop(
10404       OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
10405       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
10406       VarsWithImplicitDSA, B);
10407   if (NestedLoopCount == 0)
10408     return StmtError();
10409 
10410   assert((CurContext->isDependentContext() || B.builtAll()) &&
10411          "omp target teams distribute loop exprs were not built");
10412 
10413   setFunctionHasBranchProtectedScope();
10414   return OMPTargetTeamsDistributeDirective::Create(
10415       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
10416 }
10417 
10418 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
10419     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10420     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10421   if (!AStmt)
10422     return StmtError();
10423 
10424   auto *CS = cast<CapturedStmt>(AStmt);
10425   // 1.2.2 OpenMP Language Terminology
10426   // Structured block - An executable statement with a single entry at the
10427   // top and a single exit at the bottom.
10428   // The point of exit cannot be a branch out of the structured block.
10429   // longjmp() and throw() must not violate the entry/exit criteria.
10430   CS->getCapturedDecl()->setNothrow();
10431   for (int ThisCaptureLevel =
10432            getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
10433        ThisCaptureLevel > 1; --ThisCaptureLevel) {
10434     CS = cast<CapturedStmt>(CS->getCapturedStmt());
10435     // 1.2.2 OpenMP Language Terminology
10436     // Structured block - An executable statement with a single entry at the
10437     // top and a single exit at the bottom.
10438     // The point of exit cannot be a branch out of the structured block.
10439     // longjmp() and throw() must not violate the entry/exit criteria.
10440     CS->getCapturedDecl()->setNothrow();
10441   }
10442 
10443   OMPLoopDirective::HelperExprs B;
10444   // In presence of clause 'collapse' with number of loops, it will
10445   // define the nested loops number.
10446   unsigned NestedLoopCount = checkOpenMPLoop(
10447       OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
10448       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
10449       VarsWithImplicitDSA, B);
10450   if (NestedLoopCount == 0)
10451     return StmtError();
10452 
10453   assert((CurContext->isDependentContext() || B.builtAll()) &&
10454          "omp target teams distribute parallel for loop exprs were not built");
10455 
10456   if (!CurContext->isDependentContext()) {
10457     // Finalize the clauses that need pre-built expressions for CodeGen.
10458     for (OMPClause *C : Clauses) {
10459       if (auto *LC = dyn_cast<OMPLinearClause>(C))
10460         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
10461                                      B.NumIterations, *this, CurScope,
10462                                      DSAStack))
10463           return StmtError();
10464     }
10465   }
10466 
10467   setFunctionHasBranchProtectedScope();
10468   return OMPTargetTeamsDistributeParallelForDirective::Create(
10469       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
10470       DSAStack->isCancelRegion());
10471 }
10472 
10473 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
10474     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10475     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10476   if (!AStmt)
10477     return StmtError();
10478 
10479   auto *CS = cast<CapturedStmt>(AStmt);
10480   // 1.2.2 OpenMP Language Terminology
10481   // Structured block - An executable statement with a single entry at the
10482   // top and a single exit at the bottom.
10483   // The point of exit cannot be a branch out of the structured block.
10484   // longjmp() and throw() must not violate the entry/exit criteria.
10485   CS->getCapturedDecl()->setNothrow();
10486   for (int ThisCaptureLevel = getOpenMPCaptureLevels(
10487            OMPD_target_teams_distribute_parallel_for_simd);
10488        ThisCaptureLevel > 1; --ThisCaptureLevel) {
10489     CS = cast<CapturedStmt>(CS->getCapturedStmt());
10490     // 1.2.2 OpenMP Language Terminology
10491     // Structured block - An executable statement with a single entry at the
10492     // top and a single exit at the bottom.
10493     // The point of exit cannot be a branch out of the structured block.
10494     // longjmp() and throw() must not violate the entry/exit criteria.
10495     CS->getCapturedDecl()->setNothrow();
10496   }
10497 
10498   OMPLoopDirective::HelperExprs B;
10499   // In presence of clause 'collapse' with number of loops, it will
10500   // define the nested loops number.
10501   unsigned NestedLoopCount =
10502       checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd,
10503                       getCollapseNumberExpr(Clauses),
10504                       nullptr /*ordered not a clause on distribute*/, CS, *this,
10505                       *DSAStack, VarsWithImplicitDSA, B);
10506   if (NestedLoopCount == 0)
10507     return StmtError();
10508 
10509   assert((CurContext->isDependentContext() || B.builtAll()) &&
10510          "omp target teams distribute parallel for simd loop exprs were not "
10511          "built");
10512 
10513   if (!CurContext->isDependentContext()) {
10514     // Finalize the clauses that need pre-built expressions for CodeGen.
10515     for (OMPClause *C : Clauses) {
10516       if (auto *LC = dyn_cast<OMPLinearClause>(C))
10517         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
10518                                      B.NumIterations, *this, CurScope,
10519                                      DSAStack))
10520           return StmtError();
10521     }
10522   }
10523 
10524   if (checkSimdlenSafelenSpecified(*this, Clauses))
10525     return StmtError();
10526 
10527   setFunctionHasBranchProtectedScope();
10528   return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
10529       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
10530 }
10531 
10532 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
10533     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10534     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10535   if (!AStmt)
10536     return StmtError();
10537 
10538   auto *CS = cast<CapturedStmt>(AStmt);
10539   // 1.2.2 OpenMP Language Terminology
10540   // Structured block - An executable statement with a single entry at the
10541   // top and a single exit at the bottom.
10542   // The point of exit cannot be a branch out of the structured block.
10543   // longjmp() and throw() must not violate the entry/exit criteria.
10544   CS->getCapturedDecl()->setNothrow();
10545   for (int ThisCaptureLevel =
10546            getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
10547        ThisCaptureLevel > 1; --ThisCaptureLevel) {
10548     CS = cast<CapturedStmt>(CS->getCapturedStmt());
10549     // 1.2.2 OpenMP Language Terminology
10550     // Structured block - An executable statement with a single entry at the
10551     // top and a single exit at the bottom.
10552     // The point of exit cannot be a branch out of the structured block.
10553     // longjmp() and throw() must not violate the entry/exit criteria.
10554     CS->getCapturedDecl()->setNothrow();
10555   }
10556 
10557   OMPLoopDirective::HelperExprs B;
10558   // In presence of clause 'collapse' with number of loops, it will
10559   // define the nested loops number.
10560   unsigned NestedLoopCount = checkOpenMPLoop(
10561       OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
10562       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
10563       VarsWithImplicitDSA, B);
10564   if (NestedLoopCount == 0)
10565     return StmtError();
10566 
10567   assert((CurContext->isDependentContext() || B.builtAll()) &&
10568          "omp target teams distribute simd loop exprs were not built");
10569 
10570   if (!CurContext->isDependentContext()) {
10571     // Finalize the clauses that need pre-built expressions for CodeGen.
10572     for (OMPClause *C : Clauses) {
10573       if (auto *LC = dyn_cast<OMPLinearClause>(C))
10574         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
10575                                      B.NumIterations, *this, CurScope,
10576                                      DSAStack))
10577           return StmtError();
10578     }
10579   }
10580 
10581   if (checkSimdlenSafelenSpecified(*this, Clauses))
10582     return StmtError();
10583 
10584   setFunctionHasBranchProtectedScope();
10585   return OMPTargetTeamsDistributeSimdDirective::Create(
10586       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
10587 }
10588 
10589 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
10590                                              SourceLocation StartLoc,
10591                                              SourceLocation LParenLoc,
10592                                              SourceLocation EndLoc) {
10593   OMPClause *Res = nullptr;
10594   switch (Kind) {
10595   case OMPC_final:
10596     Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
10597     break;
10598   case OMPC_num_threads:
10599     Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
10600     break;
10601   case OMPC_safelen:
10602     Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
10603     break;
10604   case OMPC_simdlen:
10605     Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
10606     break;
10607   case OMPC_allocator:
10608     Res = ActOnOpenMPAllocatorClause(Expr, StartLoc, LParenLoc, EndLoc);
10609     break;
10610   case OMPC_collapse:
10611     Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
10612     break;
10613   case OMPC_ordered:
10614     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
10615     break;
10616   case OMPC_device:
10617     Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
10618     break;
10619   case OMPC_num_teams:
10620     Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
10621     break;
10622   case OMPC_thread_limit:
10623     Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
10624     break;
10625   case OMPC_priority:
10626     Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
10627     break;
10628   case OMPC_grainsize:
10629     Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
10630     break;
10631   case OMPC_num_tasks:
10632     Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
10633     break;
10634   case OMPC_hint:
10635     Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
10636     break;
10637   case OMPC_if:
10638   case OMPC_default:
10639   case OMPC_proc_bind:
10640   case OMPC_schedule:
10641   case OMPC_private:
10642   case OMPC_firstprivate:
10643   case OMPC_lastprivate:
10644   case OMPC_shared:
10645   case OMPC_reduction:
10646   case OMPC_task_reduction:
10647   case OMPC_in_reduction:
10648   case OMPC_linear:
10649   case OMPC_aligned:
10650   case OMPC_copyin:
10651   case OMPC_copyprivate:
10652   case OMPC_nowait:
10653   case OMPC_untied:
10654   case OMPC_mergeable:
10655   case OMPC_threadprivate:
10656   case OMPC_allocate:
10657   case OMPC_flush:
10658   case OMPC_read:
10659   case OMPC_write:
10660   case OMPC_update:
10661   case OMPC_capture:
10662   case OMPC_seq_cst:
10663   case OMPC_depend:
10664   case OMPC_threads:
10665   case OMPC_simd:
10666   case OMPC_map:
10667   case OMPC_nogroup:
10668   case OMPC_dist_schedule:
10669   case OMPC_defaultmap:
10670   case OMPC_unknown:
10671   case OMPC_uniform:
10672   case OMPC_to:
10673   case OMPC_from:
10674   case OMPC_use_device_ptr:
10675   case OMPC_is_device_ptr:
10676   case OMPC_unified_address:
10677   case OMPC_unified_shared_memory:
10678   case OMPC_reverse_offload:
10679   case OMPC_dynamic_allocators:
10680   case OMPC_atomic_default_mem_order:
10681   case OMPC_device_type:
10682   case OMPC_match:
10683     llvm_unreachable("Clause is not allowed.");
10684   }
10685   return Res;
10686 }
10687 
10688 // An OpenMP directive such as 'target parallel' has two captured regions:
10689 // for the 'target' and 'parallel' respectively.  This function returns
10690 // the region in which to capture expressions associated with a clause.
10691 // A return value of OMPD_unknown signifies that the expression should not
10692 // be captured.
10693 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
10694     OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, unsigned OpenMPVersion,
10695     OpenMPDirectiveKind NameModifier = OMPD_unknown) {
10696   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
10697   switch (CKind) {
10698   case OMPC_if:
10699     switch (DKind) {
10700     case OMPD_target_parallel:
10701     case OMPD_target_parallel_for:
10702     case OMPD_target_parallel_for_simd:
10703       // If this clause applies to the nested 'parallel' region, capture within
10704       // the 'target' region, otherwise do not capture.
10705       if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
10706         CaptureRegion = OMPD_target;
10707       break;
10708     case OMPD_target_teams_distribute_parallel_for:
10709     case OMPD_target_teams_distribute_parallel_for_simd:
10710       // If this clause applies to the nested 'parallel' region, capture within
10711       // the 'teams' region, otherwise do not capture.
10712       if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
10713         CaptureRegion = OMPD_teams;
10714       break;
10715     case OMPD_teams_distribute_parallel_for:
10716     case OMPD_teams_distribute_parallel_for_simd:
10717       CaptureRegion = OMPD_teams;
10718       break;
10719     case OMPD_target_update:
10720     case OMPD_target_enter_data:
10721     case OMPD_target_exit_data:
10722       CaptureRegion = OMPD_task;
10723       break;
10724     case OMPD_parallel_master_taskloop:
10725       if (NameModifier == OMPD_unknown || NameModifier == OMPD_taskloop)
10726         CaptureRegion = OMPD_parallel;
10727       break;
10728     case OMPD_parallel_master_taskloop_simd:
10729       if ((OpenMPVersion <= 45 && NameModifier == OMPD_unknown) ||
10730           NameModifier == OMPD_taskloop) {
10731         CaptureRegion = OMPD_parallel;
10732         break;
10733       }
10734       if (OpenMPVersion <= 45)
10735         break;
10736       if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)
10737         CaptureRegion = OMPD_taskloop;
10738       break;
10739     case OMPD_parallel_for_simd:
10740       if (OpenMPVersion <= 45)
10741         break;
10742       if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)
10743         CaptureRegion = OMPD_parallel;
10744       break;
10745     case OMPD_taskloop_simd:
10746     case OMPD_master_taskloop_simd:
10747       if (OpenMPVersion <= 45)
10748         break;
10749       if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)
10750         CaptureRegion = OMPD_taskloop;
10751       break;
10752     case OMPD_distribute_parallel_for_simd:
10753       if (OpenMPVersion <= 45)
10754         break;
10755       if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)
10756         CaptureRegion = OMPD_parallel;
10757       break;
10758     case OMPD_cancel:
10759     case OMPD_parallel:
10760     case OMPD_parallel_master:
10761     case OMPD_parallel_sections:
10762     case OMPD_parallel_for:
10763     case OMPD_target:
10764     case OMPD_target_simd:
10765     case OMPD_target_teams:
10766     case OMPD_target_teams_distribute:
10767     case OMPD_target_teams_distribute_simd:
10768     case OMPD_distribute_parallel_for:
10769     case OMPD_task:
10770     case OMPD_taskloop:
10771     case OMPD_master_taskloop:
10772     case OMPD_target_data:
10773     case OMPD_simd:
10774     case OMPD_for_simd:
10775     case OMPD_distribute_simd:
10776       // Do not capture if-clause expressions.
10777       break;
10778     case OMPD_threadprivate:
10779     case OMPD_allocate:
10780     case OMPD_taskyield:
10781     case OMPD_barrier:
10782     case OMPD_taskwait:
10783     case OMPD_cancellation_point:
10784     case OMPD_flush:
10785     case OMPD_declare_reduction:
10786     case OMPD_declare_mapper:
10787     case OMPD_declare_simd:
10788     case OMPD_declare_variant:
10789     case OMPD_declare_target:
10790     case OMPD_end_declare_target:
10791     case OMPD_teams:
10792     case OMPD_for:
10793     case OMPD_sections:
10794     case OMPD_section:
10795     case OMPD_single:
10796     case OMPD_master:
10797     case OMPD_critical:
10798     case OMPD_taskgroup:
10799     case OMPD_distribute:
10800     case OMPD_ordered:
10801     case OMPD_atomic:
10802     case OMPD_teams_distribute:
10803     case OMPD_teams_distribute_simd:
10804     case OMPD_requires:
10805       llvm_unreachable("Unexpected OpenMP directive with if-clause");
10806     case OMPD_unknown:
10807       llvm_unreachable("Unknown OpenMP directive");
10808     }
10809     break;
10810   case OMPC_num_threads:
10811     switch (DKind) {
10812     case OMPD_target_parallel:
10813     case OMPD_target_parallel_for:
10814     case OMPD_target_parallel_for_simd:
10815       CaptureRegion = OMPD_target;
10816       break;
10817     case OMPD_teams_distribute_parallel_for:
10818     case OMPD_teams_distribute_parallel_for_simd:
10819     case OMPD_target_teams_distribute_parallel_for:
10820     case OMPD_target_teams_distribute_parallel_for_simd:
10821       CaptureRegion = OMPD_teams;
10822       break;
10823     case OMPD_parallel:
10824     case OMPD_parallel_master:
10825     case OMPD_parallel_sections:
10826     case OMPD_parallel_for:
10827     case OMPD_parallel_for_simd:
10828     case OMPD_distribute_parallel_for:
10829     case OMPD_distribute_parallel_for_simd:
10830     case OMPD_parallel_master_taskloop:
10831     case OMPD_parallel_master_taskloop_simd:
10832       // Do not capture num_threads-clause expressions.
10833       break;
10834     case OMPD_target_data:
10835     case OMPD_target_enter_data:
10836     case OMPD_target_exit_data:
10837     case OMPD_target_update:
10838     case OMPD_target:
10839     case OMPD_target_simd:
10840     case OMPD_target_teams:
10841     case OMPD_target_teams_distribute:
10842     case OMPD_target_teams_distribute_simd:
10843     case OMPD_cancel:
10844     case OMPD_task:
10845     case OMPD_taskloop:
10846     case OMPD_taskloop_simd:
10847     case OMPD_master_taskloop:
10848     case OMPD_master_taskloop_simd:
10849     case OMPD_threadprivate:
10850     case OMPD_allocate:
10851     case OMPD_taskyield:
10852     case OMPD_barrier:
10853     case OMPD_taskwait:
10854     case OMPD_cancellation_point:
10855     case OMPD_flush:
10856     case OMPD_declare_reduction:
10857     case OMPD_declare_mapper:
10858     case OMPD_declare_simd:
10859     case OMPD_declare_variant:
10860     case OMPD_declare_target:
10861     case OMPD_end_declare_target:
10862     case OMPD_teams:
10863     case OMPD_simd:
10864     case OMPD_for:
10865     case OMPD_for_simd:
10866     case OMPD_sections:
10867     case OMPD_section:
10868     case OMPD_single:
10869     case OMPD_master:
10870     case OMPD_critical:
10871     case OMPD_taskgroup:
10872     case OMPD_distribute:
10873     case OMPD_ordered:
10874     case OMPD_atomic:
10875     case OMPD_distribute_simd:
10876     case OMPD_teams_distribute:
10877     case OMPD_teams_distribute_simd:
10878     case OMPD_requires:
10879       llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
10880     case OMPD_unknown:
10881       llvm_unreachable("Unknown OpenMP directive");
10882     }
10883     break;
10884   case OMPC_num_teams:
10885     switch (DKind) {
10886     case OMPD_target_teams:
10887     case OMPD_target_teams_distribute:
10888     case OMPD_target_teams_distribute_simd:
10889     case OMPD_target_teams_distribute_parallel_for:
10890     case OMPD_target_teams_distribute_parallel_for_simd:
10891       CaptureRegion = OMPD_target;
10892       break;
10893     case OMPD_teams_distribute_parallel_for:
10894     case OMPD_teams_distribute_parallel_for_simd:
10895     case OMPD_teams:
10896     case OMPD_teams_distribute:
10897     case OMPD_teams_distribute_simd:
10898       // Do not capture num_teams-clause expressions.
10899       break;
10900     case OMPD_distribute_parallel_for:
10901     case OMPD_distribute_parallel_for_simd:
10902     case OMPD_task:
10903     case OMPD_taskloop:
10904     case OMPD_taskloop_simd:
10905     case OMPD_master_taskloop:
10906     case OMPD_master_taskloop_simd:
10907     case OMPD_parallel_master_taskloop:
10908     case OMPD_parallel_master_taskloop_simd:
10909     case OMPD_target_data:
10910     case OMPD_target_enter_data:
10911     case OMPD_target_exit_data:
10912     case OMPD_target_update:
10913     case OMPD_cancel:
10914     case OMPD_parallel:
10915     case OMPD_parallel_master:
10916     case OMPD_parallel_sections:
10917     case OMPD_parallel_for:
10918     case OMPD_parallel_for_simd:
10919     case OMPD_target:
10920     case OMPD_target_simd:
10921     case OMPD_target_parallel:
10922     case OMPD_target_parallel_for:
10923     case OMPD_target_parallel_for_simd:
10924     case OMPD_threadprivate:
10925     case OMPD_allocate:
10926     case OMPD_taskyield:
10927     case OMPD_barrier:
10928     case OMPD_taskwait:
10929     case OMPD_cancellation_point:
10930     case OMPD_flush:
10931     case OMPD_declare_reduction:
10932     case OMPD_declare_mapper:
10933     case OMPD_declare_simd:
10934     case OMPD_declare_variant:
10935     case OMPD_declare_target:
10936     case OMPD_end_declare_target:
10937     case OMPD_simd:
10938     case OMPD_for:
10939     case OMPD_for_simd:
10940     case OMPD_sections:
10941     case OMPD_section:
10942     case OMPD_single:
10943     case OMPD_master:
10944     case OMPD_critical:
10945     case OMPD_taskgroup:
10946     case OMPD_distribute:
10947     case OMPD_ordered:
10948     case OMPD_atomic:
10949     case OMPD_distribute_simd:
10950     case OMPD_requires:
10951       llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
10952     case OMPD_unknown:
10953       llvm_unreachable("Unknown OpenMP directive");
10954     }
10955     break;
10956   case OMPC_thread_limit:
10957     switch (DKind) {
10958     case OMPD_target_teams:
10959     case OMPD_target_teams_distribute:
10960     case OMPD_target_teams_distribute_simd:
10961     case OMPD_target_teams_distribute_parallel_for:
10962     case OMPD_target_teams_distribute_parallel_for_simd:
10963       CaptureRegion = OMPD_target;
10964       break;
10965     case OMPD_teams_distribute_parallel_for:
10966     case OMPD_teams_distribute_parallel_for_simd:
10967     case OMPD_teams:
10968     case OMPD_teams_distribute:
10969     case OMPD_teams_distribute_simd:
10970       // Do not capture thread_limit-clause expressions.
10971       break;
10972     case OMPD_distribute_parallel_for:
10973     case OMPD_distribute_parallel_for_simd:
10974     case OMPD_task:
10975     case OMPD_taskloop:
10976     case OMPD_taskloop_simd:
10977     case OMPD_master_taskloop:
10978     case OMPD_master_taskloop_simd:
10979     case OMPD_parallel_master_taskloop:
10980     case OMPD_parallel_master_taskloop_simd:
10981     case OMPD_target_data:
10982     case OMPD_target_enter_data:
10983     case OMPD_target_exit_data:
10984     case OMPD_target_update:
10985     case OMPD_cancel:
10986     case OMPD_parallel:
10987     case OMPD_parallel_master:
10988     case OMPD_parallel_sections:
10989     case OMPD_parallel_for:
10990     case OMPD_parallel_for_simd:
10991     case OMPD_target:
10992     case OMPD_target_simd:
10993     case OMPD_target_parallel:
10994     case OMPD_target_parallel_for:
10995     case OMPD_target_parallel_for_simd:
10996     case OMPD_threadprivate:
10997     case OMPD_allocate:
10998     case OMPD_taskyield:
10999     case OMPD_barrier:
11000     case OMPD_taskwait:
11001     case OMPD_cancellation_point:
11002     case OMPD_flush:
11003     case OMPD_declare_reduction:
11004     case OMPD_declare_mapper:
11005     case OMPD_declare_simd:
11006     case OMPD_declare_variant:
11007     case OMPD_declare_target:
11008     case OMPD_end_declare_target:
11009     case OMPD_simd:
11010     case OMPD_for:
11011     case OMPD_for_simd:
11012     case OMPD_sections:
11013     case OMPD_section:
11014     case OMPD_single:
11015     case OMPD_master:
11016     case OMPD_critical:
11017     case OMPD_taskgroup:
11018     case OMPD_distribute:
11019     case OMPD_ordered:
11020     case OMPD_atomic:
11021     case OMPD_distribute_simd:
11022     case OMPD_requires:
11023       llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
11024     case OMPD_unknown:
11025       llvm_unreachable("Unknown OpenMP directive");
11026     }
11027     break;
11028   case OMPC_schedule:
11029     switch (DKind) {
11030     case OMPD_parallel_for:
11031     case OMPD_parallel_for_simd:
11032     case OMPD_distribute_parallel_for:
11033     case OMPD_distribute_parallel_for_simd:
11034     case OMPD_teams_distribute_parallel_for:
11035     case OMPD_teams_distribute_parallel_for_simd:
11036     case OMPD_target_parallel_for:
11037     case OMPD_target_parallel_for_simd:
11038     case OMPD_target_teams_distribute_parallel_for:
11039     case OMPD_target_teams_distribute_parallel_for_simd:
11040       CaptureRegion = OMPD_parallel;
11041       break;
11042     case OMPD_for:
11043     case OMPD_for_simd:
11044       // Do not capture schedule-clause expressions.
11045       break;
11046     case OMPD_task:
11047     case OMPD_taskloop:
11048     case OMPD_taskloop_simd:
11049     case OMPD_master_taskloop:
11050     case OMPD_master_taskloop_simd:
11051     case OMPD_parallel_master_taskloop:
11052     case OMPD_parallel_master_taskloop_simd:
11053     case OMPD_target_data:
11054     case OMPD_target_enter_data:
11055     case OMPD_target_exit_data:
11056     case OMPD_target_update:
11057     case OMPD_teams:
11058     case OMPD_teams_distribute:
11059     case OMPD_teams_distribute_simd:
11060     case OMPD_target_teams_distribute:
11061     case OMPD_target_teams_distribute_simd:
11062     case OMPD_target:
11063     case OMPD_target_simd:
11064     case OMPD_target_parallel:
11065     case OMPD_cancel:
11066     case OMPD_parallel:
11067     case OMPD_parallel_master:
11068     case OMPD_parallel_sections:
11069     case OMPD_threadprivate:
11070     case OMPD_allocate:
11071     case OMPD_taskyield:
11072     case OMPD_barrier:
11073     case OMPD_taskwait:
11074     case OMPD_cancellation_point:
11075     case OMPD_flush:
11076     case OMPD_declare_reduction:
11077     case OMPD_declare_mapper:
11078     case OMPD_declare_simd:
11079     case OMPD_declare_variant:
11080     case OMPD_declare_target:
11081     case OMPD_end_declare_target:
11082     case OMPD_simd:
11083     case OMPD_sections:
11084     case OMPD_section:
11085     case OMPD_single:
11086     case OMPD_master:
11087     case OMPD_critical:
11088     case OMPD_taskgroup:
11089     case OMPD_distribute:
11090     case OMPD_ordered:
11091     case OMPD_atomic:
11092     case OMPD_distribute_simd:
11093     case OMPD_target_teams:
11094     case OMPD_requires:
11095       llvm_unreachable("Unexpected OpenMP directive with schedule clause");
11096     case OMPD_unknown:
11097       llvm_unreachable("Unknown OpenMP directive");
11098     }
11099     break;
11100   case OMPC_dist_schedule:
11101     switch (DKind) {
11102     case OMPD_teams_distribute_parallel_for:
11103     case OMPD_teams_distribute_parallel_for_simd:
11104     case OMPD_teams_distribute:
11105     case OMPD_teams_distribute_simd:
11106     case OMPD_target_teams_distribute_parallel_for:
11107     case OMPD_target_teams_distribute_parallel_for_simd:
11108     case OMPD_target_teams_distribute:
11109     case OMPD_target_teams_distribute_simd:
11110       CaptureRegion = OMPD_teams;
11111       break;
11112     case OMPD_distribute_parallel_for:
11113     case OMPD_distribute_parallel_for_simd:
11114     case OMPD_distribute:
11115     case OMPD_distribute_simd:
11116       // Do not capture thread_limit-clause expressions.
11117       break;
11118     case OMPD_parallel_for:
11119     case OMPD_parallel_for_simd:
11120     case OMPD_target_parallel_for_simd:
11121     case OMPD_target_parallel_for:
11122     case OMPD_task:
11123     case OMPD_taskloop:
11124     case OMPD_taskloop_simd:
11125     case OMPD_master_taskloop:
11126     case OMPD_master_taskloop_simd:
11127     case OMPD_parallel_master_taskloop:
11128     case OMPD_parallel_master_taskloop_simd:
11129     case OMPD_target_data:
11130     case OMPD_target_enter_data:
11131     case OMPD_target_exit_data:
11132     case OMPD_target_update:
11133     case OMPD_teams:
11134     case OMPD_target:
11135     case OMPD_target_simd:
11136     case OMPD_target_parallel:
11137     case OMPD_cancel:
11138     case OMPD_parallel:
11139     case OMPD_parallel_master:
11140     case OMPD_parallel_sections:
11141     case OMPD_threadprivate:
11142     case OMPD_allocate:
11143     case OMPD_taskyield:
11144     case OMPD_barrier:
11145     case OMPD_taskwait:
11146     case OMPD_cancellation_point:
11147     case OMPD_flush:
11148     case OMPD_declare_reduction:
11149     case OMPD_declare_mapper:
11150     case OMPD_declare_simd:
11151     case OMPD_declare_variant:
11152     case OMPD_declare_target:
11153     case OMPD_end_declare_target:
11154     case OMPD_simd:
11155     case OMPD_for:
11156     case OMPD_for_simd:
11157     case OMPD_sections:
11158     case OMPD_section:
11159     case OMPD_single:
11160     case OMPD_master:
11161     case OMPD_critical:
11162     case OMPD_taskgroup:
11163     case OMPD_ordered:
11164     case OMPD_atomic:
11165     case OMPD_target_teams:
11166     case OMPD_requires:
11167       llvm_unreachable("Unexpected OpenMP directive with schedule clause");
11168     case OMPD_unknown:
11169       llvm_unreachable("Unknown OpenMP directive");
11170     }
11171     break;
11172   case OMPC_device:
11173     switch (DKind) {
11174     case OMPD_target_update:
11175     case OMPD_target_enter_data:
11176     case OMPD_target_exit_data:
11177     case OMPD_target:
11178     case OMPD_target_simd:
11179     case OMPD_target_teams:
11180     case OMPD_target_parallel:
11181     case OMPD_target_teams_distribute:
11182     case OMPD_target_teams_distribute_simd:
11183     case OMPD_target_parallel_for:
11184     case OMPD_target_parallel_for_simd:
11185     case OMPD_target_teams_distribute_parallel_for:
11186     case OMPD_target_teams_distribute_parallel_for_simd:
11187       CaptureRegion = OMPD_task;
11188       break;
11189     case OMPD_target_data:
11190       // Do not capture device-clause expressions.
11191       break;
11192     case OMPD_teams_distribute_parallel_for:
11193     case OMPD_teams_distribute_parallel_for_simd:
11194     case OMPD_teams:
11195     case OMPD_teams_distribute:
11196     case OMPD_teams_distribute_simd:
11197     case OMPD_distribute_parallel_for:
11198     case OMPD_distribute_parallel_for_simd:
11199     case OMPD_task:
11200     case OMPD_taskloop:
11201     case OMPD_taskloop_simd:
11202     case OMPD_master_taskloop:
11203     case OMPD_master_taskloop_simd:
11204     case OMPD_parallel_master_taskloop:
11205     case OMPD_parallel_master_taskloop_simd:
11206     case OMPD_cancel:
11207     case OMPD_parallel:
11208     case OMPD_parallel_master:
11209     case OMPD_parallel_sections:
11210     case OMPD_parallel_for:
11211     case OMPD_parallel_for_simd:
11212     case OMPD_threadprivate:
11213     case OMPD_allocate:
11214     case OMPD_taskyield:
11215     case OMPD_barrier:
11216     case OMPD_taskwait:
11217     case OMPD_cancellation_point:
11218     case OMPD_flush:
11219     case OMPD_declare_reduction:
11220     case OMPD_declare_mapper:
11221     case OMPD_declare_simd:
11222     case OMPD_declare_variant:
11223     case OMPD_declare_target:
11224     case OMPD_end_declare_target:
11225     case OMPD_simd:
11226     case OMPD_for:
11227     case OMPD_for_simd:
11228     case OMPD_sections:
11229     case OMPD_section:
11230     case OMPD_single:
11231     case OMPD_master:
11232     case OMPD_critical:
11233     case OMPD_taskgroup:
11234     case OMPD_distribute:
11235     case OMPD_ordered:
11236     case OMPD_atomic:
11237     case OMPD_distribute_simd:
11238     case OMPD_requires:
11239       llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
11240     case OMPD_unknown:
11241       llvm_unreachable("Unknown OpenMP directive");
11242     }
11243     break;
11244   case OMPC_grainsize:
11245   case OMPC_num_tasks:
11246   case OMPC_final:
11247   case OMPC_priority:
11248     switch (DKind) {
11249     case OMPD_task:
11250     case OMPD_taskloop:
11251     case OMPD_taskloop_simd:
11252     case OMPD_master_taskloop:
11253     case OMPD_master_taskloop_simd:
11254       break;
11255     case OMPD_parallel_master_taskloop:
11256     case OMPD_parallel_master_taskloop_simd:
11257       CaptureRegion = OMPD_parallel;
11258       break;
11259     case OMPD_target_update:
11260     case OMPD_target_enter_data:
11261     case OMPD_target_exit_data:
11262     case OMPD_target:
11263     case OMPD_target_simd:
11264     case OMPD_target_teams:
11265     case OMPD_target_parallel:
11266     case OMPD_target_teams_distribute:
11267     case OMPD_target_teams_distribute_simd:
11268     case OMPD_target_parallel_for:
11269     case OMPD_target_parallel_for_simd:
11270     case OMPD_target_teams_distribute_parallel_for:
11271     case OMPD_target_teams_distribute_parallel_for_simd:
11272     case OMPD_target_data:
11273     case OMPD_teams_distribute_parallel_for:
11274     case OMPD_teams_distribute_parallel_for_simd:
11275     case OMPD_teams:
11276     case OMPD_teams_distribute:
11277     case OMPD_teams_distribute_simd:
11278     case OMPD_distribute_parallel_for:
11279     case OMPD_distribute_parallel_for_simd:
11280     case OMPD_cancel:
11281     case OMPD_parallel:
11282     case OMPD_parallel_master:
11283     case OMPD_parallel_sections:
11284     case OMPD_parallel_for:
11285     case OMPD_parallel_for_simd:
11286     case OMPD_threadprivate:
11287     case OMPD_allocate:
11288     case OMPD_taskyield:
11289     case OMPD_barrier:
11290     case OMPD_taskwait:
11291     case OMPD_cancellation_point:
11292     case OMPD_flush:
11293     case OMPD_declare_reduction:
11294     case OMPD_declare_mapper:
11295     case OMPD_declare_simd:
11296     case OMPD_declare_variant:
11297     case OMPD_declare_target:
11298     case OMPD_end_declare_target:
11299     case OMPD_simd:
11300     case OMPD_for:
11301     case OMPD_for_simd:
11302     case OMPD_sections:
11303     case OMPD_section:
11304     case OMPD_single:
11305     case OMPD_master:
11306     case OMPD_critical:
11307     case OMPD_taskgroup:
11308     case OMPD_distribute:
11309     case OMPD_ordered:
11310     case OMPD_atomic:
11311     case OMPD_distribute_simd:
11312     case OMPD_requires:
11313       llvm_unreachable("Unexpected OpenMP directive with grainsize-clause");
11314     case OMPD_unknown:
11315       llvm_unreachable("Unknown OpenMP directive");
11316     }
11317     break;
11318   case OMPC_firstprivate:
11319   case OMPC_lastprivate:
11320   case OMPC_reduction:
11321   case OMPC_task_reduction:
11322   case OMPC_in_reduction:
11323   case OMPC_linear:
11324   case OMPC_default:
11325   case OMPC_proc_bind:
11326   case OMPC_safelen:
11327   case OMPC_simdlen:
11328   case OMPC_allocator:
11329   case OMPC_collapse:
11330   case OMPC_private:
11331   case OMPC_shared:
11332   case OMPC_aligned:
11333   case OMPC_copyin:
11334   case OMPC_copyprivate:
11335   case OMPC_ordered:
11336   case OMPC_nowait:
11337   case OMPC_untied:
11338   case OMPC_mergeable:
11339   case OMPC_threadprivate:
11340   case OMPC_allocate:
11341   case OMPC_flush:
11342   case OMPC_read:
11343   case OMPC_write:
11344   case OMPC_update:
11345   case OMPC_capture:
11346   case OMPC_seq_cst:
11347   case OMPC_depend:
11348   case OMPC_threads:
11349   case OMPC_simd:
11350   case OMPC_map:
11351   case OMPC_nogroup:
11352   case OMPC_hint:
11353   case OMPC_defaultmap:
11354   case OMPC_unknown:
11355   case OMPC_uniform:
11356   case OMPC_to:
11357   case OMPC_from:
11358   case OMPC_use_device_ptr:
11359   case OMPC_is_device_ptr:
11360   case OMPC_unified_address:
11361   case OMPC_unified_shared_memory:
11362   case OMPC_reverse_offload:
11363   case OMPC_dynamic_allocators:
11364   case OMPC_atomic_default_mem_order:
11365   case OMPC_device_type:
11366   case OMPC_match:
11367     llvm_unreachable("Unexpected OpenMP clause.");
11368   }
11369   return CaptureRegion;
11370 }
11371 
11372 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
11373                                      Expr *Condition, SourceLocation StartLoc,
11374                                      SourceLocation LParenLoc,
11375                                      SourceLocation NameModifierLoc,
11376                                      SourceLocation ColonLoc,
11377                                      SourceLocation EndLoc) {
11378   Expr *ValExpr = Condition;
11379   Stmt *HelperValStmt = nullptr;
11380   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
11381   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
11382       !Condition->isInstantiationDependent() &&
11383       !Condition->containsUnexpandedParameterPack()) {
11384     ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
11385     if (Val.isInvalid())
11386       return nullptr;
11387 
11388     ValExpr = Val.get();
11389 
11390     OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
11391     CaptureRegion = getOpenMPCaptureRegionForClause(
11392         DKind, OMPC_if, LangOpts.OpenMP, NameModifier);
11393     if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
11394       ValExpr = MakeFullExpr(ValExpr).get();
11395       llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
11396       ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11397       HelperValStmt = buildPreInits(Context, Captures);
11398     }
11399   }
11400 
11401   return new (Context)
11402       OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
11403                   LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
11404 }
11405 
11406 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
11407                                         SourceLocation StartLoc,
11408                                         SourceLocation LParenLoc,
11409                                         SourceLocation EndLoc) {
11410   Expr *ValExpr = Condition;
11411   Stmt *HelperValStmt = nullptr;
11412   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
11413   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
11414       !Condition->isInstantiationDependent() &&
11415       !Condition->containsUnexpandedParameterPack()) {
11416     ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
11417     if (Val.isInvalid())
11418       return nullptr;
11419 
11420     ValExpr = MakeFullExpr(Val.get()).get();
11421 
11422     OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
11423     CaptureRegion =
11424         getOpenMPCaptureRegionForClause(DKind, OMPC_final, LangOpts.OpenMP);
11425     if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
11426       ValExpr = MakeFullExpr(ValExpr).get();
11427       llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
11428       ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11429       HelperValStmt = buildPreInits(Context, Captures);
11430     }
11431   }
11432 
11433   return new (Context) OMPFinalClause(ValExpr, HelperValStmt, CaptureRegion,
11434                                       StartLoc, LParenLoc, EndLoc);
11435 }
11436 
11437 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
11438                                                         Expr *Op) {
11439   if (!Op)
11440     return ExprError();
11441 
11442   class IntConvertDiagnoser : public ICEConvertDiagnoser {
11443   public:
11444     IntConvertDiagnoser()
11445         : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
11446     SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
11447                                          QualType T) override {
11448       return S.Diag(Loc, diag::err_omp_not_integral) << T;
11449     }
11450     SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
11451                                              QualType T) override {
11452       return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
11453     }
11454     SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
11455                                                QualType T,
11456                                                QualType ConvTy) override {
11457       return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
11458     }
11459     SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
11460                                            QualType ConvTy) override {
11461       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
11462              << ConvTy->isEnumeralType() << ConvTy;
11463     }
11464     SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
11465                                             QualType T) override {
11466       return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
11467     }
11468     SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
11469                                         QualType ConvTy) override {
11470       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
11471              << ConvTy->isEnumeralType() << ConvTy;
11472     }
11473     SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
11474                                              QualType) override {
11475       llvm_unreachable("conversion functions are permitted");
11476     }
11477   } ConvertDiagnoser;
11478   return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
11479 }
11480 
11481 static bool
11482 isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, OpenMPClauseKind CKind,
11483                           bool StrictlyPositive, bool BuildCapture = false,
11484                           OpenMPDirectiveKind DKind = OMPD_unknown,
11485                           OpenMPDirectiveKind *CaptureRegion = nullptr,
11486                           Stmt **HelperValStmt = nullptr) {
11487   if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
11488       !ValExpr->isInstantiationDependent()) {
11489     SourceLocation Loc = ValExpr->getExprLoc();
11490     ExprResult Value =
11491         SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
11492     if (Value.isInvalid())
11493       return false;
11494 
11495     ValExpr = Value.get();
11496     // The expression must evaluate to a non-negative integer value.
11497     llvm::APSInt Result;
11498     if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
11499         Result.isSigned() &&
11500         !((!StrictlyPositive && Result.isNonNegative()) ||
11501           (StrictlyPositive && Result.isStrictlyPositive()))) {
11502       SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
11503           << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
11504           << ValExpr->getSourceRange();
11505       return false;
11506     }
11507     if (!BuildCapture)
11508       return true;
11509     *CaptureRegion =
11510         getOpenMPCaptureRegionForClause(DKind, CKind, SemaRef.LangOpts.OpenMP);
11511     if (*CaptureRegion != OMPD_unknown &&
11512         !SemaRef.CurContext->isDependentContext()) {
11513       ValExpr = SemaRef.MakeFullExpr(ValExpr).get();
11514       llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
11515       ValExpr = tryBuildCapture(SemaRef, ValExpr, Captures).get();
11516       *HelperValStmt = buildPreInits(SemaRef.Context, Captures);
11517     }
11518   }
11519   return true;
11520 }
11521 
11522 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
11523                                              SourceLocation StartLoc,
11524                                              SourceLocation LParenLoc,
11525                                              SourceLocation EndLoc) {
11526   Expr *ValExpr = NumThreads;
11527   Stmt *HelperValStmt = nullptr;
11528 
11529   // OpenMP [2.5, Restrictions]
11530   //  The num_threads expression must evaluate to a positive integer value.
11531   if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
11532                                  /*StrictlyPositive=*/true))
11533     return nullptr;
11534 
11535   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
11536   OpenMPDirectiveKind CaptureRegion =
11537       getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads, LangOpts.OpenMP);
11538   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
11539     ValExpr = MakeFullExpr(ValExpr).get();
11540     llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
11541     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11542     HelperValStmt = buildPreInits(Context, Captures);
11543   }
11544 
11545   return new (Context) OMPNumThreadsClause(
11546       ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
11547 }
11548 
11549 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
11550                                                        OpenMPClauseKind CKind,
11551                                                        bool StrictlyPositive) {
11552   if (!E)
11553     return ExprError();
11554   if (E->isValueDependent() || E->isTypeDependent() ||
11555       E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
11556     return E;
11557   llvm::APSInt Result;
11558   ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
11559   if (ICE.isInvalid())
11560     return ExprError();
11561   if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
11562       (!StrictlyPositive && !Result.isNonNegative())) {
11563     Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
11564         << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
11565         << E->getSourceRange();
11566     return ExprError();
11567   }
11568   if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
11569     Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
11570         << E->getSourceRange();
11571     return ExprError();
11572   }
11573   if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
11574     DSAStack->setAssociatedLoops(Result.getExtValue());
11575   else if (CKind == OMPC_ordered)
11576     DSAStack->setAssociatedLoops(Result.getExtValue());
11577   return ICE;
11578 }
11579 
11580 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
11581                                           SourceLocation LParenLoc,
11582                                           SourceLocation EndLoc) {
11583   // OpenMP [2.8.1, simd construct, Description]
11584   // The parameter of the safelen clause must be a constant
11585   // positive integer expression.
11586   ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
11587   if (Safelen.isInvalid())
11588     return nullptr;
11589   return new (Context)
11590       OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
11591 }
11592 
11593 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
11594                                           SourceLocation LParenLoc,
11595                                           SourceLocation EndLoc) {
11596   // OpenMP [2.8.1, simd construct, Description]
11597   // The parameter of the simdlen clause must be a constant
11598   // positive integer expression.
11599   ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
11600   if (Simdlen.isInvalid())
11601     return nullptr;
11602   return new (Context)
11603       OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
11604 }
11605 
11606 /// Tries to find omp_allocator_handle_t type.
11607 static bool findOMPAllocatorHandleT(Sema &S, SourceLocation Loc,
11608                                     DSAStackTy *Stack) {
11609   QualType OMPAllocatorHandleT = Stack->getOMPAllocatorHandleT();
11610   if (!OMPAllocatorHandleT.isNull())
11611     return true;
11612   // Build the predefined allocator expressions.
11613   bool ErrorFound = false;
11614   for (int I = OMPAllocateDeclAttr::OMPDefaultMemAlloc;
11615        I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
11616     auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
11617     StringRef Allocator =
11618         OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind);
11619     DeclarationName AllocatorName = &S.getASTContext().Idents.get(Allocator);
11620     auto *VD = dyn_cast_or_null<ValueDecl>(
11621         S.LookupSingleName(S.TUScope, AllocatorName, Loc, Sema::LookupAnyName));
11622     if (!VD) {
11623       ErrorFound = true;
11624       break;
11625     }
11626     QualType AllocatorType =
11627         VD->getType().getNonLValueExprType(S.getASTContext());
11628     ExprResult Res = S.BuildDeclRefExpr(VD, AllocatorType, VK_LValue, Loc);
11629     if (!Res.isUsable()) {
11630       ErrorFound = true;
11631       break;
11632     }
11633     if (OMPAllocatorHandleT.isNull())
11634       OMPAllocatorHandleT = AllocatorType;
11635     if (!S.getASTContext().hasSameType(OMPAllocatorHandleT, AllocatorType)) {
11636       ErrorFound = true;
11637       break;
11638     }
11639     Stack->setAllocator(AllocatorKind, Res.get());
11640   }
11641   if (ErrorFound) {
11642     S.Diag(Loc, diag::err_implied_omp_allocator_handle_t_not_found);
11643     return false;
11644   }
11645   OMPAllocatorHandleT.addConst();
11646   Stack->setOMPAllocatorHandleT(OMPAllocatorHandleT);
11647   return true;
11648 }
11649 
11650 OMPClause *Sema::ActOnOpenMPAllocatorClause(Expr *A, SourceLocation StartLoc,
11651                                             SourceLocation LParenLoc,
11652                                             SourceLocation EndLoc) {
11653   // OpenMP [2.11.3, allocate Directive, Description]
11654   // allocator is an expression of omp_allocator_handle_t type.
11655   if (!findOMPAllocatorHandleT(*this, A->getExprLoc(), DSAStack))
11656     return nullptr;
11657 
11658   ExprResult Allocator = DefaultLvalueConversion(A);
11659   if (Allocator.isInvalid())
11660     return nullptr;
11661   Allocator = PerformImplicitConversion(Allocator.get(),
11662                                         DSAStack->getOMPAllocatorHandleT(),
11663                                         Sema::AA_Initializing,
11664                                         /*AllowExplicit=*/true);
11665   if (Allocator.isInvalid())
11666     return nullptr;
11667   return new (Context)
11668       OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc);
11669 }
11670 
11671 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
11672                                            SourceLocation StartLoc,
11673                                            SourceLocation LParenLoc,
11674                                            SourceLocation EndLoc) {
11675   // OpenMP [2.7.1, loop construct, Description]
11676   // OpenMP [2.8.1, simd construct, Description]
11677   // OpenMP [2.9.6, distribute construct, Description]
11678   // The parameter of the collapse clause must be a constant
11679   // positive integer expression.
11680   ExprResult NumForLoopsResult =
11681       VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
11682   if (NumForLoopsResult.isInvalid())
11683     return nullptr;
11684   return new (Context)
11685       OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
11686 }
11687 
11688 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
11689                                           SourceLocation EndLoc,
11690                                           SourceLocation LParenLoc,
11691                                           Expr *NumForLoops) {
11692   // OpenMP [2.7.1, loop construct, Description]
11693   // OpenMP [2.8.1, simd construct, Description]
11694   // OpenMP [2.9.6, distribute construct, Description]
11695   // The parameter of the ordered clause must be a constant
11696   // positive integer expression if any.
11697   if (NumForLoops && LParenLoc.isValid()) {
11698     ExprResult NumForLoopsResult =
11699         VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
11700     if (NumForLoopsResult.isInvalid())
11701       return nullptr;
11702     NumForLoops = NumForLoopsResult.get();
11703   } else {
11704     NumForLoops = nullptr;
11705   }
11706   auto *Clause = OMPOrderedClause::Create(
11707       Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0,
11708       StartLoc, LParenLoc, EndLoc);
11709   DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause);
11710   return Clause;
11711 }
11712 
11713 OMPClause *Sema::ActOnOpenMPSimpleClause(
11714     OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
11715     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
11716   OMPClause *Res = nullptr;
11717   switch (Kind) {
11718   case OMPC_default:
11719     Res =
11720         ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
11721                                  ArgumentLoc, StartLoc, LParenLoc, EndLoc);
11722     break;
11723   case OMPC_proc_bind:
11724     Res = ActOnOpenMPProcBindClause(
11725         static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
11726         LParenLoc, EndLoc);
11727     break;
11728   case OMPC_atomic_default_mem_order:
11729     Res = ActOnOpenMPAtomicDefaultMemOrderClause(
11730         static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument),
11731         ArgumentLoc, StartLoc, LParenLoc, EndLoc);
11732     break;
11733   case OMPC_if:
11734   case OMPC_final:
11735   case OMPC_num_threads:
11736   case OMPC_safelen:
11737   case OMPC_simdlen:
11738   case OMPC_allocator:
11739   case OMPC_collapse:
11740   case OMPC_schedule:
11741   case OMPC_private:
11742   case OMPC_firstprivate:
11743   case OMPC_lastprivate:
11744   case OMPC_shared:
11745   case OMPC_reduction:
11746   case OMPC_task_reduction:
11747   case OMPC_in_reduction:
11748   case OMPC_linear:
11749   case OMPC_aligned:
11750   case OMPC_copyin:
11751   case OMPC_copyprivate:
11752   case OMPC_ordered:
11753   case OMPC_nowait:
11754   case OMPC_untied:
11755   case OMPC_mergeable:
11756   case OMPC_threadprivate:
11757   case OMPC_allocate:
11758   case OMPC_flush:
11759   case OMPC_read:
11760   case OMPC_write:
11761   case OMPC_update:
11762   case OMPC_capture:
11763   case OMPC_seq_cst:
11764   case OMPC_depend:
11765   case OMPC_device:
11766   case OMPC_threads:
11767   case OMPC_simd:
11768   case OMPC_map:
11769   case OMPC_num_teams:
11770   case OMPC_thread_limit:
11771   case OMPC_priority:
11772   case OMPC_grainsize:
11773   case OMPC_nogroup:
11774   case OMPC_num_tasks:
11775   case OMPC_hint:
11776   case OMPC_dist_schedule:
11777   case OMPC_defaultmap:
11778   case OMPC_unknown:
11779   case OMPC_uniform:
11780   case OMPC_to:
11781   case OMPC_from:
11782   case OMPC_use_device_ptr:
11783   case OMPC_is_device_ptr:
11784   case OMPC_unified_address:
11785   case OMPC_unified_shared_memory:
11786   case OMPC_reverse_offload:
11787   case OMPC_dynamic_allocators:
11788   case OMPC_device_type:
11789   case OMPC_match:
11790     llvm_unreachable("Clause is not allowed.");
11791   }
11792   return Res;
11793 }
11794 
11795 static std::string
11796 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
11797                         ArrayRef<unsigned> Exclude = llvm::None) {
11798   SmallString<256> Buffer;
11799   llvm::raw_svector_ostream Out(Buffer);
11800   unsigned Bound = Last >= 2 ? Last - 2 : 0;
11801   unsigned Skipped = Exclude.size();
11802   auto S = Exclude.begin(), E = Exclude.end();
11803   for (unsigned I = First; I < Last; ++I) {
11804     if (std::find(S, E, I) != E) {
11805       --Skipped;
11806       continue;
11807     }
11808     Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'";
11809     if (I == Bound - Skipped)
11810       Out << " or ";
11811     else if (I != Bound + 1 - Skipped)
11812       Out << ", ";
11813   }
11814   return Out.str();
11815 }
11816 
11817 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
11818                                           SourceLocation KindKwLoc,
11819                                           SourceLocation StartLoc,
11820                                           SourceLocation LParenLoc,
11821                                           SourceLocation EndLoc) {
11822   if (Kind == OMPC_DEFAULT_unknown) {
11823     static_assert(OMPC_DEFAULT_unknown > 0,
11824                   "OMPC_DEFAULT_unknown not greater than 0");
11825     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
11826         << getListOfPossibleValues(OMPC_default, /*First=*/0,
11827                                    /*Last=*/OMPC_DEFAULT_unknown)
11828         << getOpenMPClauseName(OMPC_default);
11829     return nullptr;
11830   }
11831   switch (Kind) {
11832   case OMPC_DEFAULT_none:
11833     DSAStack->setDefaultDSANone(KindKwLoc);
11834     break;
11835   case OMPC_DEFAULT_shared:
11836     DSAStack->setDefaultDSAShared(KindKwLoc);
11837     break;
11838   case OMPC_DEFAULT_unknown:
11839     llvm_unreachable("Clause kind is not allowed.");
11840     break;
11841   }
11842   return new (Context)
11843       OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
11844 }
11845 
11846 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
11847                                            SourceLocation KindKwLoc,
11848                                            SourceLocation StartLoc,
11849                                            SourceLocation LParenLoc,
11850                                            SourceLocation EndLoc) {
11851   if (Kind == OMPC_PROC_BIND_unknown) {
11852     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
11853         << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
11854                                    /*Last=*/OMPC_PROC_BIND_unknown)
11855         << getOpenMPClauseName(OMPC_proc_bind);
11856     return nullptr;
11857   }
11858   return new (Context)
11859       OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
11860 }
11861 
11862 OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause(
11863     OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc,
11864     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
11865   if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) {
11866     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
11867         << getListOfPossibleValues(
11868                OMPC_atomic_default_mem_order, /*First=*/0,
11869                /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown)
11870         << getOpenMPClauseName(OMPC_atomic_default_mem_order);
11871     return nullptr;
11872   }
11873   return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc,
11874                                                       LParenLoc, EndLoc);
11875 }
11876 
11877 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
11878     OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
11879     SourceLocation StartLoc, SourceLocation LParenLoc,
11880     ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
11881     SourceLocation EndLoc) {
11882   OMPClause *Res = nullptr;
11883   switch (Kind) {
11884   case OMPC_schedule:
11885     enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
11886     assert(Argument.size() == NumberOfElements &&
11887            ArgumentLoc.size() == NumberOfElements);
11888     Res = ActOnOpenMPScheduleClause(
11889         static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
11890         static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
11891         static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
11892         StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
11893         ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
11894     break;
11895   case OMPC_if:
11896     assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
11897     Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
11898                               Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
11899                               DelimLoc, EndLoc);
11900     break;
11901   case OMPC_dist_schedule:
11902     Res = ActOnOpenMPDistScheduleClause(
11903         static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
11904         StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
11905     break;
11906   case OMPC_defaultmap:
11907     enum { Modifier, DefaultmapKind };
11908     Res = ActOnOpenMPDefaultmapClause(
11909         static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
11910         static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
11911         StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
11912         EndLoc);
11913     break;
11914   case OMPC_final:
11915   case OMPC_num_threads:
11916   case OMPC_safelen:
11917   case OMPC_simdlen:
11918   case OMPC_allocator:
11919   case OMPC_collapse:
11920   case OMPC_default:
11921   case OMPC_proc_bind:
11922   case OMPC_private:
11923   case OMPC_firstprivate:
11924   case OMPC_lastprivate:
11925   case OMPC_shared:
11926   case OMPC_reduction:
11927   case OMPC_task_reduction:
11928   case OMPC_in_reduction:
11929   case OMPC_linear:
11930   case OMPC_aligned:
11931   case OMPC_copyin:
11932   case OMPC_copyprivate:
11933   case OMPC_ordered:
11934   case OMPC_nowait:
11935   case OMPC_untied:
11936   case OMPC_mergeable:
11937   case OMPC_threadprivate:
11938   case OMPC_allocate:
11939   case OMPC_flush:
11940   case OMPC_read:
11941   case OMPC_write:
11942   case OMPC_update:
11943   case OMPC_capture:
11944   case OMPC_seq_cst:
11945   case OMPC_depend:
11946   case OMPC_device:
11947   case OMPC_threads:
11948   case OMPC_simd:
11949   case OMPC_map:
11950   case OMPC_num_teams:
11951   case OMPC_thread_limit:
11952   case OMPC_priority:
11953   case OMPC_grainsize:
11954   case OMPC_nogroup:
11955   case OMPC_num_tasks:
11956   case OMPC_hint:
11957   case OMPC_unknown:
11958   case OMPC_uniform:
11959   case OMPC_to:
11960   case OMPC_from:
11961   case OMPC_use_device_ptr:
11962   case OMPC_is_device_ptr:
11963   case OMPC_unified_address:
11964   case OMPC_unified_shared_memory:
11965   case OMPC_reverse_offload:
11966   case OMPC_dynamic_allocators:
11967   case OMPC_atomic_default_mem_order:
11968   case OMPC_device_type:
11969   case OMPC_match:
11970     llvm_unreachable("Clause is not allowed.");
11971   }
11972   return Res;
11973 }
11974 
11975 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
11976                                    OpenMPScheduleClauseModifier M2,
11977                                    SourceLocation M1Loc, SourceLocation M2Loc) {
11978   if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
11979     SmallVector<unsigned, 2> Excluded;
11980     if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
11981       Excluded.push_back(M2);
11982     if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
11983       Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
11984     if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
11985       Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
11986     S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
11987         << getListOfPossibleValues(OMPC_schedule,
11988                                    /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
11989                                    /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
11990                                    Excluded)
11991         << getOpenMPClauseName(OMPC_schedule);
11992     return true;
11993   }
11994   return false;
11995 }
11996 
11997 OMPClause *Sema::ActOnOpenMPScheduleClause(
11998     OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
11999     OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
12000     SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
12001     SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
12002   if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
12003       checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
12004     return nullptr;
12005   // OpenMP, 2.7.1, Loop Construct, Restrictions
12006   // Either the monotonic modifier or the nonmonotonic modifier can be specified
12007   // but not both.
12008   if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
12009       (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
12010        M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
12011       (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
12012        M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
12013     Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
12014         << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
12015         << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
12016     return nullptr;
12017   }
12018   if (Kind == OMPC_SCHEDULE_unknown) {
12019     std::string Values;
12020     if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
12021       unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
12022       Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
12023                                        /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
12024                                        Exclude);
12025     } else {
12026       Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
12027                                        /*Last=*/OMPC_SCHEDULE_unknown);
12028     }
12029     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
12030         << Values << getOpenMPClauseName(OMPC_schedule);
12031     return nullptr;
12032   }
12033   // OpenMP, 2.7.1, Loop Construct, Restrictions
12034   // The nonmonotonic modifier can only be specified with schedule(dynamic) or
12035   // schedule(guided).
12036   if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
12037        M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
12038       Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
12039     Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
12040          diag::err_omp_schedule_nonmonotonic_static);
12041     return nullptr;
12042   }
12043   Expr *ValExpr = ChunkSize;
12044   Stmt *HelperValStmt = nullptr;
12045   if (ChunkSize) {
12046     if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
12047         !ChunkSize->isInstantiationDependent() &&
12048         !ChunkSize->containsUnexpandedParameterPack()) {
12049       SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
12050       ExprResult Val =
12051           PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
12052       if (Val.isInvalid())
12053         return nullptr;
12054 
12055       ValExpr = Val.get();
12056 
12057       // OpenMP [2.7.1, Restrictions]
12058       //  chunk_size must be a loop invariant integer expression with a positive
12059       //  value.
12060       llvm::APSInt Result;
12061       if (ValExpr->isIntegerConstantExpr(Result, Context)) {
12062         if (Result.isSigned() && !Result.isStrictlyPositive()) {
12063           Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
12064               << "schedule" << 1 << ChunkSize->getSourceRange();
12065           return nullptr;
12066         }
12067       } else if (getOpenMPCaptureRegionForClause(
12068                      DSAStack->getCurrentDirective(), OMPC_schedule,
12069                      LangOpts.OpenMP) != OMPD_unknown &&
12070                  !CurContext->isDependentContext()) {
12071         ValExpr = MakeFullExpr(ValExpr).get();
12072         llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
12073         ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12074         HelperValStmt = buildPreInits(Context, Captures);
12075       }
12076     }
12077   }
12078 
12079   return new (Context)
12080       OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
12081                         ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
12082 }
12083 
12084 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
12085                                    SourceLocation StartLoc,
12086                                    SourceLocation EndLoc) {
12087   OMPClause *Res = nullptr;
12088   switch (Kind) {
12089   case OMPC_ordered:
12090     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
12091     break;
12092   case OMPC_nowait:
12093     Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
12094     break;
12095   case OMPC_untied:
12096     Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
12097     break;
12098   case OMPC_mergeable:
12099     Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
12100     break;
12101   case OMPC_read:
12102     Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
12103     break;
12104   case OMPC_write:
12105     Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
12106     break;
12107   case OMPC_update:
12108     Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
12109     break;
12110   case OMPC_capture:
12111     Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
12112     break;
12113   case OMPC_seq_cst:
12114     Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
12115     break;
12116   case OMPC_threads:
12117     Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
12118     break;
12119   case OMPC_simd:
12120     Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
12121     break;
12122   case OMPC_nogroup:
12123     Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
12124     break;
12125   case OMPC_unified_address:
12126     Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc);
12127     break;
12128   case OMPC_unified_shared_memory:
12129     Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
12130     break;
12131   case OMPC_reverse_offload:
12132     Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc);
12133     break;
12134   case OMPC_dynamic_allocators:
12135     Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc);
12136     break;
12137   case OMPC_if:
12138   case OMPC_final:
12139   case OMPC_num_threads:
12140   case OMPC_safelen:
12141   case OMPC_simdlen:
12142   case OMPC_allocator:
12143   case OMPC_collapse:
12144   case OMPC_schedule:
12145   case OMPC_private:
12146   case OMPC_firstprivate:
12147   case OMPC_lastprivate:
12148   case OMPC_shared:
12149   case OMPC_reduction:
12150   case OMPC_task_reduction:
12151   case OMPC_in_reduction:
12152   case OMPC_linear:
12153   case OMPC_aligned:
12154   case OMPC_copyin:
12155   case OMPC_copyprivate:
12156   case OMPC_default:
12157   case OMPC_proc_bind:
12158   case OMPC_threadprivate:
12159   case OMPC_allocate:
12160   case OMPC_flush:
12161   case OMPC_depend:
12162   case OMPC_device:
12163   case OMPC_map:
12164   case OMPC_num_teams:
12165   case OMPC_thread_limit:
12166   case OMPC_priority:
12167   case OMPC_grainsize:
12168   case OMPC_num_tasks:
12169   case OMPC_hint:
12170   case OMPC_dist_schedule:
12171   case OMPC_defaultmap:
12172   case OMPC_unknown:
12173   case OMPC_uniform:
12174   case OMPC_to:
12175   case OMPC_from:
12176   case OMPC_use_device_ptr:
12177   case OMPC_is_device_ptr:
12178   case OMPC_atomic_default_mem_order:
12179   case OMPC_device_type:
12180   case OMPC_match:
12181     llvm_unreachable("Clause is not allowed.");
12182   }
12183   return Res;
12184 }
12185 
12186 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
12187                                          SourceLocation EndLoc) {
12188   DSAStack->setNowaitRegion();
12189   return new (Context) OMPNowaitClause(StartLoc, EndLoc);
12190 }
12191 
12192 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
12193                                          SourceLocation EndLoc) {
12194   return new (Context) OMPUntiedClause(StartLoc, EndLoc);
12195 }
12196 
12197 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
12198                                             SourceLocation EndLoc) {
12199   return new (Context) OMPMergeableClause(StartLoc, EndLoc);
12200 }
12201 
12202 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
12203                                        SourceLocation EndLoc) {
12204   return new (Context) OMPReadClause(StartLoc, EndLoc);
12205 }
12206 
12207 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
12208                                         SourceLocation EndLoc) {
12209   return new (Context) OMPWriteClause(StartLoc, EndLoc);
12210 }
12211 
12212 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
12213                                          SourceLocation EndLoc) {
12214   return new (Context) OMPUpdateClause(StartLoc, EndLoc);
12215 }
12216 
12217 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
12218                                           SourceLocation EndLoc) {
12219   return new (Context) OMPCaptureClause(StartLoc, EndLoc);
12220 }
12221 
12222 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
12223                                          SourceLocation EndLoc) {
12224   return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
12225 }
12226 
12227 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
12228                                           SourceLocation EndLoc) {
12229   return new (Context) OMPThreadsClause(StartLoc, EndLoc);
12230 }
12231 
12232 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
12233                                        SourceLocation EndLoc) {
12234   return new (Context) OMPSIMDClause(StartLoc, EndLoc);
12235 }
12236 
12237 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
12238                                           SourceLocation EndLoc) {
12239   return new (Context) OMPNogroupClause(StartLoc, EndLoc);
12240 }
12241 
12242 OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc,
12243                                                  SourceLocation EndLoc) {
12244   return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc);
12245 }
12246 
12247 OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc,
12248                                                       SourceLocation EndLoc) {
12249   return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
12250 }
12251 
12252 OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc,
12253                                                  SourceLocation EndLoc) {
12254   return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc);
12255 }
12256 
12257 OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc,
12258                                                     SourceLocation EndLoc) {
12259   return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc);
12260 }
12261 
12262 OMPClause *Sema::ActOnOpenMPVarListClause(
12263     OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
12264     const OMPVarListLocTy &Locs, SourceLocation ColonLoc,
12265     CXXScopeSpec &ReductionOrMapperIdScopeSpec,
12266     DeclarationNameInfo &ReductionOrMapperId, OpenMPDependClauseKind DepKind,
12267     OpenMPLinearClauseKind LinKind,
12268     ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
12269     ArrayRef<SourceLocation> MapTypeModifiersLoc, OpenMPMapClauseKind MapType,
12270     bool IsMapTypeImplicit, SourceLocation DepLinMapLoc) {
12271   SourceLocation StartLoc = Locs.StartLoc;
12272   SourceLocation LParenLoc = Locs.LParenLoc;
12273   SourceLocation EndLoc = Locs.EndLoc;
12274   OMPClause *Res = nullptr;
12275   switch (Kind) {
12276   case OMPC_private:
12277     Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
12278     break;
12279   case OMPC_firstprivate:
12280     Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
12281     break;
12282   case OMPC_lastprivate:
12283     Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
12284     break;
12285   case OMPC_shared:
12286     Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
12287     break;
12288   case OMPC_reduction:
12289     Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
12290                                      EndLoc, ReductionOrMapperIdScopeSpec,
12291                                      ReductionOrMapperId);
12292     break;
12293   case OMPC_task_reduction:
12294     Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
12295                                          EndLoc, ReductionOrMapperIdScopeSpec,
12296                                          ReductionOrMapperId);
12297     break;
12298   case OMPC_in_reduction:
12299     Res = ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
12300                                        EndLoc, ReductionOrMapperIdScopeSpec,
12301                                        ReductionOrMapperId);
12302     break;
12303   case OMPC_linear:
12304     Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
12305                                   LinKind, DepLinMapLoc, ColonLoc, EndLoc);
12306     break;
12307   case OMPC_aligned:
12308     Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
12309                                    ColonLoc, EndLoc);
12310     break;
12311   case OMPC_copyin:
12312     Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
12313     break;
12314   case OMPC_copyprivate:
12315     Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
12316     break;
12317   case OMPC_flush:
12318     Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
12319     break;
12320   case OMPC_depend:
12321     Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
12322                                   StartLoc, LParenLoc, EndLoc);
12323     break;
12324   case OMPC_map:
12325     Res = ActOnOpenMPMapClause(MapTypeModifiers, MapTypeModifiersLoc,
12326                                ReductionOrMapperIdScopeSpec,
12327                                ReductionOrMapperId, MapType, IsMapTypeImplicit,
12328                                DepLinMapLoc, ColonLoc, VarList, Locs);
12329     break;
12330   case OMPC_to:
12331     Res = ActOnOpenMPToClause(VarList, ReductionOrMapperIdScopeSpec,
12332                               ReductionOrMapperId, Locs);
12333     break;
12334   case OMPC_from:
12335     Res = ActOnOpenMPFromClause(VarList, ReductionOrMapperIdScopeSpec,
12336                                 ReductionOrMapperId, Locs);
12337     break;
12338   case OMPC_use_device_ptr:
12339     Res = ActOnOpenMPUseDevicePtrClause(VarList, Locs);
12340     break;
12341   case OMPC_is_device_ptr:
12342     Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs);
12343     break;
12344   case OMPC_allocate:
12345     Res = ActOnOpenMPAllocateClause(TailExpr, VarList, StartLoc, LParenLoc,
12346                                     ColonLoc, EndLoc);
12347     break;
12348   case OMPC_if:
12349   case OMPC_final:
12350   case OMPC_num_threads:
12351   case OMPC_safelen:
12352   case OMPC_simdlen:
12353   case OMPC_allocator:
12354   case OMPC_collapse:
12355   case OMPC_default:
12356   case OMPC_proc_bind:
12357   case OMPC_schedule:
12358   case OMPC_ordered:
12359   case OMPC_nowait:
12360   case OMPC_untied:
12361   case OMPC_mergeable:
12362   case OMPC_threadprivate:
12363   case OMPC_read:
12364   case OMPC_write:
12365   case OMPC_update:
12366   case OMPC_capture:
12367   case OMPC_seq_cst:
12368   case OMPC_device:
12369   case OMPC_threads:
12370   case OMPC_simd:
12371   case OMPC_num_teams:
12372   case OMPC_thread_limit:
12373   case OMPC_priority:
12374   case OMPC_grainsize:
12375   case OMPC_nogroup:
12376   case OMPC_num_tasks:
12377   case OMPC_hint:
12378   case OMPC_dist_schedule:
12379   case OMPC_defaultmap:
12380   case OMPC_unknown:
12381   case OMPC_uniform:
12382   case OMPC_unified_address:
12383   case OMPC_unified_shared_memory:
12384   case OMPC_reverse_offload:
12385   case OMPC_dynamic_allocators:
12386   case OMPC_atomic_default_mem_order:
12387   case OMPC_device_type:
12388   case OMPC_match:
12389     llvm_unreachable("Clause is not allowed.");
12390   }
12391   return Res;
12392 }
12393 
12394 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
12395                                        ExprObjectKind OK, SourceLocation Loc) {
12396   ExprResult Res = BuildDeclRefExpr(
12397       Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
12398   if (!Res.isUsable())
12399     return ExprError();
12400   if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
12401     Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
12402     if (!Res.isUsable())
12403       return ExprError();
12404   }
12405   if (VK != VK_LValue && Res.get()->isGLValue()) {
12406     Res = DefaultLvalueConversion(Res.get());
12407     if (!Res.isUsable())
12408       return ExprError();
12409   }
12410   return Res;
12411 }
12412 
12413 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
12414                                           SourceLocation StartLoc,
12415                                           SourceLocation LParenLoc,
12416                                           SourceLocation EndLoc) {
12417   SmallVector<Expr *, 8> Vars;
12418   SmallVector<Expr *, 8> PrivateCopies;
12419   for (Expr *RefExpr : VarList) {
12420     assert(RefExpr && "NULL expr in OpenMP private clause.");
12421     SourceLocation ELoc;
12422     SourceRange ERange;
12423     Expr *SimpleRefExpr = RefExpr;
12424     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
12425     if (Res.second) {
12426       // It will be analyzed later.
12427       Vars.push_back(RefExpr);
12428       PrivateCopies.push_back(nullptr);
12429     }
12430     ValueDecl *D = Res.first;
12431     if (!D)
12432       continue;
12433 
12434     QualType Type = D->getType();
12435     auto *VD = dyn_cast<VarDecl>(D);
12436 
12437     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
12438     //  A variable that appears in a private clause must not have an incomplete
12439     //  type or a reference type.
12440     if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
12441       continue;
12442     Type = Type.getNonReferenceType();
12443 
12444     // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
12445     // A variable that is privatized must not have a const-qualified type
12446     // unless it is of class type with a mutable member. This restriction does
12447     // not apply to the firstprivate clause.
12448     //
12449     // OpenMP 3.1 [2.9.3.3, private clause, Restrictions]
12450     // A variable that appears in a private clause must not have a
12451     // const-qualified type unless it is of class type with a mutable member.
12452     if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc))
12453       continue;
12454 
12455     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
12456     // in a Construct]
12457     //  Variables with the predetermined data-sharing attributes may not be
12458     //  listed in data-sharing attributes clauses, except for the cases
12459     //  listed below. For these exceptions only, listing a predetermined
12460     //  variable in a data-sharing attribute clause is allowed and overrides
12461     //  the variable's predetermined data-sharing attributes.
12462     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
12463     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
12464       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
12465                                           << getOpenMPClauseName(OMPC_private);
12466       reportOriginalDsa(*this, DSAStack, D, DVar);
12467       continue;
12468     }
12469 
12470     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
12471     // Variably modified types are not supported for tasks.
12472     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
12473         isOpenMPTaskingDirective(CurrDir)) {
12474       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
12475           << getOpenMPClauseName(OMPC_private) << Type
12476           << getOpenMPDirectiveName(CurrDir);
12477       bool IsDecl =
12478           !VD ||
12479           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
12480       Diag(D->getLocation(),
12481            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
12482           << D;
12483       continue;
12484     }
12485 
12486     // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
12487     // A list item cannot appear in both a map clause and a data-sharing
12488     // attribute clause on the same construct
12489     //
12490     // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
12491     // A list item cannot appear in both a map clause and a data-sharing
12492     // attribute clause on the same construct unless the construct is a
12493     // combined construct.
12494     if ((LangOpts.OpenMP <= 45 && isOpenMPTargetExecutionDirective(CurrDir)) ||
12495         CurrDir == OMPD_target) {
12496       OpenMPClauseKind ConflictKind;
12497       if (DSAStack->checkMappableExprComponentListsForDecl(
12498               VD, /*CurrentRegionOnly=*/true,
12499               [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
12500                   OpenMPClauseKind WhereFoundClauseKind) -> bool {
12501                 ConflictKind = WhereFoundClauseKind;
12502                 return true;
12503               })) {
12504         Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
12505             << getOpenMPClauseName(OMPC_private)
12506             << getOpenMPClauseName(ConflictKind)
12507             << getOpenMPDirectiveName(CurrDir);
12508         reportOriginalDsa(*this, DSAStack, D, DVar);
12509         continue;
12510       }
12511     }
12512 
12513     // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
12514     //  A variable of class type (or array thereof) that appears in a private
12515     //  clause requires an accessible, unambiguous default constructor for the
12516     //  class type.
12517     // Generate helper private variable and initialize it with the default
12518     // value. The address of the original variable is replaced by the address of
12519     // the new private variable in CodeGen. This new variable is not added to
12520     // IdResolver, so the code in the OpenMP region uses original variable for
12521     // proper diagnostics.
12522     Type = Type.getUnqualifiedType();
12523     VarDecl *VDPrivate =
12524         buildVarDecl(*this, ELoc, Type, D->getName(),
12525                      D->hasAttrs() ? &D->getAttrs() : nullptr,
12526                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
12527     ActOnUninitializedDecl(VDPrivate);
12528     if (VDPrivate->isInvalidDecl())
12529       continue;
12530     DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
12531         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
12532 
12533     DeclRefExpr *Ref = nullptr;
12534     if (!VD && !CurContext->isDependentContext())
12535       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
12536     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
12537     Vars.push_back((VD || CurContext->isDependentContext())
12538                        ? RefExpr->IgnoreParens()
12539                        : Ref);
12540     PrivateCopies.push_back(VDPrivateRefExpr);
12541   }
12542 
12543   if (Vars.empty())
12544     return nullptr;
12545 
12546   return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
12547                                   PrivateCopies);
12548 }
12549 
12550 namespace {
12551 class DiagsUninitializedSeveretyRAII {
12552 private:
12553   DiagnosticsEngine &Diags;
12554   SourceLocation SavedLoc;
12555   bool IsIgnored = false;
12556 
12557 public:
12558   DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
12559                                  bool IsIgnored)
12560       : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
12561     if (!IsIgnored) {
12562       Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
12563                         /*Map*/ diag::Severity::Ignored, Loc);
12564     }
12565   }
12566   ~DiagsUninitializedSeveretyRAII() {
12567     if (!IsIgnored)
12568       Diags.popMappings(SavedLoc);
12569   }
12570 };
12571 }
12572 
12573 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
12574                                                SourceLocation StartLoc,
12575                                                SourceLocation LParenLoc,
12576                                                SourceLocation EndLoc) {
12577   SmallVector<Expr *, 8> Vars;
12578   SmallVector<Expr *, 8> PrivateCopies;
12579   SmallVector<Expr *, 8> Inits;
12580   SmallVector<Decl *, 4> ExprCaptures;
12581   bool IsImplicitClause =
12582       StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
12583   SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
12584 
12585   for (Expr *RefExpr : VarList) {
12586     assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
12587     SourceLocation ELoc;
12588     SourceRange ERange;
12589     Expr *SimpleRefExpr = RefExpr;
12590     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
12591     if (Res.second) {
12592       // It will be analyzed later.
12593       Vars.push_back(RefExpr);
12594       PrivateCopies.push_back(nullptr);
12595       Inits.push_back(nullptr);
12596     }
12597     ValueDecl *D = Res.first;
12598     if (!D)
12599       continue;
12600 
12601     ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
12602     QualType Type = D->getType();
12603     auto *VD = dyn_cast<VarDecl>(D);
12604 
12605     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
12606     //  A variable that appears in a private clause must not have an incomplete
12607     //  type or a reference type.
12608     if (RequireCompleteType(ELoc, Type,
12609                             diag::err_omp_firstprivate_incomplete_type))
12610       continue;
12611     Type = Type.getNonReferenceType();
12612 
12613     // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
12614     //  A variable of class type (or array thereof) that appears in a private
12615     //  clause requires an accessible, unambiguous copy constructor for the
12616     //  class type.
12617     QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
12618 
12619     // If an implicit firstprivate variable found it was checked already.
12620     DSAStackTy::DSAVarData TopDVar;
12621     if (!IsImplicitClause) {
12622       DSAStackTy::DSAVarData DVar =
12623           DSAStack->getTopDSA(D, /*FromParent=*/false);
12624       TopDVar = DVar;
12625       OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
12626       bool IsConstant = ElemType.isConstant(Context);
12627       // OpenMP [2.4.13, Data-sharing Attribute Clauses]
12628       //  A list item that specifies a given variable may not appear in more
12629       // than one clause on the same directive, except that a variable may be
12630       //  specified in both firstprivate and lastprivate clauses.
12631       // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
12632       // A list item may appear in a firstprivate or lastprivate clause but not
12633       // both.
12634       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
12635           (isOpenMPDistributeDirective(CurrDir) ||
12636            DVar.CKind != OMPC_lastprivate) &&
12637           DVar.RefExpr) {
12638         Diag(ELoc, diag::err_omp_wrong_dsa)
12639             << getOpenMPClauseName(DVar.CKind)
12640             << getOpenMPClauseName(OMPC_firstprivate);
12641         reportOriginalDsa(*this, DSAStack, D, DVar);
12642         continue;
12643       }
12644 
12645       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
12646       // in a Construct]
12647       //  Variables with the predetermined data-sharing attributes may not be
12648       //  listed in data-sharing attributes clauses, except for the cases
12649       //  listed below. For these exceptions only, listing a predetermined
12650       //  variable in a data-sharing attribute clause is allowed and overrides
12651       //  the variable's predetermined data-sharing attributes.
12652       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
12653       // in a Construct, C/C++, p.2]
12654       //  Variables with const-qualified type having no mutable member may be
12655       //  listed in a firstprivate clause, even if they are static data members.
12656       if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
12657           DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
12658         Diag(ELoc, diag::err_omp_wrong_dsa)
12659             << getOpenMPClauseName(DVar.CKind)
12660             << getOpenMPClauseName(OMPC_firstprivate);
12661         reportOriginalDsa(*this, DSAStack, D, DVar);
12662         continue;
12663       }
12664 
12665       // OpenMP [2.9.3.4, Restrictions, p.2]
12666       //  A list item that is private within a parallel region must not appear
12667       //  in a firstprivate clause on a worksharing construct if any of the
12668       //  worksharing regions arising from the worksharing construct ever bind
12669       //  to any of the parallel regions arising from the parallel construct.
12670       // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
12671       // A list item that is private within a teams region must not appear in a
12672       // firstprivate clause on a distribute construct if any of the distribute
12673       // regions arising from the distribute construct ever bind to any of the
12674       // teams regions arising from the teams construct.
12675       // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
12676       // A list item that appears in a reduction clause of a teams construct
12677       // must not appear in a firstprivate clause on a distribute construct if
12678       // any of the distribute regions arising from the distribute construct
12679       // ever bind to any of the teams regions arising from the teams construct.
12680       if ((isOpenMPWorksharingDirective(CurrDir) ||
12681            isOpenMPDistributeDirective(CurrDir)) &&
12682           !isOpenMPParallelDirective(CurrDir) &&
12683           !isOpenMPTeamsDirective(CurrDir)) {
12684         DVar = DSAStack->getImplicitDSA(D, true);
12685         if (DVar.CKind != OMPC_shared &&
12686             (isOpenMPParallelDirective(DVar.DKind) ||
12687              isOpenMPTeamsDirective(DVar.DKind) ||
12688              DVar.DKind == OMPD_unknown)) {
12689           Diag(ELoc, diag::err_omp_required_access)
12690               << getOpenMPClauseName(OMPC_firstprivate)
12691               << getOpenMPClauseName(OMPC_shared);
12692           reportOriginalDsa(*this, DSAStack, D, DVar);
12693           continue;
12694         }
12695       }
12696       // OpenMP [2.9.3.4, Restrictions, p.3]
12697       //  A list item that appears in a reduction clause of a parallel construct
12698       //  must not appear in a firstprivate clause on a worksharing or task
12699       //  construct if any of the worksharing or task regions arising from the
12700       //  worksharing or task construct ever bind to any of the parallel regions
12701       //  arising from the parallel construct.
12702       // OpenMP [2.9.3.4, Restrictions, p.4]
12703       //  A list item that appears in a reduction clause in worksharing
12704       //  construct must not appear in a firstprivate clause in a task construct
12705       //  encountered during execution of any of the worksharing regions arising
12706       //  from the worksharing construct.
12707       if (isOpenMPTaskingDirective(CurrDir)) {
12708         DVar = DSAStack->hasInnermostDSA(
12709             D, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
12710             [](OpenMPDirectiveKind K) {
12711               return isOpenMPParallelDirective(K) ||
12712                      isOpenMPWorksharingDirective(K) ||
12713                      isOpenMPTeamsDirective(K);
12714             },
12715             /*FromParent=*/true);
12716         if (DVar.CKind == OMPC_reduction &&
12717             (isOpenMPParallelDirective(DVar.DKind) ||
12718              isOpenMPWorksharingDirective(DVar.DKind) ||
12719              isOpenMPTeamsDirective(DVar.DKind))) {
12720           Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
12721               << getOpenMPDirectiveName(DVar.DKind);
12722           reportOriginalDsa(*this, DSAStack, D, DVar);
12723           continue;
12724         }
12725       }
12726 
12727       // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
12728       // A list item cannot appear in both a map clause and a data-sharing
12729       // attribute clause on the same construct
12730       //
12731       // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
12732       // A list item cannot appear in both a map clause and a data-sharing
12733       // attribute clause on the same construct unless the construct is a
12734       // combined construct.
12735       if ((LangOpts.OpenMP <= 45 &&
12736            isOpenMPTargetExecutionDirective(CurrDir)) ||
12737           CurrDir == OMPD_target) {
12738         OpenMPClauseKind ConflictKind;
12739         if (DSAStack->checkMappableExprComponentListsForDecl(
12740                 VD, /*CurrentRegionOnly=*/true,
12741                 [&ConflictKind](
12742                     OMPClauseMappableExprCommon::MappableExprComponentListRef,
12743                     OpenMPClauseKind WhereFoundClauseKind) {
12744                   ConflictKind = WhereFoundClauseKind;
12745                   return true;
12746                 })) {
12747           Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
12748               << getOpenMPClauseName(OMPC_firstprivate)
12749               << getOpenMPClauseName(ConflictKind)
12750               << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
12751           reportOriginalDsa(*this, DSAStack, D, DVar);
12752           continue;
12753         }
12754       }
12755     }
12756 
12757     // Variably modified types are not supported for tasks.
12758     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
12759         isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
12760       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
12761           << getOpenMPClauseName(OMPC_firstprivate) << Type
12762           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
12763       bool IsDecl =
12764           !VD ||
12765           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
12766       Diag(D->getLocation(),
12767            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
12768           << D;
12769       continue;
12770     }
12771 
12772     Type = Type.getUnqualifiedType();
12773     VarDecl *VDPrivate =
12774         buildVarDecl(*this, ELoc, Type, D->getName(),
12775                      D->hasAttrs() ? &D->getAttrs() : nullptr,
12776                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
12777     // Generate helper private variable and initialize it with the value of the
12778     // original variable. The address of the original variable is replaced by
12779     // the address of the new private variable in the CodeGen. This new variable
12780     // is not added to IdResolver, so the code in the OpenMP region uses
12781     // original variable for proper diagnostics and variable capturing.
12782     Expr *VDInitRefExpr = nullptr;
12783     // For arrays generate initializer for single element and replace it by the
12784     // original array element in CodeGen.
12785     if (Type->isArrayType()) {
12786       VarDecl *VDInit =
12787           buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
12788       VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
12789       Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get();
12790       ElemType = ElemType.getUnqualifiedType();
12791       VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
12792                                          ".firstprivate.temp");
12793       InitializedEntity Entity =
12794           InitializedEntity::InitializeVariable(VDInitTemp);
12795       InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
12796 
12797       InitializationSequence InitSeq(*this, Entity, Kind, Init);
12798       ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
12799       if (Result.isInvalid())
12800         VDPrivate->setInvalidDecl();
12801       else
12802         VDPrivate->setInit(Result.getAs<Expr>());
12803       // Remove temp variable declaration.
12804       Context.Deallocate(VDInitTemp);
12805     } else {
12806       VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
12807                                      ".firstprivate.temp");
12808       VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
12809                                        RefExpr->getExprLoc());
12810       AddInitializerToDecl(VDPrivate,
12811                            DefaultLvalueConversion(VDInitRefExpr).get(),
12812                            /*DirectInit=*/false);
12813     }
12814     if (VDPrivate->isInvalidDecl()) {
12815       if (IsImplicitClause) {
12816         Diag(RefExpr->getExprLoc(),
12817              diag::note_omp_task_predetermined_firstprivate_here);
12818       }
12819       continue;
12820     }
12821     CurContext->addDecl(VDPrivate);
12822     DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
12823         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
12824         RefExpr->getExprLoc());
12825     DeclRefExpr *Ref = nullptr;
12826     if (!VD && !CurContext->isDependentContext()) {
12827       if (TopDVar.CKind == OMPC_lastprivate) {
12828         Ref = TopDVar.PrivateCopy;
12829       } else {
12830         Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
12831         if (!isOpenMPCapturedDecl(D))
12832           ExprCaptures.push_back(Ref->getDecl());
12833       }
12834     }
12835     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
12836     Vars.push_back((VD || CurContext->isDependentContext())
12837                        ? RefExpr->IgnoreParens()
12838                        : Ref);
12839     PrivateCopies.push_back(VDPrivateRefExpr);
12840     Inits.push_back(VDInitRefExpr);
12841   }
12842 
12843   if (Vars.empty())
12844     return nullptr;
12845 
12846   return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12847                                        Vars, PrivateCopies, Inits,
12848                                        buildPreInits(Context, ExprCaptures));
12849 }
12850 
12851 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
12852                                               SourceLocation StartLoc,
12853                                               SourceLocation LParenLoc,
12854                                               SourceLocation EndLoc) {
12855   SmallVector<Expr *, 8> Vars;
12856   SmallVector<Expr *, 8> SrcExprs;
12857   SmallVector<Expr *, 8> DstExprs;
12858   SmallVector<Expr *, 8> AssignmentOps;
12859   SmallVector<Decl *, 4> ExprCaptures;
12860   SmallVector<Expr *, 4> ExprPostUpdates;
12861   for (Expr *RefExpr : VarList) {
12862     assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
12863     SourceLocation ELoc;
12864     SourceRange ERange;
12865     Expr *SimpleRefExpr = RefExpr;
12866     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
12867     if (Res.second) {
12868       // It will be analyzed later.
12869       Vars.push_back(RefExpr);
12870       SrcExprs.push_back(nullptr);
12871       DstExprs.push_back(nullptr);
12872       AssignmentOps.push_back(nullptr);
12873     }
12874     ValueDecl *D = Res.first;
12875     if (!D)
12876       continue;
12877 
12878     QualType Type = D->getType();
12879     auto *VD = dyn_cast<VarDecl>(D);
12880 
12881     // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
12882     //  A variable that appears in a lastprivate clause must not have an
12883     //  incomplete type or a reference type.
12884     if (RequireCompleteType(ELoc, Type,
12885                             diag::err_omp_lastprivate_incomplete_type))
12886       continue;
12887     Type = Type.getNonReferenceType();
12888 
12889     // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
12890     // A variable that is privatized must not have a const-qualified type
12891     // unless it is of class type with a mutable member. This restriction does
12892     // not apply to the firstprivate clause.
12893     //
12894     // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions]
12895     // A variable that appears in a lastprivate clause must not have a
12896     // const-qualified type unless it is of class type with a mutable member.
12897     if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc))
12898       continue;
12899 
12900     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
12901     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
12902     // in a Construct]
12903     //  Variables with the predetermined data-sharing attributes may not be
12904     //  listed in data-sharing attributes clauses, except for the cases
12905     //  listed below.
12906     // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
12907     // A list item may appear in a firstprivate or lastprivate clause but not
12908     // both.
12909     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
12910     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
12911         (isOpenMPDistributeDirective(CurrDir) ||
12912          DVar.CKind != OMPC_firstprivate) &&
12913         (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
12914       Diag(ELoc, diag::err_omp_wrong_dsa)
12915           << getOpenMPClauseName(DVar.CKind)
12916           << getOpenMPClauseName(OMPC_lastprivate);
12917       reportOriginalDsa(*this, DSAStack, D, DVar);
12918       continue;
12919     }
12920 
12921     // OpenMP [2.14.3.5, Restrictions, p.2]
12922     // A list item that is private within a parallel region, or that appears in
12923     // the reduction clause of a parallel construct, must not appear in a
12924     // lastprivate clause on a worksharing construct if any of the corresponding
12925     // worksharing regions ever binds to any of the corresponding parallel
12926     // regions.
12927     DSAStackTy::DSAVarData TopDVar = DVar;
12928     if (isOpenMPWorksharingDirective(CurrDir) &&
12929         !isOpenMPParallelDirective(CurrDir) &&
12930         !isOpenMPTeamsDirective(CurrDir)) {
12931       DVar = DSAStack->getImplicitDSA(D, true);
12932       if (DVar.CKind != OMPC_shared) {
12933         Diag(ELoc, diag::err_omp_required_access)
12934             << getOpenMPClauseName(OMPC_lastprivate)
12935             << getOpenMPClauseName(OMPC_shared);
12936         reportOriginalDsa(*this, DSAStack, D, DVar);
12937         continue;
12938       }
12939     }
12940 
12941     // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
12942     //  A variable of class type (or array thereof) that appears in a
12943     //  lastprivate clause requires an accessible, unambiguous default
12944     //  constructor for the class type, unless the list item is also specified
12945     //  in a firstprivate clause.
12946     //  A variable of class type (or array thereof) that appears in a
12947     //  lastprivate clause requires an accessible, unambiguous copy assignment
12948     //  operator for the class type.
12949     Type = Context.getBaseElementType(Type).getNonReferenceType();
12950     VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(),
12951                                   Type.getUnqualifiedType(), ".lastprivate.src",
12952                                   D->hasAttrs() ? &D->getAttrs() : nullptr);
12953     DeclRefExpr *PseudoSrcExpr =
12954         buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
12955     VarDecl *DstVD =
12956         buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
12957                      D->hasAttrs() ? &D->getAttrs() : nullptr);
12958     DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
12959     // For arrays generate assignment operation for single element and replace
12960     // it by the original array element in CodeGen.
12961     ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
12962                                          PseudoDstExpr, PseudoSrcExpr);
12963     if (AssignmentOp.isInvalid())
12964       continue;
12965     AssignmentOp =
12966         ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
12967     if (AssignmentOp.isInvalid())
12968       continue;
12969 
12970     DeclRefExpr *Ref = nullptr;
12971     if (!VD && !CurContext->isDependentContext()) {
12972       if (TopDVar.CKind == OMPC_firstprivate) {
12973         Ref = TopDVar.PrivateCopy;
12974       } else {
12975         Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
12976         if (!isOpenMPCapturedDecl(D))
12977           ExprCaptures.push_back(Ref->getDecl());
12978       }
12979       if (TopDVar.CKind == OMPC_firstprivate ||
12980           (!isOpenMPCapturedDecl(D) &&
12981            Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
12982         ExprResult RefRes = DefaultLvalueConversion(Ref);
12983         if (!RefRes.isUsable())
12984           continue;
12985         ExprResult PostUpdateRes =
12986             BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
12987                        RefRes.get());
12988         if (!PostUpdateRes.isUsable())
12989           continue;
12990         ExprPostUpdates.push_back(
12991             IgnoredValueConversions(PostUpdateRes.get()).get());
12992       }
12993     }
12994     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
12995     Vars.push_back((VD || CurContext->isDependentContext())
12996                        ? RefExpr->IgnoreParens()
12997                        : Ref);
12998     SrcExprs.push_back(PseudoSrcExpr);
12999     DstExprs.push_back(PseudoDstExpr);
13000     AssignmentOps.push_back(AssignmentOp.get());
13001   }
13002 
13003   if (Vars.empty())
13004     return nullptr;
13005 
13006   return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13007                                       Vars, SrcExprs, DstExprs, AssignmentOps,
13008                                       buildPreInits(Context, ExprCaptures),
13009                                       buildPostUpdate(*this, ExprPostUpdates));
13010 }
13011 
13012 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
13013                                          SourceLocation StartLoc,
13014                                          SourceLocation LParenLoc,
13015                                          SourceLocation EndLoc) {
13016   SmallVector<Expr *, 8> Vars;
13017   for (Expr *RefExpr : VarList) {
13018     assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
13019     SourceLocation ELoc;
13020     SourceRange ERange;
13021     Expr *SimpleRefExpr = RefExpr;
13022     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13023     if (Res.second) {
13024       // It will be analyzed later.
13025       Vars.push_back(RefExpr);
13026     }
13027     ValueDecl *D = Res.first;
13028     if (!D)
13029       continue;
13030 
13031     auto *VD = dyn_cast<VarDecl>(D);
13032     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
13033     // in a Construct]
13034     //  Variables with the predetermined data-sharing attributes may not be
13035     //  listed in data-sharing attributes clauses, except for the cases
13036     //  listed below. For these exceptions only, listing a predetermined
13037     //  variable in a data-sharing attribute clause is allowed and overrides
13038     //  the variable's predetermined data-sharing attributes.
13039     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
13040     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
13041         DVar.RefExpr) {
13042       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
13043                                           << getOpenMPClauseName(OMPC_shared);
13044       reportOriginalDsa(*this, DSAStack, D, DVar);
13045       continue;
13046     }
13047 
13048     DeclRefExpr *Ref = nullptr;
13049     if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
13050       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
13051     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
13052     Vars.push_back((VD || !Ref || CurContext->isDependentContext())
13053                        ? RefExpr->IgnoreParens()
13054                        : Ref);
13055   }
13056 
13057   if (Vars.empty())
13058     return nullptr;
13059 
13060   return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
13061 }
13062 
13063 namespace {
13064 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
13065   DSAStackTy *Stack;
13066 
13067 public:
13068   bool VisitDeclRefExpr(DeclRefExpr *E) {
13069     if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
13070       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
13071       if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
13072         return false;
13073       if (DVar.CKind != OMPC_unknown)
13074         return true;
13075       DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
13076           VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; },
13077           /*FromParent=*/true);
13078       return DVarPrivate.CKind != OMPC_unknown;
13079     }
13080     return false;
13081   }
13082   bool VisitStmt(Stmt *S) {
13083     for (Stmt *Child : S->children()) {
13084       if (Child && Visit(Child))
13085         return true;
13086     }
13087     return false;
13088   }
13089   explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
13090 };
13091 } // namespace
13092 
13093 namespace {
13094 // Transform MemberExpression for specified FieldDecl of current class to
13095 // DeclRefExpr to specified OMPCapturedExprDecl.
13096 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
13097   typedef TreeTransform<TransformExprToCaptures> BaseTransform;
13098   ValueDecl *Field = nullptr;
13099   DeclRefExpr *CapturedExpr = nullptr;
13100 
13101 public:
13102   TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
13103       : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
13104 
13105   ExprResult TransformMemberExpr(MemberExpr *E) {
13106     if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
13107         E->getMemberDecl() == Field) {
13108       CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
13109       return CapturedExpr;
13110     }
13111     return BaseTransform::TransformMemberExpr(E);
13112   }
13113   DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
13114 };
13115 } // namespace
13116 
13117 template <typename T, typename U>
13118 static T filterLookupForUDReductionAndMapper(
13119     SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) {
13120   for (U &Set : Lookups) {
13121     for (auto *D : Set) {
13122       if (T Res = Gen(cast<ValueDecl>(D)))
13123         return Res;
13124     }
13125   }
13126   return T();
13127 }
13128 
13129 static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
13130   assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
13131 
13132   for (auto RD : D->redecls()) {
13133     // Don't bother with extra checks if we already know this one isn't visible.
13134     if (RD == D)
13135       continue;
13136 
13137     auto ND = cast<NamedDecl>(RD);
13138     if (LookupResult::isVisible(SemaRef, ND))
13139       return ND;
13140   }
13141 
13142   return nullptr;
13143 }
13144 
13145 static void
13146 argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id,
13147                         SourceLocation Loc, QualType Ty,
13148                         SmallVectorImpl<UnresolvedSet<8>> &Lookups) {
13149   // Find all of the associated namespaces and classes based on the
13150   // arguments we have.
13151   Sema::AssociatedNamespaceSet AssociatedNamespaces;
13152   Sema::AssociatedClassSet AssociatedClasses;
13153   OpaqueValueExpr OVE(Loc, Ty, VK_LValue);
13154   SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces,
13155                                              AssociatedClasses);
13156 
13157   // C++ [basic.lookup.argdep]p3:
13158   //   Let X be the lookup set produced by unqualified lookup (3.4.1)
13159   //   and let Y be the lookup set produced by argument dependent
13160   //   lookup (defined as follows). If X contains [...] then Y is
13161   //   empty. Otherwise Y is the set of declarations found in the
13162   //   namespaces associated with the argument types as described
13163   //   below. The set of declarations found by the lookup of the name
13164   //   is the union of X and Y.
13165   //
13166   // Here, we compute Y and add its members to the overloaded
13167   // candidate set.
13168   for (auto *NS : AssociatedNamespaces) {
13169     //   When considering an associated namespace, the lookup is the
13170     //   same as the lookup performed when the associated namespace is
13171     //   used as a qualifier (3.4.3.2) except that:
13172     //
13173     //     -- Any using-directives in the associated namespace are
13174     //        ignored.
13175     //
13176     //     -- Any namespace-scope friend functions declared in
13177     //        associated classes are visible within their respective
13178     //        namespaces even if they are not visible during an ordinary
13179     //        lookup (11.4).
13180     DeclContext::lookup_result R = NS->lookup(Id.getName());
13181     for (auto *D : R) {
13182       auto *Underlying = D;
13183       if (auto *USD = dyn_cast<UsingShadowDecl>(D))
13184         Underlying = USD->getTargetDecl();
13185 
13186       if (!isa<OMPDeclareReductionDecl>(Underlying) &&
13187           !isa<OMPDeclareMapperDecl>(Underlying))
13188         continue;
13189 
13190       if (!SemaRef.isVisible(D)) {
13191         D = findAcceptableDecl(SemaRef, D);
13192         if (!D)
13193           continue;
13194         if (auto *USD = dyn_cast<UsingShadowDecl>(D))
13195           Underlying = USD->getTargetDecl();
13196       }
13197       Lookups.emplace_back();
13198       Lookups.back().addDecl(Underlying);
13199     }
13200   }
13201 }
13202 
13203 static ExprResult
13204 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
13205                          Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
13206                          const DeclarationNameInfo &ReductionId, QualType Ty,
13207                          CXXCastPath &BasePath, Expr *UnresolvedReduction) {
13208   if (ReductionIdScopeSpec.isInvalid())
13209     return ExprError();
13210   SmallVector<UnresolvedSet<8>, 4> Lookups;
13211   if (S) {
13212     LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
13213     Lookup.suppressDiagnostics();
13214     while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
13215       NamedDecl *D = Lookup.getRepresentativeDecl();
13216       do {
13217         S = S->getParent();
13218       } while (S && !S->isDeclScope(D));
13219       if (S)
13220         S = S->getParent();
13221       Lookups.emplace_back();
13222       Lookups.back().append(Lookup.begin(), Lookup.end());
13223       Lookup.clear();
13224     }
13225   } else if (auto *ULE =
13226                  cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
13227     Lookups.push_back(UnresolvedSet<8>());
13228     Decl *PrevD = nullptr;
13229     for (NamedDecl *D : ULE->decls()) {
13230       if (D == PrevD)
13231         Lookups.push_back(UnresolvedSet<8>());
13232       else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(D))
13233         Lookups.back().addDecl(DRD);
13234       PrevD = D;
13235     }
13236   }
13237   if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
13238       Ty->isInstantiationDependentType() ||
13239       Ty->containsUnexpandedParameterPack() ||
13240       filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) {
13241         return !D->isInvalidDecl() &&
13242                (D->getType()->isDependentType() ||
13243                 D->getType()->isInstantiationDependentType() ||
13244                 D->getType()->containsUnexpandedParameterPack());
13245       })) {
13246     UnresolvedSet<8> ResSet;
13247     for (const UnresolvedSet<8> &Set : Lookups) {
13248       if (Set.empty())
13249         continue;
13250       ResSet.append(Set.begin(), Set.end());
13251       // The last item marks the end of all declarations at the specified scope.
13252       ResSet.addDecl(Set[Set.size() - 1]);
13253     }
13254     return UnresolvedLookupExpr::Create(
13255         SemaRef.Context, /*NamingClass=*/nullptr,
13256         ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
13257         /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
13258   }
13259   // Lookup inside the classes.
13260   // C++ [over.match.oper]p3:
13261   //   For a unary operator @ with an operand of a type whose
13262   //   cv-unqualified version is T1, and for a binary operator @ with
13263   //   a left operand of a type whose cv-unqualified version is T1 and
13264   //   a right operand of a type whose cv-unqualified version is T2,
13265   //   three sets of candidate functions, designated member
13266   //   candidates, non-member candidates and built-in candidates, are
13267   //   constructed as follows:
13268   //     -- If T1 is a complete class type or a class currently being
13269   //        defined, the set of member candidates is the result of the
13270   //        qualified lookup of T1::operator@ (13.3.1.1.1); otherwise,
13271   //        the set of member candidates is empty.
13272   LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
13273   Lookup.suppressDiagnostics();
13274   if (const auto *TyRec = Ty->getAs<RecordType>()) {
13275     // Complete the type if it can be completed.
13276     // If the type is neither complete nor being defined, bail out now.
13277     if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() ||
13278         TyRec->getDecl()->getDefinition()) {
13279       Lookup.clear();
13280       SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl());
13281       if (Lookup.empty()) {
13282         Lookups.emplace_back();
13283         Lookups.back().append(Lookup.begin(), Lookup.end());
13284       }
13285     }
13286   }
13287   // Perform ADL.
13288   if (SemaRef.getLangOpts().CPlusPlus)
13289     argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups);
13290   if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
13291           Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
13292             if (!D->isInvalidDecl() &&
13293                 SemaRef.Context.hasSameType(D->getType(), Ty))
13294               return D;
13295             return nullptr;
13296           }))
13297     return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(),
13298                                     VK_LValue, Loc);
13299   if (SemaRef.getLangOpts().CPlusPlus) {
13300     if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
13301             Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
13302               if (!D->isInvalidDecl() &&
13303                   SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
13304                   !Ty.isMoreQualifiedThan(D->getType()))
13305                 return D;
13306               return nullptr;
13307             })) {
13308       CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
13309                          /*DetectVirtual=*/false);
13310       if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
13311         if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
13312                 VD->getType().getUnqualifiedType()))) {
13313           if (SemaRef.CheckBaseClassAccess(
13314                   Loc, VD->getType(), Ty, Paths.front(),
13315                   /*DiagID=*/0) != Sema::AR_inaccessible) {
13316             SemaRef.BuildBasePathArray(Paths, BasePath);
13317             return SemaRef.BuildDeclRefExpr(
13318                 VD, VD->getType().getNonReferenceType(), VK_LValue, Loc);
13319           }
13320         }
13321       }
13322     }
13323   }
13324   if (ReductionIdScopeSpec.isSet()) {
13325     SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
13326     return ExprError();
13327   }
13328   return ExprEmpty();
13329 }
13330 
13331 namespace {
13332 /// Data for the reduction-based clauses.
13333 struct ReductionData {
13334   /// List of original reduction items.
13335   SmallVector<Expr *, 8> Vars;
13336   /// List of private copies of the reduction items.
13337   SmallVector<Expr *, 8> Privates;
13338   /// LHS expressions for the reduction_op expressions.
13339   SmallVector<Expr *, 8> LHSs;
13340   /// RHS expressions for the reduction_op expressions.
13341   SmallVector<Expr *, 8> RHSs;
13342   /// Reduction operation expression.
13343   SmallVector<Expr *, 8> ReductionOps;
13344   /// Taskgroup descriptors for the corresponding reduction items in
13345   /// in_reduction clauses.
13346   SmallVector<Expr *, 8> TaskgroupDescriptors;
13347   /// List of captures for clause.
13348   SmallVector<Decl *, 4> ExprCaptures;
13349   /// List of postupdate expressions.
13350   SmallVector<Expr *, 4> ExprPostUpdates;
13351   ReductionData() = delete;
13352   /// Reserves required memory for the reduction data.
13353   ReductionData(unsigned Size) {
13354     Vars.reserve(Size);
13355     Privates.reserve(Size);
13356     LHSs.reserve(Size);
13357     RHSs.reserve(Size);
13358     ReductionOps.reserve(Size);
13359     TaskgroupDescriptors.reserve(Size);
13360     ExprCaptures.reserve(Size);
13361     ExprPostUpdates.reserve(Size);
13362   }
13363   /// Stores reduction item and reduction operation only (required for dependent
13364   /// reduction item).
13365   void push(Expr *Item, Expr *ReductionOp) {
13366     Vars.emplace_back(Item);
13367     Privates.emplace_back(nullptr);
13368     LHSs.emplace_back(nullptr);
13369     RHSs.emplace_back(nullptr);
13370     ReductionOps.emplace_back(ReductionOp);
13371     TaskgroupDescriptors.emplace_back(nullptr);
13372   }
13373   /// Stores reduction data.
13374   void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
13375             Expr *TaskgroupDescriptor) {
13376     Vars.emplace_back(Item);
13377     Privates.emplace_back(Private);
13378     LHSs.emplace_back(LHS);
13379     RHSs.emplace_back(RHS);
13380     ReductionOps.emplace_back(ReductionOp);
13381     TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
13382   }
13383 };
13384 } // namespace
13385 
13386 static bool checkOMPArraySectionConstantForReduction(
13387     ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
13388     SmallVectorImpl<llvm::APSInt> &ArraySizes) {
13389   const Expr *Length = OASE->getLength();
13390   if (Length == nullptr) {
13391     // For array sections of the form [1:] or [:], we would need to analyze
13392     // the lower bound...
13393     if (OASE->getColonLoc().isValid())
13394       return false;
13395 
13396     // This is an array subscript which has implicit length 1!
13397     SingleElement = true;
13398     ArraySizes.push_back(llvm::APSInt::get(1));
13399   } else {
13400     Expr::EvalResult Result;
13401     if (!Length->EvaluateAsInt(Result, Context))
13402       return false;
13403 
13404     llvm::APSInt ConstantLengthValue = Result.Val.getInt();
13405     SingleElement = (ConstantLengthValue.getSExtValue() == 1);
13406     ArraySizes.push_back(ConstantLengthValue);
13407   }
13408 
13409   // Get the base of this array section and walk up from there.
13410   const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
13411 
13412   // We require length = 1 for all array sections except the right-most to
13413   // guarantee that the memory region is contiguous and has no holes in it.
13414   while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
13415     Length = TempOASE->getLength();
13416     if (Length == nullptr) {
13417       // For array sections of the form [1:] or [:], we would need to analyze
13418       // the lower bound...
13419       if (OASE->getColonLoc().isValid())
13420         return false;
13421 
13422       // This is an array subscript which has implicit length 1!
13423       ArraySizes.push_back(llvm::APSInt::get(1));
13424     } else {
13425       Expr::EvalResult Result;
13426       if (!Length->EvaluateAsInt(Result, Context))
13427         return false;
13428 
13429       llvm::APSInt ConstantLengthValue = Result.Val.getInt();
13430       if (ConstantLengthValue.getSExtValue() != 1)
13431         return false;
13432 
13433       ArraySizes.push_back(ConstantLengthValue);
13434     }
13435     Base = TempOASE->getBase()->IgnoreParenImpCasts();
13436   }
13437 
13438   // If we have a single element, we don't need to add the implicit lengths.
13439   if (!SingleElement) {
13440     while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
13441       // Has implicit length 1!
13442       ArraySizes.push_back(llvm::APSInt::get(1));
13443       Base = TempASE->getBase()->IgnoreParenImpCasts();
13444     }
13445   }
13446 
13447   // This array section can be privatized as a single value or as a constant
13448   // sized array.
13449   return true;
13450 }
13451 
13452 static bool actOnOMPReductionKindClause(
13453     Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
13454     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
13455     SourceLocation ColonLoc, SourceLocation EndLoc,
13456     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
13457     ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
13458   DeclarationName DN = ReductionId.getName();
13459   OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
13460   BinaryOperatorKind BOK = BO_Comma;
13461 
13462   ASTContext &Context = S.Context;
13463   // OpenMP [2.14.3.6, reduction clause]
13464   // C
13465   // reduction-identifier is either an identifier or one of the following
13466   // operators: +, -, *,  &, |, ^, && and ||
13467   // C++
13468   // reduction-identifier is either an id-expression or one of the following
13469   // operators: +, -, *, &, |, ^, && and ||
13470   switch (OOK) {
13471   case OO_Plus:
13472   case OO_Minus:
13473     BOK = BO_Add;
13474     break;
13475   case OO_Star:
13476     BOK = BO_Mul;
13477     break;
13478   case OO_Amp:
13479     BOK = BO_And;
13480     break;
13481   case OO_Pipe:
13482     BOK = BO_Or;
13483     break;
13484   case OO_Caret:
13485     BOK = BO_Xor;
13486     break;
13487   case OO_AmpAmp:
13488     BOK = BO_LAnd;
13489     break;
13490   case OO_PipePipe:
13491     BOK = BO_LOr;
13492     break;
13493   case OO_New:
13494   case OO_Delete:
13495   case OO_Array_New:
13496   case OO_Array_Delete:
13497   case OO_Slash:
13498   case OO_Percent:
13499   case OO_Tilde:
13500   case OO_Exclaim:
13501   case OO_Equal:
13502   case OO_Less:
13503   case OO_Greater:
13504   case OO_LessEqual:
13505   case OO_GreaterEqual:
13506   case OO_PlusEqual:
13507   case OO_MinusEqual:
13508   case OO_StarEqual:
13509   case OO_SlashEqual:
13510   case OO_PercentEqual:
13511   case OO_CaretEqual:
13512   case OO_AmpEqual:
13513   case OO_PipeEqual:
13514   case OO_LessLess:
13515   case OO_GreaterGreater:
13516   case OO_LessLessEqual:
13517   case OO_GreaterGreaterEqual:
13518   case OO_EqualEqual:
13519   case OO_ExclaimEqual:
13520   case OO_Spaceship:
13521   case OO_PlusPlus:
13522   case OO_MinusMinus:
13523   case OO_Comma:
13524   case OO_ArrowStar:
13525   case OO_Arrow:
13526   case OO_Call:
13527   case OO_Subscript:
13528   case OO_Conditional:
13529   case OO_Coawait:
13530   case NUM_OVERLOADED_OPERATORS:
13531     llvm_unreachable("Unexpected reduction identifier");
13532   case OO_None:
13533     if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
13534       if (II->isStr("max"))
13535         BOK = BO_GT;
13536       else if (II->isStr("min"))
13537         BOK = BO_LT;
13538     }
13539     break;
13540   }
13541   SourceRange ReductionIdRange;
13542   if (ReductionIdScopeSpec.isValid())
13543     ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
13544   else
13545     ReductionIdRange.setBegin(ReductionId.getBeginLoc());
13546   ReductionIdRange.setEnd(ReductionId.getEndLoc());
13547 
13548   auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
13549   bool FirstIter = true;
13550   for (Expr *RefExpr : VarList) {
13551     assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
13552     // OpenMP [2.1, C/C++]
13553     //  A list item is a variable or array section, subject to the restrictions
13554     //  specified in Section 2.4 on page 42 and in each of the sections
13555     // describing clauses and directives for which a list appears.
13556     // OpenMP  [2.14.3.3, Restrictions, p.1]
13557     //  A variable that is part of another variable (as an array or
13558     //  structure element) cannot appear in a private clause.
13559     if (!FirstIter && IR != ER)
13560       ++IR;
13561     FirstIter = false;
13562     SourceLocation ELoc;
13563     SourceRange ERange;
13564     Expr *SimpleRefExpr = RefExpr;
13565     auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
13566                               /*AllowArraySection=*/true);
13567     if (Res.second) {
13568       // Try to find 'declare reduction' corresponding construct before using
13569       // builtin/overloaded operators.
13570       QualType Type = Context.DependentTy;
13571       CXXCastPath BasePath;
13572       ExprResult DeclareReductionRef = buildDeclareReductionRef(
13573           S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
13574           ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
13575       Expr *ReductionOp = nullptr;
13576       if (S.CurContext->isDependentContext() &&
13577           (DeclareReductionRef.isUnset() ||
13578            isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
13579         ReductionOp = DeclareReductionRef.get();
13580       // It will be analyzed later.
13581       RD.push(RefExpr, ReductionOp);
13582     }
13583     ValueDecl *D = Res.first;
13584     if (!D)
13585       continue;
13586 
13587     Expr *TaskgroupDescriptor = nullptr;
13588     QualType Type;
13589     auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
13590     auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
13591     if (ASE) {
13592       Type = ASE->getType().getNonReferenceType();
13593     } else if (OASE) {
13594       QualType BaseType =
13595           OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
13596       if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
13597         Type = ATy->getElementType();
13598       else
13599         Type = BaseType->getPointeeType();
13600       Type = Type.getNonReferenceType();
13601     } else {
13602       Type = Context.getBaseElementType(D->getType().getNonReferenceType());
13603     }
13604     auto *VD = dyn_cast<VarDecl>(D);
13605 
13606     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
13607     //  A variable that appears in a private clause must not have an incomplete
13608     //  type or a reference type.
13609     if (S.RequireCompleteType(ELoc, D->getType(),
13610                               diag::err_omp_reduction_incomplete_type))
13611       continue;
13612     // OpenMP [2.14.3.6, reduction clause, Restrictions]
13613     // A list item that appears in a reduction clause must not be
13614     // const-qualified.
13615     if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc,
13616                                   /*AcceptIfMutable*/ false, ASE || OASE))
13617       continue;
13618 
13619     OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
13620     // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
13621     //  If a list-item is a reference type then it must bind to the same object
13622     //  for all threads of the team.
13623     if (!ASE && !OASE) {
13624       if (VD) {
13625         VarDecl *VDDef = VD->getDefinition();
13626         if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
13627           DSARefChecker Check(Stack);
13628           if (Check.Visit(VDDef->getInit())) {
13629             S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
13630                 << getOpenMPClauseName(ClauseKind) << ERange;
13631             S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
13632             continue;
13633           }
13634         }
13635       }
13636 
13637       // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
13638       // in a Construct]
13639       //  Variables with the predetermined data-sharing attributes may not be
13640       //  listed in data-sharing attributes clauses, except for the cases
13641       //  listed below. For these exceptions only, listing a predetermined
13642       //  variable in a data-sharing attribute clause is allowed and overrides
13643       //  the variable's predetermined data-sharing attributes.
13644       // OpenMP [2.14.3.6, Restrictions, p.3]
13645       //  Any number of reduction clauses can be specified on the directive,
13646       //  but a list item can appear only once in the reduction clauses for that
13647       //  directive.
13648       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
13649       if (DVar.CKind == OMPC_reduction) {
13650         S.Diag(ELoc, diag::err_omp_once_referenced)
13651             << getOpenMPClauseName(ClauseKind);
13652         if (DVar.RefExpr)
13653           S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
13654         continue;
13655       }
13656       if (DVar.CKind != OMPC_unknown) {
13657         S.Diag(ELoc, diag::err_omp_wrong_dsa)
13658             << getOpenMPClauseName(DVar.CKind)
13659             << getOpenMPClauseName(OMPC_reduction);
13660         reportOriginalDsa(S, Stack, D, DVar);
13661         continue;
13662       }
13663 
13664       // OpenMP [2.14.3.6, Restrictions, p.1]
13665       //  A list item that appears in a reduction clause of a worksharing
13666       //  construct must be shared in the parallel regions to which any of the
13667       //  worksharing regions arising from the worksharing construct bind.
13668       if (isOpenMPWorksharingDirective(CurrDir) &&
13669           !isOpenMPParallelDirective(CurrDir) &&
13670           !isOpenMPTeamsDirective(CurrDir)) {
13671         DVar = Stack->getImplicitDSA(D, true);
13672         if (DVar.CKind != OMPC_shared) {
13673           S.Diag(ELoc, diag::err_omp_required_access)
13674               << getOpenMPClauseName(OMPC_reduction)
13675               << getOpenMPClauseName(OMPC_shared);
13676           reportOriginalDsa(S, Stack, D, DVar);
13677           continue;
13678         }
13679       }
13680     }
13681 
13682     // Try to find 'declare reduction' corresponding construct before using
13683     // builtin/overloaded operators.
13684     CXXCastPath BasePath;
13685     ExprResult DeclareReductionRef = buildDeclareReductionRef(
13686         S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
13687         ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
13688     if (DeclareReductionRef.isInvalid())
13689       continue;
13690     if (S.CurContext->isDependentContext() &&
13691         (DeclareReductionRef.isUnset() ||
13692          isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
13693       RD.push(RefExpr, DeclareReductionRef.get());
13694       continue;
13695     }
13696     if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
13697       // Not allowed reduction identifier is found.
13698       S.Diag(ReductionId.getBeginLoc(),
13699              diag::err_omp_unknown_reduction_identifier)
13700           << Type << ReductionIdRange;
13701       continue;
13702     }
13703 
13704     // OpenMP [2.14.3.6, reduction clause, Restrictions]
13705     // The type of a list item that appears in a reduction clause must be valid
13706     // for the reduction-identifier. For a max or min reduction in C, the type
13707     // of the list item must be an allowed arithmetic data type: char, int,
13708     // float, double, or _Bool, possibly modified with long, short, signed, or
13709     // unsigned. For a max or min reduction in C++, the type of the list item
13710     // must be an allowed arithmetic data type: char, wchar_t, int, float,
13711     // double, or bool, possibly modified with long, short, signed, or unsigned.
13712     if (DeclareReductionRef.isUnset()) {
13713       if ((BOK == BO_GT || BOK == BO_LT) &&
13714           !(Type->isScalarType() ||
13715             (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
13716         S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
13717             << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
13718         if (!ASE && !OASE) {
13719           bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
13720                                    VarDecl::DeclarationOnly;
13721           S.Diag(D->getLocation(),
13722                  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
13723               << D;
13724         }
13725         continue;
13726       }
13727       if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
13728           !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
13729         S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
13730             << getOpenMPClauseName(ClauseKind);
13731         if (!ASE && !OASE) {
13732           bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
13733                                    VarDecl::DeclarationOnly;
13734           S.Diag(D->getLocation(),
13735                  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
13736               << D;
13737         }
13738         continue;
13739       }
13740     }
13741 
13742     Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
13743     VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
13744                                   D->hasAttrs() ? &D->getAttrs() : nullptr);
13745     VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
13746                                   D->hasAttrs() ? &D->getAttrs() : nullptr);
13747     QualType PrivateTy = Type;
13748 
13749     // Try if we can determine constant lengths for all array sections and avoid
13750     // the VLA.
13751     bool ConstantLengthOASE = false;
13752     if (OASE) {
13753       bool SingleElement;
13754       llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
13755       ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
13756           Context, OASE, SingleElement, ArraySizes);
13757 
13758       // If we don't have a single element, we must emit a constant array type.
13759       if (ConstantLengthOASE && !SingleElement) {
13760         for (llvm::APSInt &Size : ArraySizes)
13761           PrivateTy = Context.getConstantArrayType(PrivateTy, Size, nullptr,
13762                                                    ArrayType::Normal,
13763                                                    /*IndexTypeQuals=*/0);
13764       }
13765     }
13766 
13767     if ((OASE && !ConstantLengthOASE) ||
13768         (!OASE && !ASE &&
13769          D->getType().getNonReferenceType()->isVariablyModifiedType())) {
13770       if (!Context.getTargetInfo().isVLASupported()) {
13771         if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective())) {
13772           S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
13773           S.Diag(ELoc, diag::note_vla_unsupported);
13774         } else {
13775           S.targetDiag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
13776           S.targetDiag(ELoc, diag::note_vla_unsupported);
13777         }
13778         continue;
13779       }
13780       // For arrays/array sections only:
13781       // Create pseudo array type for private copy. The size for this array will
13782       // be generated during codegen.
13783       // For array subscripts or single variables Private Ty is the same as Type
13784       // (type of the variable or single array element).
13785       PrivateTy = Context.getVariableArrayType(
13786           Type,
13787           new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue),
13788           ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
13789     } else if (!ASE && !OASE &&
13790                Context.getAsArrayType(D->getType().getNonReferenceType())) {
13791       PrivateTy = D->getType().getNonReferenceType();
13792     }
13793     // Private copy.
13794     VarDecl *PrivateVD =
13795         buildVarDecl(S, ELoc, PrivateTy, D->getName(),
13796                      D->hasAttrs() ? &D->getAttrs() : nullptr,
13797                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
13798     // Add initializer for private variable.
13799     Expr *Init = nullptr;
13800     DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
13801     DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
13802     if (DeclareReductionRef.isUsable()) {
13803       auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
13804       auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
13805       if (DRD->getInitializer()) {
13806         Init = DRDRef;
13807         RHSVD->setInit(DRDRef);
13808         RHSVD->setInitStyle(VarDecl::CallInit);
13809       }
13810     } else {
13811       switch (BOK) {
13812       case BO_Add:
13813       case BO_Xor:
13814       case BO_Or:
13815       case BO_LOr:
13816         // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
13817         if (Type->isScalarType() || Type->isAnyComplexType())
13818           Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
13819         break;
13820       case BO_Mul:
13821       case BO_LAnd:
13822         if (Type->isScalarType() || Type->isAnyComplexType()) {
13823           // '*' and '&&' reduction ops - initializer is '1'.
13824           Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
13825         }
13826         break;
13827       case BO_And: {
13828         // '&' reduction op - initializer is '~0'.
13829         QualType OrigType = Type;
13830         if (auto *ComplexTy = OrigType->getAs<ComplexType>())
13831           Type = ComplexTy->getElementType();
13832         if (Type->isRealFloatingType()) {
13833           llvm::APFloat InitValue =
13834               llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
13835                                              /*isIEEE=*/true);
13836           Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
13837                                          Type, ELoc);
13838         } else if (Type->isScalarType()) {
13839           uint64_t Size = Context.getTypeSize(Type);
13840           QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
13841           llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
13842           Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
13843         }
13844         if (Init && OrigType->isAnyComplexType()) {
13845           // Init = 0xFFFF + 0xFFFFi;
13846           auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
13847           Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
13848         }
13849         Type = OrigType;
13850         break;
13851       }
13852       case BO_LT:
13853       case BO_GT: {
13854         // 'min' reduction op - initializer is 'Largest representable number in
13855         // the reduction list item type'.
13856         // 'max' reduction op - initializer is 'Least representable number in
13857         // the reduction list item type'.
13858         if (Type->isIntegerType() || Type->isPointerType()) {
13859           bool IsSigned = Type->hasSignedIntegerRepresentation();
13860           uint64_t Size = Context.getTypeSize(Type);
13861           QualType IntTy =
13862               Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
13863           llvm::APInt InitValue =
13864               (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
13865                                         : llvm::APInt::getMinValue(Size)
13866                              : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
13867                                         : llvm::APInt::getMaxValue(Size);
13868           Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
13869           if (Type->isPointerType()) {
13870             // Cast to pointer type.
13871             ExprResult CastExpr = S.BuildCStyleCastExpr(
13872                 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
13873             if (CastExpr.isInvalid())
13874               continue;
13875             Init = CastExpr.get();
13876           }
13877         } else if (Type->isRealFloatingType()) {
13878           llvm::APFloat InitValue = llvm::APFloat::getLargest(
13879               Context.getFloatTypeSemantics(Type), BOK != BO_LT);
13880           Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
13881                                          Type, ELoc);
13882         }
13883         break;
13884       }
13885       case BO_PtrMemD:
13886       case BO_PtrMemI:
13887       case BO_MulAssign:
13888       case BO_Div:
13889       case BO_Rem:
13890       case BO_Sub:
13891       case BO_Shl:
13892       case BO_Shr:
13893       case BO_LE:
13894       case BO_GE:
13895       case BO_EQ:
13896       case BO_NE:
13897       case BO_Cmp:
13898       case BO_AndAssign:
13899       case BO_XorAssign:
13900       case BO_OrAssign:
13901       case BO_Assign:
13902       case BO_AddAssign:
13903       case BO_SubAssign:
13904       case BO_DivAssign:
13905       case BO_RemAssign:
13906       case BO_ShlAssign:
13907       case BO_ShrAssign:
13908       case BO_Comma:
13909         llvm_unreachable("Unexpected reduction operation");
13910       }
13911     }
13912     if (Init && DeclareReductionRef.isUnset())
13913       S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
13914     else if (!Init)
13915       S.ActOnUninitializedDecl(RHSVD);
13916     if (RHSVD->isInvalidDecl())
13917       continue;
13918     if (!RHSVD->hasInit() &&
13919         (DeclareReductionRef.isUnset() || !S.LangOpts.CPlusPlus)) {
13920       S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
13921           << Type << ReductionIdRange;
13922       bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
13923                                VarDecl::DeclarationOnly;
13924       S.Diag(D->getLocation(),
13925              IsDecl ? diag::note_previous_decl : diag::note_defined_here)
13926           << D;
13927       continue;
13928     }
13929     // Store initializer for single element in private copy. Will be used during
13930     // codegen.
13931     PrivateVD->setInit(RHSVD->getInit());
13932     PrivateVD->setInitStyle(RHSVD->getInitStyle());
13933     DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
13934     ExprResult ReductionOp;
13935     if (DeclareReductionRef.isUsable()) {
13936       QualType RedTy = DeclareReductionRef.get()->getType();
13937       QualType PtrRedTy = Context.getPointerType(RedTy);
13938       ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
13939       ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
13940       if (!BasePath.empty()) {
13941         LHS = S.DefaultLvalueConversion(LHS.get());
13942         RHS = S.DefaultLvalueConversion(RHS.get());
13943         LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
13944                                        CK_UncheckedDerivedToBase, LHS.get(),
13945                                        &BasePath, LHS.get()->getValueKind());
13946         RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
13947                                        CK_UncheckedDerivedToBase, RHS.get(),
13948                                        &BasePath, RHS.get()->getValueKind());
13949       }
13950       FunctionProtoType::ExtProtoInfo EPI;
13951       QualType Params[] = {PtrRedTy, PtrRedTy};
13952       QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
13953       auto *OVE = new (Context) OpaqueValueExpr(
13954           ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
13955           S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
13956       Expr *Args[] = {LHS.get(), RHS.get()};
13957       ReductionOp =
13958           CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
13959     } else {
13960       ReductionOp = S.BuildBinOp(
13961           Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE);
13962       if (ReductionOp.isUsable()) {
13963         if (BOK != BO_LT && BOK != BO_GT) {
13964           ReductionOp =
13965               S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
13966                            BO_Assign, LHSDRE, ReductionOp.get());
13967         } else {
13968           auto *ConditionalOp = new (Context)
13969               ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
13970                                   Type, VK_LValue, OK_Ordinary);
13971           ReductionOp =
13972               S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
13973                            BO_Assign, LHSDRE, ConditionalOp);
13974         }
13975         if (ReductionOp.isUsable())
13976           ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(),
13977                                               /*DiscardedValue*/ false);
13978       }
13979       if (!ReductionOp.isUsable())
13980         continue;
13981     }
13982 
13983     // OpenMP [2.15.4.6, Restrictions, p.2]
13984     // A list item that appears in an in_reduction clause of a task construct
13985     // must appear in a task_reduction clause of a construct associated with a
13986     // taskgroup region that includes the participating task in its taskgroup
13987     // set. The construct associated with the innermost region that meets this
13988     // condition must specify the same reduction-identifier as the in_reduction
13989     // clause.
13990     if (ClauseKind == OMPC_in_reduction) {
13991       SourceRange ParentSR;
13992       BinaryOperatorKind ParentBOK;
13993       const Expr *ParentReductionOp;
13994       Expr *ParentBOKTD, *ParentReductionOpTD;
13995       DSAStackTy::DSAVarData ParentBOKDSA =
13996           Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
13997                                                   ParentBOKTD);
13998       DSAStackTy::DSAVarData ParentReductionOpDSA =
13999           Stack->getTopMostTaskgroupReductionData(
14000               D, ParentSR, ParentReductionOp, ParentReductionOpTD);
14001       bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
14002       bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
14003       if (!IsParentBOK && !IsParentReductionOp) {
14004         S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction);
14005         continue;
14006       }
14007       if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
14008           (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK ||
14009           IsParentReductionOp) {
14010         bool EmitError = true;
14011         if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
14012           llvm::FoldingSetNodeID RedId, ParentRedId;
14013           ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
14014           DeclareReductionRef.get()->Profile(RedId, Context,
14015                                              /*Canonical=*/true);
14016           EmitError = RedId != ParentRedId;
14017         }
14018         if (EmitError) {
14019           S.Diag(ReductionId.getBeginLoc(),
14020                  diag::err_omp_reduction_identifier_mismatch)
14021               << ReductionIdRange << RefExpr->getSourceRange();
14022           S.Diag(ParentSR.getBegin(),
14023                  diag::note_omp_previous_reduction_identifier)
14024               << ParentSR
14025               << (IsParentBOK ? ParentBOKDSA.RefExpr
14026                               : ParentReductionOpDSA.RefExpr)
14027                      ->getSourceRange();
14028           continue;
14029         }
14030       }
14031       TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
14032       assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined.");
14033     }
14034 
14035     DeclRefExpr *Ref = nullptr;
14036     Expr *VarsExpr = RefExpr->IgnoreParens();
14037     if (!VD && !S.CurContext->isDependentContext()) {
14038       if (ASE || OASE) {
14039         TransformExprToCaptures RebuildToCapture(S, D);
14040         VarsExpr =
14041             RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
14042         Ref = RebuildToCapture.getCapturedExpr();
14043       } else {
14044         VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
14045       }
14046       if (!S.isOpenMPCapturedDecl(D)) {
14047         RD.ExprCaptures.emplace_back(Ref->getDecl());
14048         if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
14049           ExprResult RefRes = S.DefaultLvalueConversion(Ref);
14050           if (!RefRes.isUsable())
14051             continue;
14052           ExprResult PostUpdateRes =
14053               S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
14054                            RefRes.get());
14055           if (!PostUpdateRes.isUsable())
14056             continue;
14057           if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
14058               Stack->getCurrentDirective() == OMPD_taskgroup) {
14059             S.Diag(RefExpr->getExprLoc(),
14060                    diag::err_omp_reduction_non_addressable_expression)
14061                 << RefExpr->getSourceRange();
14062             continue;
14063           }
14064           RD.ExprPostUpdates.emplace_back(
14065               S.IgnoredValueConversions(PostUpdateRes.get()).get());
14066         }
14067       }
14068     }
14069     // All reduction items are still marked as reduction (to do not increase
14070     // code base size).
14071     Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
14072     if (CurrDir == OMPD_taskgroup) {
14073       if (DeclareReductionRef.isUsable())
14074         Stack->addTaskgroupReductionData(D, ReductionIdRange,
14075                                          DeclareReductionRef.get());
14076       else
14077         Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
14078     }
14079     RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
14080             TaskgroupDescriptor);
14081   }
14082   return RD.Vars.empty();
14083 }
14084 
14085 OMPClause *Sema::ActOnOpenMPReductionClause(
14086     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
14087     SourceLocation ColonLoc, SourceLocation EndLoc,
14088     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
14089     ArrayRef<Expr *> UnresolvedReductions) {
14090   ReductionData RD(VarList.size());
14091   if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
14092                                   StartLoc, LParenLoc, ColonLoc, EndLoc,
14093                                   ReductionIdScopeSpec, ReductionId,
14094                                   UnresolvedReductions, RD))
14095     return nullptr;
14096 
14097   return OMPReductionClause::Create(
14098       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
14099       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
14100       RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
14101       buildPreInits(Context, RD.ExprCaptures),
14102       buildPostUpdate(*this, RD.ExprPostUpdates));
14103 }
14104 
14105 OMPClause *Sema::ActOnOpenMPTaskReductionClause(
14106     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
14107     SourceLocation ColonLoc, SourceLocation EndLoc,
14108     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
14109     ArrayRef<Expr *> UnresolvedReductions) {
14110   ReductionData RD(VarList.size());
14111   if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList,
14112                                   StartLoc, LParenLoc, ColonLoc, EndLoc,
14113                                   ReductionIdScopeSpec, ReductionId,
14114                                   UnresolvedReductions, RD))
14115     return nullptr;
14116 
14117   return OMPTaskReductionClause::Create(
14118       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
14119       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
14120       RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
14121       buildPreInits(Context, RD.ExprCaptures),
14122       buildPostUpdate(*this, RD.ExprPostUpdates));
14123 }
14124 
14125 OMPClause *Sema::ActOnOpenMPInReductionClause(
14126     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
14127     SourceLocation ColonLoc, SourceLocation EndLoc,
14128     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
14129     ArrayRef<Expr *> UnresolvedReductions) {
14130   ReductionData RD(VarList.size());
14131   if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
14132                                   StartLoc, LParenLoc, ColonLoc, EndLoc,
14133                                   ReductionIdScopeSpec, ReductionId,
14134                                   UnresolvedReductions, RD))
14135     return nullptr;
14136 
14137   return OMPInReductionClause::Create(
14138       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
14139       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
14140       RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
14141       buildPreInits(Context, RD.ExprCaptures),
14142       buildPostUpdate(*this, RD.ExprPostUpdates));
14143 }
14144 
14145 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
14146                                      SourceLocation LinLoc) {
14147   if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
14148       LinKind == OMPC_LINEAR_unknown) {
14149     Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
14150     return true;
14151   }
14152   return false;
14153 }
14154 
14155 bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
14156                                  OpenMPLinearClauseKind LinKind,
14157                                  QualType Type) {
14158   const auto *VD = dyn_cast_or_null<VarDecl>(D);
14159   // A variable must not have an incomplete type or a reference type.
14160   if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
14161     return true;
14162   if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
14163       !Type->isReferenceType()) {
14164     Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
14165         << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
14166     return true;
14167   }
14168   Type = Type.getNonReferenceType();
14169 
14170   // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
14171   // A variable that is privatized must not have a const-qualified type
14172   // unless it is of class type with a mutable member. This restriction does
14173   // not apply to the firstprivate clause.
14174   if (rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc))
14175     return true;
14176 
14177   // A list item must be of integral or pointer type.
14178   Type = Type.getUnqualifiedType().getCanonicalType();
14179   const auto *Ty = Type.getTypePtrOrNull();
14180   if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
14181               !Ty->isPointerType())) {
14182     Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
14183     if (D) {
14184       bool IsDecl =
14185           !VD ||
14186           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
14187       Diag(D->getLocation(),
14188            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
14189           << D;
14190     }
14191     return true;
14192   }
14193   return false;
14194 }
14195 
14196 OMPClause *Sema::ActOnOpenMPLinearClause(
14197     ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
14198     SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
14199     SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
14200   SmallVector<Expr *, 8> Vars;
14201   SmallVector<Expr *, 8> Privates;
14202   SmallVector<Expr *, 8> Inits;
14203   SmallVector<Decl *, 4> ExprCaptures;
14204   SmallVector<Expr *, 4> ExprPostUpdates;
14205   if (CheckOpenMPLinearModifier(LinKind, LinLoc))
14206     LinKind = OMPC_LINEAR_val;
14207   for (Expr *RefExpr : VarList) {
14208     assert(RefExpr && "NULL expr in OpenMP linear clause.");
14209     SourceLocation ELoc;
14210     SourceRange ERange;
14211     Expr *SimpleRefExpr = RefExpr;
14212     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
14213     if (Res.second) {
14214       // It will be analyzed later.
14215       Vars.push_back(RefExpr);
14216       Privates.push_back(nullptr);
14217       Inits.push_back(nullptr);
14218     }
14219     ValueDecl *D = Res.first;
14220     if (!D)
14221       continue;
14222 
14223     QualType Type = D->getType();
14224     auto *VD = dyn_cast<VarDecl>(D);
14225 
14226     // OpenMP [2.14.3.7, linear clause]
14227     //  A list-item cannot appear in more than one linear clause.
14228     //  A list-item that appears in a linear clause cannot appear in any
14229     //  other data-sharing attribute clause.
14230     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
14231     if (DVar.RefExpr) {
14232       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
14233                                           << getOpenMPClauseName(OMPC_linear);
14234       reportOriginalDsa(*this, DSAStack, D, DVar);
14235       continue;
14236     }
14237 
14238     if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
14239       continue;
14240     Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
14241 
14242     // Build private copy of original var.
14243     VarDecl *Private =
14244         buildVarDecl(*this, ELoc, Type, D->getName(),
14245                      D->hasAttrs() ? &D->getAttrs() : nullptr,
14246                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
14247     DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
14248     // Build var to save initial value.
14249     VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
14250     Expr *InitExpr;
14251     DeclRefExpr *Ref = nullptr;
14252     if (!VD && !CurContext->isDependentContext()) {
14253       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
14254       if (!isOpenMPCapturedDecl(D)) {
14255         ExprCaptures.push_back(Ref->getDecl());
14256         if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
14257           ExprResult RefRes = DefaultLvalueConversion(Ref);
14258           if (!RefRes.isUsable())
14259             continue;
14260           ExprResult PostUpdateRes =
14261               BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
14262                          SimpleRefExpr, RefRes.get());
14263           if (!PostUpdateRes.isUsable())
14264             continue;
14265           ExprPostUpdates.push_back(
14266               IgnoredValueConversions(PostUpdateRes.get()).get());
14267         }
14268       }
14269     }
14270     if (LinKind == OMPC_LINEAR_uval)
14271       InitExpr = VD ? VD->getInit() : SimpleRefExpr;
14272     else
14273       InitExpr = VD ? SimpleRefExpr : Ref;
14274     AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
14275                          /*DirectInit=*/false);
14276     DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
14277 
14278     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
14279     Vars.push_back((VD || CurContext->isDependentContext())
14280                        ? RefExpr->IgnoreParens()
14281                        : Ref);
14282     Privates.push_back(PrivateRef);
14283     Inits.push_back(InitRef);
14284   }
14285 
14286   if (Vars.empty())
14287     return nullptr;
14288 
14289   Expr *StepExpr = Step;
14290   Expr *CalcStepExpr = nullptr;
14291   if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
14292       !Step->isInstantiationDependent() &&
14293       !Step->containsUnexpandedParameterPack()) {
14294     SourceLocation StepLoc = Step->getBeginLoc();
14295     ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
14296     if (Val.isInvalid())
14297       return nullptr;
14298     StepExpr = Val.get();
14299 
14300     // Build var to save the step value.
14301     VarDecl *SaveVar =
14302         buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
14303     ExprResult SaveRef =
14304         buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
14305     ExprResult CalcStep =
14306         BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
14307     CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false);
14308 
14309     // Warn about zero linear step (it would be probably better specified as
14310     // making corresponding variables 'const').
14311     llvm::APSInt Result;
14312     bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
14313     if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
14314       Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
14315                                                      << (Vars.size() > 1);
14316     if (!IsConstant && CalcStep.isUsable()) {
14317       // Calculate the step beforehand instead of doing this on each iteration.
14318       // (This is not used if the number of iterations may be kfold-ed).
14319       CalcStepExpr = CalcStep.get();
14320     }
14321   }
14322 
14323   return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
14324                                  ColonLoc, EndLoc, Vars, Privates, Inits,
14325                                  StepExpr, CalcStepExpr,
14326                                  buildPreInits(Context, ExprCaptures),
14327                                  buildPostUpdate(*this, ExprPostUpdates));
14328 }
14329 
14330 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
14331                                      Expr *NumIterations, Sema &SemaRef,
14332                                      Scope *S, DSAStackTy *Stack) {
14333   // Walk the vars and build update/final expressions for the CodeGen.
14334   SmallVector<Expr *, 8> Updates;
14335   SmallVector<Expr *, 8> Finals;
14336   SmallVector<Expr *, 8> UsedExprs;
14337   Expr *Step = Clause.getStep();
14338   Expr *CalcStep = Clause.getCalcStep();
14339   // OpenMP [2.14.3.7, linear clause]
14340   // If linear-step is not specified it is assumed to be 1.
14341   if (!Step)
14342     Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
14343   else if (CalcStep)
14344     Step = cast<BinaryOperator>(CalcStep)->getLHS();
14345   bool HasErrors = false;
14346   auto CurInit = Clause.inits().begin();
14347   auto CurPrivate = Clause.privates().begin();
14348   OpenMPLinearClauseKind LinKind = Clause.getModifier();
14349   for (Expr *RefExpr : Clause.varlists()) {
14350     SourceLocation ELoc;
14351     SourceRange ERange;
14352     Expr *SimpleRefExpr = RefExpr;
14353     auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange);
14354     ValueDecl *D = Res.first;
14355     if (Res.second || !D) {
14356       Updates.push_back(nullptr);
14357       Finals.push_back(nullptr);
14358       HasErrors = true;
14359       continue;
14360     }
14361     auto &&Info = Stack->isLoopControlVariable(D);
14362     // OpenMP [2.15.11, distribute simd Construct]
14363     // A list item may not appear in a linear clause, unless it is the loop
14364     // iteration variable.
14365     if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
14366         isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
14367       SemaRef.Diag(ELoc,
14368                    diag::err_omp_linear_distribute_var_non_loop_iteration);
14369       Updates.push_back(nullptr);
14370       Finals.push_back(nullptr);
14371       HasErrors = true;
14372       continue;
14373     }
14374     Expr *InitExpr = *CurInit;
14375 
14376     // Build privatized reference to the current linear var.
14377     auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
14378     Expr *CapturedRef;
14379     if (LinKind == OMPC_LINEAR_uval)
14380       CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
14381     else
14382       CapturedRef =
14383           buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
14384                            DE->getType().getUnqualifiedType(), DE->getExprLoc(),
14385                            /*RefersToCapture=*/true);
14386 
14387     // Build update: Var = InitExpr + IV * Step
14388     ExprResult Update;
14389     if (!Info.first)
14390       Update = buildCounterUpdate(
14391           SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, InitExpr, IV, Step,
14392           /*Subtract=*/false, /*IsNonRectangularLB=*/false);
14393     else
14394       Update = *CurPrivate;
14395     Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(),
14396                                          /*DiscardedValue*/ false);
14397 
14398     // Build final: Var = InitExpr + NumIterations * Step
14399     ExprResult Final;
14400     if (!Info.first)
14401       Final =
14402           buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
14403                              InitExpr, NumIterations, Step, /*Subtract=*/false,
14404                              /*IsNonRectangularLB=*/false);
14405     else
14406       Final = *CurPrivate;
14407     Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(),
14408                                         /*DiscardedValue*/ false);
14409 
14410     if (!Update.isUsable() || !Final.isUsable()) {
14411       Updates.push_back(nullptr);
14412       Finals.push_back(nullptr);
14413       UsedExprs.push_back(nullptr);
14414       HasErrors = true;
14415     } else {
14416       Updates.push_back(Update.get());
14417       Finals.push_back(Final.get());
14418       if (!Info.first)
14419         UsedExprs.push_back(SimpleRefExpr);
14420     }
14421     ++CurInit;
14422     ++CurPrivate;
14423   }
14424   if (Expr *S = Clause.getStep())
14425     UsedExprs.push_back(S);
14426   // Fill the remaining part with the nullptr.
14427   UsedExprs.append(Clause.varlist_size() + 1 - UsedExprs.size(), nullptr);
14428   Clause.setUpdates(Updates);
14429   Clause.setFinals(Finals);
14430   Clause.setUsedExprs(UsedExprs);
14431   return HasErrors;
14432 }
14433 
14434 OMPClause *Sema::ActOnOpenMPAlignedClause(
14435     ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
14436     SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
14437   SmallVector<Expr *, 8> Vars;
14438   for (Expr *RefExpr : VarList) {
14439     assert(RefExpr && "NULL expr in OpenMP linear clause.");
14440     SourceLocation ELoc;
14441     SourceRange ERange;
14442     Expr *SimpleRefExpr = RefExpr;
14443     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
14444     if (Res.second) {
14445       // It will be analyzed later.
14446       Vars.push_back(RefExpr);
14447     }
14448     ValueDecl *D = Res.first;
14449     if (!D)
14450       continue;
14451 
14452     QualType QType = D->getType();
14453     auto *VD = dyn_cast<VarDecl>(D);
14454 
14455     // OpenMP  [2.8.1, simd construct, Restrictions]
14456     // The type of list items appearing in the aligned clause must be
14457     // array, pointer, reference to array, or reference to pointer.
14458     QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
14459     const Type *Ty = QType.getTypePtrOrNull();
14460     if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
14461       Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
14462           << QType << getLangOpts().CPlusPlus << ERange;
14463       bool IsDecl =
14464           !VD ||
14465           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
14466       Diag(D->getLocation(),
14467            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
14468           << D;
14469       continue;
14470     }
14471 
14472     // OpenMP  [2.8.1, simd construct, Restrictions]
14473     // A list-item cannot appear in more than one aligned clause.
14474     if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
14475       Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
14476       Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
14477           << getOpenMPClauseName(OMPC_aligned);
14478       continue;
14479     }
14480 
14481     DeclRefExpr *Ref = nullptr;
14482     if (!VD && isOpenMPCapturedDecl(D))
14483       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
14484     Vars.push_back(DefaultFunctionArrayConversion(
14485                        (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
14486                        .get());
14487   }
14488 
14489   // OpenMP [2.8.1, simd construct, Description]
14490   // The parameter of the aligned clause, alignment, must be a constant
14491   // positive integer expression.
14492   // If no optional parameter is specified, implementation-defined default
14493   // alignments for SIMD instructions on the target platforms are assumed.
14494   if (Alignment != nullptr) {
14495     ExprResult AlignResult =
14496         VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
14497     if (AlignResult.isInvalid())
14498       return nullptr;
14499     Alignment = AlignResult.get();
14500   }
14501   if (Vars.empty())
14502     return nullptr;
14503 
14504   return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
14505                                   EndLoc, Vars, Alignment);
14506 }
14507 
14508 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
14509                                          SourceLocation StartLoc,
14510                                          SourceLocation LParenLoc,
14511                                          SourceLocation EndLoc) {
14512   SmallVector<Expr *, 8> Vars;
14513   SmallVector<Expr *, 8> SrcExprs;
14514   SmallVector<Expr *, 8> DstExprs;
14515   SmallVector<Expr *, 8> AssignmentOps;
14516   for (Expr *RefExpr : VarList) {
14517     assert(RefExpr && "NULL expr in OpenMP copyin clause.");
14518     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
14519       // It will be analyzed later.
14520       Vars.push_back(RefExpr);
14521       SrcExprs.push_back(nullptr);
14522       DstExprs.push_back(nullptr);
14523       AssignmentOps.push_back(nullptr);
14524       continue;
14525     }
14526 
14527     SourceLocation ELoc = RefExpr->getExprLoc();
14528     // OpenMP [2.1, C/C++]
14529     //  A list item is a variable name.
14530     // OpenMP  [2.14.4.1, Restrictions, p.1]
14531     //  A list item that appears in a copyin clause must be threadprivate.
14532     auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
14533     if (!DE || !isa<VarDecl>(DE->getDecl())) {
14534       Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
14535           << 0 << RefExpr->getSourceRange();
14536       continue;
14537     }
14538 
14539     Decl *D = DE->getDecl();
14540     auto *VD = cast<VarDecl>(D);
14541 
14542     QualType Type = VD->getType();
14543     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
14544       // It will be analyzed later.
14545       Vars.push_back(DE);
14546       SrcExprs.push_back(nullptr);
14547       DstExprs.push_back(nullptr);
14548       AssignmentOps.push_back(nullptr);
14549       continue;
14550     }
14551 
14552     // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
14553     //  A list item that appears in a copyin clause must be threadprivate.
14554     if (!DSAStack->isThreadPrivate(VD)) {
14555       Diag(ELoc, diag::err_omp_required_access)
14556           << getOpenMPClauseName(OMPC_copyin)
14557           << getOpenMPDirectiveName(OMPD_threadprivate);
14558       continue;
14559     }
14560 
14561     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
14562     //  A variable of class type (or array thereof) that appears in a
14563     //  copyin clause requires an accessible, unambiguous copy assignment
14564     //  operator for the class type.
14565     QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
14566     VarDecl *SrcVD =
14567         buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(),
14568                      ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
14569     DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
14570         *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
14571     VarDecl *DstVD =
14572         buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst",
14573                      VD->hasAttrs() ? &VD->getAttrs() : nullptr);
14574     DeclRefExpr *PseudoDstExpr =
14575         buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
14576     // For arrays generate assignment operation for single element and replace
14577     // it by the original array element in CodeGen.
14578     ExprResult AssignmentOp =
14579         BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr,
14580                    PseudoSrcExpr);
14581     if (AssignmentOp.isInvalid())
14582       continue;
14583     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
14584                                        /*DiscardedValue*/ false);
14585     if (AssignmentOp.isInvalid())
14586       continue;
14587 
14588     DSAStack->addDSA(VD, DE, OMPC_copyin);
14589     Vars.push_back(DE);
14590     SrcExprs.push_back(PseudoSrcExpr);
14591     DstExprs.push_back(PseudoDstExpr);
14592     AssignmentOps.push_back(AssignmentOp.get());
14593   }
14594 
14595   if (Vars.empty())
14596     return nullptr;
14597 
14598   return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
14599                                  SrcExprs, DstExprs, AssignmentOps);
14600 }
14601 
14602 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
14603                                               SourceLocation StartLoc,
14604                                               SourceLocation LParenLoc,
14605                                               SourceLocation EndLoc) {
14606   SmallVector<Expr *, 8> Vars;
14607   SmallVector<Expr *, 8> SrcExprs;
14608   SmallVector<Expr *, 8> DstExprs;
14609   SmallVector<Expr *, 8> AssignmentOps;
14610   for (Expr *RefExpr : VarList) {
14611     assert(RefExpr && "NULL expr in OpenMP linear clause.");
14612     SourceLocation ELoc;
14613     SourceRange ERange;
14614     Expr *SimpleRefExpr = RefExpr;
14615     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
14616     if (Res.second) {
14617       // It will be analyzed later.
14618       Vars.push_back(RefExpr);
14619       SrcExprs.push_back(nullptr);
14620       DstExprs.push_back(nullptr);
14621       AssignmentOps.push_back(nullptr);
14622     }
14623     ValueDecl *D = Res.first;
14624     if (!D)
14625       continue;
14626 
14627     QualType Type = D->getType();
14628     auto *VD = dyn_cast<VarDecl>(D);
14629 
14630     // OpenMP [2.14.4.2, Restrictions, p.2]
14631     //  A list item that appears in a copyprivate clause may not appear in a
14632     //  private or firstprivate clause on the single construct.
14633     if (!VD || !DSAStack->isThreadPrivate(VD)) {
14634       DSAStackTy::DSAVarData DVar =
14635           DSAStack->getTopDSA(D, /*FromParent=*/false);
14636       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
14637           DVar.RefExpr) {
14638         Diag(ELoc, diag::err_omp_wrong_dsa)
14639             << getOpenMPClauseName(DVar.CKind)
14640             << getOpenMPClauseName(OMPC_copyprivate);
14641         reportOriginalDsa(*this, DSAStack, D, DVar);
14642         continue;
14643       }
14644 
14645       // OpenMP [2.11.4.2, Restrictions, p.1]
14646       //  All list items that appear in a copyprivate clause must be either
14647       //  threadprivate or private in the enclosing context.
14648       if (DVar.CKind == OMPC_unknown) {
14649         DVar = DSAStack->getImplicitDSA(D, false);
14650         if (DVar.CKind == OMPC_shared) {
14651           Diag(ELoc, diag::err_omp_required_access)
14652               << getOpenMPClauseName(OMPC_copyprivate)
14653               << "threadprivate or private in the enclosing context";
14654           reportOriginalDsa(*this, DSAStack, D, DVar);
14655           continue;
14656         }
14657       }
14658     }
14659 
14660     // Variably modified types are not supported.
14661     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
14662       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
14663           << getOpenMPClauseName(OMPC_copyprivate) << Type
14664           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
14665       bool IsDecl =
14666           !VD ||
14667           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
14668       Diag(D->getLocation(),
14669            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
14670           << D;
14671       continue;
14672     }
14673 
14674     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
14675     //  A variable of class type (or array thereof) that appears in a
14676     //  copyin clause requires an accessible, unambiguous copy assignment
14677     //  operator for the class type.
14678     Type = Context.getBaseElementType(Type.getNonReferenceType())
14679                .getUnqualifiedType();
14680     VarDecl *SrcVD =
14681         buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src",
14682                      D->hasAttrs() ? &D->getAttrs() : nullptr);
14683     DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
14684     VarDecl *DstVD =
14685         buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst",
14686                      D->hasAttrs() ? &D->getAttrs() : nullptr);
14687     DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
14688     ExprResult AssignmentOp = BuildBinOp(
14689         DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr);
14690     if (AssignmentOp.isInvalid())
14691       continue;
14692     AssignmentOp =
14693         ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
14694     if (AssignmentOp.isInvalid())
14695       continue;
14696 
14697     // No need to mark vars as copyprivate, they are already threadprivate or
14698     // implicitly private.
14699     assert(VD || isOpenMPCapturedDecl(D));
14700     Vars.push_back(
14701         VD ? RefExpr->IgnoreParens()
14702            : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
14703     SrcExprs.push_back(PseudoSrcExpr);
14704     DstExprs.push_back(PseudoDstExpr);
14705     AssignmentOps.push_back(AssignmentOp.get());
14706   }
14707 
14708   if (Vars.empty())
14709     return nullptr;
14710 
14711   return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
14712                                       Vars, SrcExprs, DstExprs, AssignmentOps);
14713 }
14714 
14715 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
14716                                         SourceLocation StartLoc,
14717                                         SourceLocation LParenLoc,
14718                                         SourceLocation EndLoc) {
14719   if (VarList.empty())
14720     return nullptr;
14721 
14722   return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
14723 }
14724 
14725 OMPClause *
14726 Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
14727                               SourceLocation DepLoc, SourceLocation ColonLoc,
14728                               ArrayRef<Expr *> VarList, SourceLocation StartLoc,
14729                               SourceLocation LParenLoc, SourceLocation EndLoc) {
14730   if (DSAStack->getCurrentDirective() == OMPD_ordered &&
14731       DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
14732     Diag(DepLoc, diag::err_omp_unexpected_clause_value)
14733         << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
14734     return nullptr;
14735   }
14736   if (DSAStack->getCurrentDirective() != OMPD_ordered &&
14737       (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
14738        DepKind == OMPC_DEPEND_sink)) {
14739     unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
14740     Diag(DepLoc, diag::err_omp_unexpected_clause_value)
14741         << getListOfPossibleValues(OMPC_depend, /*First=*/0,
14742                                    /*Last=*/OMPC_DEPEND_unknown, Except)
14743         << getOpenMPClauseName(OMPC_depend);
14744     return nullptr;
14745   }
14746   SmallVector<Expr *, 8> Vars;
14747   DSAStackTy::OperatorOffsetTy OpsOffs;
14748   llvm::APSInt DepCounter(/*BitWidth=*/32);
14749   llvm::APSInt TotalDepCount(/*BitWidth=*/32);
14750   if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
14751     if (const Expr *OrderedCountExpr =
14752             DSAStack->getParentOrderedRegionParam().first) {
14753       TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
14754       TotalDepCount.setIsUnsigned(/*Val=*/true);
14755     }
14756   }
14757   for (Expr *RefExpr : VarList) {
14758     assert(RefExpr && "NULL expr in OpenMP shared clause.");
14759     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
14760       // It will be analyzed later.
14761       Vars.push_back(RefExpr);
14762       continue;
14763     }
14764 
14765     SourceLocation ELoc = RefExpr->getExprLoc();
14766     Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
14767     if (DepKind == OMPC_DEPEND_sink) {
14768       if (DSAStack->getParentOrderedRegionParam().first &&
14769           DepCounter >= TotalDepCount) {
14770         Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
14771         continue;
14772       }
14773       ++DepCounter;
14774       // OpenMP  [2.13.9, Summary]
14775       // depend(dependence-type : vec), where dependence-type is:
14776       // 'sink' and where vec is the iteration vector, which has the form:
14777       //  x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
14778       // where n is the value specified by the ordered clause in the loop
14779       // directive, xi denotes the loop iteration variable of the i-th nested
14780       // loop associated with the loop directive, and di is a constant
14781       // non-negative integer.
14782       if (CurContext->isDependentContext()) {
14783         // It will be analyzed later.
14784         Vars.push_back(RefExpr);
14785         continue;
14786       }
14787       SimpleExpr = SimpleExpr->IgnoreImplicit();
14788       OverloadedOperatorKind OOK = OO_None;
14789       SourceLocation OOLoc;
14790       Expr *LHS = SimpleExpr;
14791       Expr *RHS = nullptr;
14792       if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
14793         OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
14794         OOLoc = BO->getOperatorLoc();
14795         LHS = BO->getLHS()->IgnoreParenImpCasts();
14796         RHS = BO->getRHS()->IgnoreParenImpCasts();
14797       } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
14798         OOK = OCE->getOperator();
14799         OOLoc = OCE->getOperatorLoc();
14800         LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
14801         RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
14802       } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
14803         OOK = MCE->getMethodDecl()
14804                   ->getNameInfo()
14805                   .getName()
14806                   .getCXXOverloadedOperator();
14807         OOLoc = MCE->getCallee()->getExprLoc();
14808         LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
14809         RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
14810       }
14811       SourceLocation ELoc;
14812       SourceRange ERange;
14813       auto Res = getPrivateItem(*this, LHS, ELoc, ERange);
14814       if (Res.second) {
14815         // It will be analyzed later.
14816         Vars.push_back(RefExpr);
14817       }
14818       ValueDecl *D = Res.first;
14819       if (!D)
14820         continue;
14821 
14822       if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
14823         Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
14824         continue;
14825       }
14826       if (RHS) {
14827         ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
14828             RHS, OMPC_depend, /*StrictlyPositive=*/false);
14829         if (RHSRes.isInvalid())
14830           continue;
14831       }
14832       if (!CurContext->isDependentContext() &&
14833           DSAStack->getParentOrderedRegionParam().first &&
14834           DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
14835         const ValueDecl *VD =
14836             DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue());
14837         if (VD)
14838           Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
14839               << 1 << VD;
14840         else
14841           Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
14842         continue;
14843       }
14844       OpsOffs.emplace_back(RHS, OOK);
14845     } else {
14846       auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
14847       if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
14848           (ASE &&
14849            !ASE->getBase()->getType().getNonReferenceType()->isPointerType() &&
14850            !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
14851         Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
14852             << RefExpr->getSourceRange();
14853         continue;
14854       }
14855 
14856       ExprResult Res;
14857       {
14858         Sema::TentativeAnalysisScope Trap(*this);
14859         Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf,
14860                                    RefExpr->IgnoreParenImpCasts());
14861       }
14862       if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) {
14863         Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
14864             << RefExpr->getSourceRange();
14865         continue;
14866       }
14867     }
14868     Vars.push_back(RefExpr->IgnoreParenImpCasts());
14869   }
14870 
14871   if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
14872       TotalDepCount > VarList.size() &&
14873       DSAStack->getParentOrderedRegionParam().first &&
14874       DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
14875     Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
14876         << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
14877   }
14878   if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
14879       Vars.empty())
14880     return nullptr;
14881 
14882   auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
14883                                     DepKind, DepLoc, ColonLoc, Vars,
14884                                     TotalDepCount.getZExtValue());
14885   if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
14886       DSAStack->isParentOrderedRegion())
14887     DSAStack->addDoacrossDependClause(C, OpsOffs);
14888   return C;
14889 }
14890 
14891 OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
14892                                          SourceLocation LParenLoc,
14893                                          SourceLocation EndLoc) {
14894   Expr *ValExpr = Device;
14895   Stmt *HelperValStmt = nullptr;
14896 
14897   // OpenMP [2.9.1, Restrictions]
14898   // The device expression must evaluate to a non-negative integer value.
14899   if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
14900                                  /*StrictlyPositive=*/false))
14901     return nullptr;
14902 
14903   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
14904   OpenMPDirectiveKind CaptureRegion =
14905       getOpenMPCaptureRegionForClause(DKind, OMPC_device, LangOpts.OpenMP);
14906   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
14907     ValExpr = MakeFullExpr(ValExpr).get();
14908     llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
14909     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
14910     HelperValStmt = buildPreInits(Context, Captures);
14911   }
14912 
14913   return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion,
14914                                        StartLoc, LParenLoc, EndLoc);
14915 }
14916 
14917 static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
14918                               DSAStackTy *Stack, QualType QTy,
14919                               bool FullCheck = true) {
14920   NamedDecl *ND;
14921   if (QTy->isIncompleteType(&ND)) {
14922     SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
14923     return false;
14924   }
14925   if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
14926       !QTy.isTriviallyCopyableType(SemaRef.Context))
14927     SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
14928   return true;
14929 }
14930 
14931 /// Return true if it can be proven that the provided array expression
14932 /// (array section or array subscript) does NOT specify the whole size of the
14933 /// array whose base type is \a BaseQTy.
14934 static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
14935                                                         const Expr *E,
14936                                                         QualType BaseQTy) {
14937   const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
14938 
14939   // If this is an array subscript, it refers to the whole size if the size of
14940   // the dimension is constant and equals 1. Also, an array section assumes the
14941   // format of an array subscript if no colon is used.
14942   if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
14943     if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
14944       return ATy->getSize().getSExtValue() != 1;
14945     // Size can't be evaluated statically.
14946     return false;
14947   }
14948 
14949   assert(OASE && "Expecting array section if not an array subscript.");
14950   const Expr *LowerBound = OASE->getLowerBound();
14951   const Expr *Length = OASE->getLength();
14952 
14953   // If there is a lower bound that does not evaluates to zero, we are not
14954   // covering the whole dimension.
14955   if (LowerBound) {
14956     Expr::EvalResult Result;
14957     if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext()))
14958       return false; // Can't get the integer value as a constant.
14959 
14960     llvm::APSInt ConstLowerBound = Result.Val.getInt();
14961     if (ConstLowerBound.getSExtValue())
14962       return true;
14963   }
14964 
14965   // If we don't have a length we covering the whole dimension.
14966   if (!Length)
14967     return false;
14968 
14969   // If the base is a pointer, we don't have a way to get the size of the
14970   // pointee.
14971   if (BaseQTy->isPointerType())
14972     return false;
14973 
14974   // We can only check if the length is the same as the size of the dimension
14975   // if we have a constant array.
14976   const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
14977   if (!CATy)
14978     return false;
14979 
14980   Expr::EvalResult Result;
14981   if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
14982     return false; // Can't get the integer value as a constant.
14983 
14984   llvm::APSInt ConstLength = Result.Val.getInt();
14985   return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
14986 }
14987 
14988 // Return true if it can be proven that the provided array expression (array
14989 // section or array subscript) does NOT specify a single element of the array
14990 // whose base type is \a BaseQTy.
14991 static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
14992                                                         const Expr *E,
14993                                                         QualType BaseQTy) {
14994   const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
14995 
14996   // An array subscript always refer to a single element. Also, an array section
14997   // assumes the format of an array subscript if no colon is used.
14998   if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
14999     return false;
15000 
15001   assert(OASE && "Expecting array section if not an array subscript.");
15002   const Expr *Length = OASE->getLength();
15003 
15004   // If we don't have a length we have to check if the array has unitary size
15005   // for this dimension. Also, we should always expect a length if the base type
15006   // is pointer.
15007   if (!Length) {
15008     if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
15009       return ATy->getSize().getSExtValue() != 1;
15010     // We cannot assume anything.
15011     return false;
15012   }
15013 
15014   // Check if the length evaluates to 1.
15015   Expr::EvalResult Result;
15016   if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
15017     return false; // Can't get the integer value as a constant.
15018 
15019   llvm::APSInt ConstLength = Result.Val.getInt();
15020   return ConstLength.getSExtValue() != 1;
15021 }
15022 
15023 // Return the expression of the base of the mappable expression or null if it
15024 // cannot be determined and do all the necessary checks to see if the expression
15025 // is valid as a standalone mappable expression. In the process, record all the
15026 // components of the expression.
15027 static const Expr *checkMapClauseExpressionBase(
15028     Sema &SemaRef, Expr *E,
15029     OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
15030     OpenMPClauseKind CKind, bool NoDiagnose) {
15031   SourceLocation ELoc = E->getExprLoc();
15032   SourceRange ERange = E->getSourceRange();
15033 
15034   // The base of elements of list in a map clause have to be either:
15035   //  - a reference to variable or field.
15036   //  - a member expression.
15037   //  - an array expression.
15038   //
15039   // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
15040   // reference to 'r'.
15041   //
15042   // If we have:
15043   //
15044   // struct SS {
15045   //   Bla S;
15046   //   foo() {
15047   //     #pragma omp target map (S.Arr[:12]);
15048   //   }
15049   // }
15050   //
15051   // We want to retrieve the member expression 'this->S';
15052 
15053   const Expr *RelevantExpr = nullptr;
15054 
15055   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
15056   //  If a list item is an array section, it must specify contiguous storage.
15057   //
15058   // For this restriction it is sufficient that we make sure only references
15059   // to variables or fields and array expressions, and that no array sections
15060   // exist except in the rightmost expression (unless they cover the whole
15061   // dimension of the array). E.g. these would be invalid:
15062   //
15063   //   r.ArrS[3:5].Arr[6:7]
15064   //
15065   //   r.ArrS[3:5].x
15066   //
15067   // but these would be valid:
15068   //   r.ArrS[3].Arr[6:7]
15069   //
15070   //   r.ArrS[3].x
15071 
15072   bool AllowUnitySizeArraySection = true;
15073   bool AllowWholeSizeArraySection = true;
15074 
15075   while (!RelevantExpr) {
15076     E = E->IgnoreParenImpCasts();
15077 
15078     if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
15079       if (!isa<VarDecl>(CurE->getDecl()))
15080         return nullptr;
15081 
15082       RelevantExpr = CurE;
15083 
15084       // If we got a reference to a declaration, we should not expect any array
15085       // section before that.
15086       AllowUnitySizeArraySection = false;
15087       AllowWholeSizeArraySection = false;
15088 
15089       // Record the component.
15090       CurComponents.emplace_back(CurE, CurE->getDecl());
15091     } else if (auto *CurE = dyn_cast<MemberExpr>(E)) {
15092       Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts();
15093 
15094       if (isa<CXXThisExpr>(BaseE))
15095         // We found a base expression: this->Val.
15096         RelevantExpr = CurE;
15097       else
15098         E = BaseE;
15099 
15100       if (!isa<FieldDecl>(CurE->getMemberDecl())) {
15101         if (!NoDiagnose) {
15102           SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
15103               << CurE->getSourceRange();
15104           return nullptr;
15105         }
15106         if (RelevantExpr)
15107           return nullptr;
15108         continue;
15109       }
15110 
15111       auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
15112 
15113       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
15114       //  A bit-field cannot appear in a map clause.
15115       //
15116       if (FD->isBitField()) {
15117         if (!NoDiagnose) {
15118           SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
15119               << CurE->getSourceRange() << getOpenMPClauseName(CKind);
15120           return nullptr;
15121         }
15122         if (RelevantExpr)
15123           return nullptr;
15124         continue;
15125       }
15126 
15127       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
15128       //  If the type of a list item is a reference to a type T then the type
15129       //  will be considered to be T for all purposes of this clause.
15130       QualType CurType = BaseE->getType().getNonReferenceType();
15131 
15132       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
15133       //  A list item cannot be a variable that is a member of a structure with
15134       //  a union type.
15135       //
15136       if (CurType->isUnionType()) {
15137         if (!NoDiagnose) {
15138           SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
15139               << CurE->getSourceRange();
15140           return nullptr;
15141         }
15142         continue;
15143       }
15144 
15145       // If we got a member expression, we should not expect any array section
15146       // before that:
15147       //
15148       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
15149       //  If a list item is an element of a structure, only the rightmost symbol
15150       //  of the variable reference can be an array section.
15151       //
15152       AllowUnitySizeArraySection = false;
15153       AllowWholeSizeArraySection = false;
15154 
15155       // Record the component.
15156       CurComponents.emplace_back(CurE, FD);
15157     } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
15158       E = CurE->getBase()->IgnoreParenImpCasts();
15159 
15160       if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
15161         if (!NoDiagnose) {
15162           SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
15163               << 0 << CurE->getSourceRange();
15164           return nullptr;
15165         }
15166         continue;
15167       }
15168 
15169       // If we got an array subscript that express the whole dimension we
15170       // can have any array expressions before. If it only expressing part of
15171       // the dimension, we can only have unitary-size array expressions.
15172       if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
15173                                                       E->getType()))
15174         AllowWholeSizeArraySection = false;
15175 
15176       if (const auto *TE = dyn_cast<CXXThisExpr>(E)) {
15177         Expr::EvalResult Result;
15178         if (CurE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext())) {
15179           if (!Result.Val.getInt().isNullValue()) {
15180             SemaRef.Diag(CurE->getIdx()->getExprLoc(),
15181                          diag::err_omp_invalid_map_this_expr);
15182             SemaRef.Diag(CurE->getIdx()->getExprLoc(),
15183                          diag::note_omp_invalid_subscript_on_this_ptr_map);
15184           }
15185         }
15186         RelevantExpr = TE;
15187       }
15188 
15189       // Record the component - we don't have any declaration associated.
15190       CurComponents.emplace_back(CurE, nullptr);
15191     } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
15192       assert(!NoDiagnose && "Array sections cannot be implicitly mapped.");
15193       E = CurE->getBase()->IgnoreParenImpCasts();
15194 
15195       QualType CurType =
15196           OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
15197 
15198       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
15199       //  If the type of a list item is a reference to a type T then the type
15200       //  will be considered to be T for all purposes of this clause.
15201       if (CurType->isReferenceType())
15202         CurType = CurType->getPointeeType();
15203 
15204       bool IsPointer = CurType->isAnyPointerType();
15205 
15206       if (!IsPointer && !CurType->isArrayType()) {
15207         SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
15208             << 0 << CurE->getSourceRange();
15209         return nullptr;
15210       }
15211 
15212       bool NotWhole =
15213           checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
15214       bool NotUnity =
15215           checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
15216 
15217       if (AllowWholeSizeArraySection) {
15218         // Any array section is currently allowed. Allowing a whole size array
15219         // section implies allowing a unity array section as well.
15220         //
15221         // If this array section refers to the whole dimension we can still
15222         // accept other array sections before this one, except if the base is a
15223         // pointer. Otherwise, only unitary sections are accepted.
15224         if (NotWhole || IsPointer)
15225           AllowWholeSizeArraySection = false;
15226       } else if (AllowUnitySizeArraySection && NotUnity) {
15227         // A unity or whole array section is not allowed and that is not
15228         // compatible with the properties of the current array section.
15229         SemaRef.Diag(
15230             ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
15231             << CurE->getSourceRange();
15232         return nullptr;
15233       }
15234 
15235       if (const auto *TE = dyn_cast<CXXThisExpr>(E)) {
15236         Expr::EvalResult ResultR;
15237         Expr::EvalResult ResultL;
15238         if (CurE->getLength()->EvaluateAsInt(ResultR,
15239                                              SemaRef.getASTContext())) {
15240           if (!ResultR.Val.getInt().isOneValue()) {
15241             SemaRef.Diag(CurE->getLength()->getExprLoc(),
15242                          diag::err_omp_invalid_map_this_expr);
15243             SemaRef.Diag(CurE->getLength()->getExprLoc(),
15244                          diag::note_omp_invalid_length_on_this_ptr_mapping);
15245           }
15246         }
15247         if (CurE->getLowerBound() && CurE->getLowerBound()->EvaluateAsInt(
15248                                         ResultL, SemaRef.getASTContext())) {
15249           if (!ResultL.Val.getInt().isNullValue()) {
15250             SemaRef.Diag(CurE->getLowerBound()->getExprLoc(),
15251                          diag::err_omp_invalid_map_this_expr);
15252             SemaRef.Diag(CurE->getLowerBound()->getExprLoc(),
15253                          diag::note_omp_invalid_lower_bound_on_this_ptr_mapping);
15254           }
15255         }
15256         RelevantExpr = TE;
15257       }
15258 
15259       // Record the component - we don't have any declaration associated.
15260       CurComponents.emplace_back(CurE, nullptr);
15261     } else {
15262       if (!NoDiagnose) {
15263         // If nothing else worked, this is not a valid map clause expression.
15264         SemaRef.Diag(
15265             ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
15266             << ERange;
15267       }
15268       return nullptr;
15269     }
15270   }
15271 
15272   return RelevantExpr;
15273 }
15274 
15275 // Return true if expression E associated with value VD has conflicts with other
15276 // map information.
15277 static bool checkMapConflicts(
15278     Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
15279     bool CurrentRegionOnly,
15280     OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
15281     OpenMPClauseKind CKind) {
15282   assert(VD && E);
15283   SourceLocation ELoc = E->getExprLoc();
15284   SourceRange ERange = E->getSourceRange();
15285 
15286   // In order to easily check the conflicts we need to match each component of
15287   // the expression under test with the components of the expressions that are
15288   // already in the stack.
15289 
15290   assert(!CurComponents.empty() && "Map clause expression with no components!");
15291   assert(CurComponents.back().getAssociatedDeclaration() == VD &&
15292          "Map clause expression with unexpected base!");
15293 
15294   // Variables to help detecting enclosing problems in data environment nests.
15295   bool IsEnclosedByDataEnvironmentExpr = false;
15296   const Expr *EnclosingExpr = nullptr;
15297 
15298   bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
15299       VD, CurrentRegionOnly,
15300       [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
15301        ERange, CKind, &EnclosingExpr,
15302        CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
15303                           StackComponents,
15304                       OpenMPClauseKind) {
15305         assert(!StackComponents.empty() &&
15306                "Map clause expression with no components!");
15307         assert(StackComponents.back().getAssociatedDeclaration() == VD &&
15308                "Map clause expression with unexpected base!");
15309         (void)VD;
15310 
15311         // The whole expression in the stack.
15312         const Expr *RE = StackComponents.front().getAssociatedExpression();
15313 
15314         // Expressions must start from the same base. Here we detect at which
15315         // point both expressions diverge from each other and see if we can
15316         // detect if the memory referred to both expressions is contiguous and
15317         // do not overlap.
15318         auto CI = CurComponents.rbegin();
15319         auto CE = CurComponents.rend();
15320         auto SI = StackComponents.rbegin();
15321         auto SE = StackComponents.rend();
15322         for (; CI != CE && SI != SE; ++CI, ++SI) {
15323 
15324           // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
15325           //  At most one list item can be an array item derived from a given
15326           //  variable in map clauses of the same construct.
15327           if (CurrentRegionOnly &&
15328               (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
15329                isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
15330               (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
15331                isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
15332             SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
15333                          diag::err_omp_multiple_array_items_in_map_clause)
15334                 << CI->getAssociatedExpression()->getSourceRange();
15335             SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
15336                          diag::note_used_here)
15337                 << SI->getAssociatedExpression()->getSourceRange();
15338             return true;
15339           }
15340 
15341           // Do both expressions have the same kind?
15342           if (CI->getAssociatedExpression()->getStmtClass() !=
15343               SI->getAssociatedExpression()->getStmtClass())
15344             break;
15345 
15346           // Are we dealing with different variables/fields?
15347           if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
15348             break;
15349         }
15350         // Check if the extra components of the expressions in the enclosing
15351         // data environment are redundant for the current base declaration.
15352         // If they are, the maps completely overlap, which is legal.
15353         for (; SI != SE; ++SI) {
15354           QualType Type;
15355           if (const auto *ASE =
15356                   dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
15357             Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
15358           } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(
15359                          SI->getAssociatedExpression())) {
15360             const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
15361             Type =
15362                 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
15363           }
15364           if (Type.isNull() || Type->isAnyPointerType() ||
15365               checkArrayExpressionDoesNotReferToWholeSize(
15366                   SemaRef, SI->getAssociatedExpression(), Type))
15367             break;
15368         }
15369 
15370         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
15371         //  List items of map clauses in the same construct must not share
15372         //  original storage.
15373         //
15374         // If the expressions are exactly the same or one is a subset of the
15375         // other, it means they are sharing storage.
15376         if (CI == CE && SI == SE) {
15377           if (CurrentRegionOnly) {
15378             if (CKind == OMPC_map) {
15379               SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
15380             } else {
15381               assert(CKind == OMPC_to || CKind == OMPC_from);
15382               SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
15383                   << ERange;
15384             }
15385             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
15386                 << RE->getSourceRange();
15387             return true;
15388           }
15389           // If we find the same expression in the enclosing data environment,
15390           // that is legal.
15391           IsEnclosedByDataEnvironmentExpr = true;
15392           return false;
15393         }
15394 
15395         QualType DerivedType =
15396             std::prev(CI)->getAssociatedDeclaration()->getType();
15397         SourceLocation DerivedLoc =
15398             std::prev(CI)->getAssociatedExpression()->getExprLoc();
15399 
15400         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
15401         //  If the type of a list item is a reference to a type T then the type
15402         //  will be considered to be T for all purposes of this clause.
15403         DerivedType = DerivedType.getNonReferenceType();
15404 
15405         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
15406         //  A variable for which the type is pointer and an array section
15407         //  derived from that variable must not appear as list items of map
15408         //  clauses of the same construct.
15409         //
15410         // Also, cover one of the cases in:
15411         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
15412         //  If any part of the original storage of a list item has corresponding
15413         //  storage in the device data environment, all of the original storage
15414         //  must have corresponding storage in the device data environment.
15415         //
15416         if (DerivedType->isAnyPointerType()) {
15417           if (CI == CE || SI == SE) {
15418             SemaRef.Diag(
15419                 DerivedLoc,
15420                 diag::err_omp_pointer_mapped_along_with_derived_section)
15421                 << DerivedLoc;
15422             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
15423                 << RE->getSourceRange();
15424             return true;
15425           }
15426           if (CI->getAssociatedExpression()->getStmtClass() !=
15427                          SI->getAssociatedExpression()->getStmtClass() ||
15428                      CI->getAssociatedDeclaration()->getCanonicalDecl() ==
15429                          SI->getAssociatedDeclaration()->getCanonicalDecl()) {
15430             assert(CI != CE && SI != SE);
15431             SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced)
15432                 << DerivedLoc;
15433             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
15434                 << RE->getSourceRange();
15435             return true;
15436           }
15437         }
15438 
15439         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
15440         //  List items of map clauses in the same construct must not share
15441         //  original storage.
15442         //
15443         // An expression is a subset of the other.
15444         if (CurrentRegionOnly && (CI == CE || SI == SE)) {
15445           if (CKind == OMPC_map) {
15446             if (CI != CE || SI != SE) {
15447               // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is
15448               // a pointer.
15449               auto Begin =
15450                   CI != CE ? CurComponents.begin() : StackComponents.begin();
15451               auto End = CI != CE ? CurComponents.end() : StackComponents.end();
15452               auto It = Begin;
15453               while (It != End && !It->getAssociatedDeclaration())
15454                 std::advance(It, 1);
15455               assert(It != End &&
15456                      "Expected at least one component with the declaration.");
15457               if (It != Begin && It->getAssociatedDeclaration()
15458                                      ->getType()
15459                                      .getCanonicalType()
15460                                      ->isAnyPointerType()) {
15461                 IsEnclosedByDataEnvironmentExpr = false;
15462                 EnclosingExpr = nullptr;
15463                 return false;
15464               }
15465             }
15466             SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
15467           } else {
15468             assert(CKind == OMPC_to || CKind == OMPC_from);
15469             SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
15470                 << ERange;
15471           }
15472           SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
15473               << RE->getSourceRange();
15474           return true;
15475         }
15476 
15477         // The current expression uses the same base as other expression in the
15478         // data environment but does not contain it completely.
15479         if (!CurrentRegionOnly && SI != SE)
15480           EnclosingExpr = RE;
15481 
15482         // The current expression is a subset of the expression in the data
15483         // environment.
15484         IsEnclosedByDataEnvironmentExpr |=
15485             (!CurrentRegionOnly && CI != CE && SI == SE);
15486 
15487         return false;
15488       });
15489 
15490   if (CurrentRegionOnly)
15491     return FoundError;
15492 
15493   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
15494   //  If any part of the original storage of a list item has corresponding
15495   //  storage in the device data environment, all of the original storage must
15496   //  have corresponding storage in the device data environment.
15497   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
15498   //  If a list item is an element of a structure, and a different element of
15499   //  the structure has a corresponding list item in the device data environment
15500   //  prior to a task encountering the construct associated with the map clause,
15501   //  then the list item must also have a corresponding list item in the device
15502   //  data environment prior to the task encountering the construct.
15503   //
15504   if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
15505     SemaRef.Diag(ELoc,
15506                  diag::err_omp_original_storage_is_shared_and_does_not_contain)
15507         << ERange;
15508     SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
15509         << EnclosingExpr->getSourceRange();
15510     return true;
15511   }
15512 
15513   return FoundError;
15514 }
15515 
15516 // Look up the user-defined mapper given the mapper name and mapped type, and
15517 // build a reference to it.
15518 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S,
15519                                             CXXScopeSpec &MapperIdScopeSpec,
15520                                             const DeclarationNameInfo &MapperId,
15521                                             QualType Type,
15522                                             Expr *UnresolvedMapper) {
15523   if (MapperIdScopeSpec.isInvalid())
15524     return ExprError();
15525   // Get the actual type for the array type.
15526   if (Type->isArrayType()) {
15527     assert(Type->getAsArrayTypeUnsafe() && "Expect to get a valid array type");
15528     Type = Type->getAsArrayTypeUnsafe()->getElementType().getCanonicalType();
15529   }
15530   // Find all user-defined mappers with the given MapperId.
15531   SmallVector<UnresolvedSet<8>, 4> Lookups;
15532   LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName);
15533   Lookup.suppressDiagnostics();
15534   if (S) {
15535     while (S && SemaRef.LookupParsedName(Lookup, S, &MapperIdScopeSpec)) {
15536       NamedDecl *D = Lookup.getRepresentativeDecl();
15537       while (S && !S->isDeclScope(D))
15538         S = S->getParent();
15539       if (S)
15540         S = S->getParent();
15541       Lookups.emplace_back();
15542       Lookups.back().append(Lookup.begin(), Lookup.end());
15543       Lookup.clear();
15544     }
15545   } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(UnresolvedMapper)) {
15546     // Extract the user-defined mappers with the given MapperId.
15547     Lookups.push_back(UnresolvedSet<8>());
15548     for (NamedDecl *D : ULE->decls()) {
15549       auto *DMD = cast<OMPDeclareMapperDecl>(D);
15550       assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation.");
15551       Lookups.back().addDecl(DMD);
15552     }
15553   }
15554   // Defer the lookup for dependent types. The results will be passed through
15555   // UnresolvedMapper on instantiation.
15556   if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() ||
15557       Type->isInstantiationDependentType() ||
15558       Type->containsUnexpandedParameterPack() ||
15559       filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) {
15560         return !D->isInvalidDecl() &&
15561                (D->getType()->isDependentType() ||
15562                 D->getType()->isInstantiationDependentType() ||
15563                 D->getType()->containsUnexpandedParameterPack());
15564       })) {
15565     UnresolvedSet<8> URS;
15566     for (const UnresolvedSet<8> &Set : Lookups) {
15567       if (Set.empty())
15568         continue;
15569       URS.append(Set.begin(), Set.end());
15570     }
15571     return UnresolvedLookupExpr::Create(
15572         SemaRef.Context, /*NamingClass=*/nullptr,
15573         MapperIdScopeSpec.getWithLocInContext(SemaRef.Context), MapperId,
15574         /*ADL=*/false, /*Overloaded=*/true, URS.begin(), URS.end());
15575   }
15576   SourceLocation Loc = MapperId.getLoc();
15577   // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
15578   //  The type must be of struct, union or class type in C and C++
15579   if (!Type->isStructureOrClassType() && !Type->isUnionType() &&
15580       (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default")) {
15581     SemaRef.Diag(Loc, diag::err_omp_mapper_wrong_type);
15582     return ExprError();
15583   }
15584   // Perform argument dependent lookup.
15585   if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet())
15586     argumentDependentLookup(SemaRef, MapperId, Loc, Type, Lookups);
15587   // Return the first user-defined mapper with the desired type.
15588   if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
15589           Lookups, [&SemaRef, Type](ValueDecl *D) -> ValueDecl * {
15590             if (!D->isInvalidDecl() &&
15591                 SemaRef.Context.hasSameType(D->getType(), Type))
15592               return D;
15593             return nullptr;
15594           }))
15595     return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc);
15596   // Find the first user-defined mapper with a type derived from the desired
15597   // type.
15598   if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
15599           Lookups, [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * {
15600             if (!D->isInvalidDecl() &&
15601                 SemaRef.IsDerivedFrom(Loc, Type, D->getType()) &&
15602                 !Type.isMoreQualifiedThan(D->getType()))
15603               return D;
15604             return nullptr;
15605           })) {
15606     CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
15607                        /*DetectVirtual=*/false);
15608     if (SemaRef.IsDerivedFrom(Loc, Type, VD->getType(), Paths)) {
15609       if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
15610               VD->getType().getUnqualifiedType()))) {
15611         if (SemaRef.CheckBaseClassAccess(
15612                 Loc, VD->getType(), Type, Paths.front(),
15613                 /*DiagID=*/0) != Sema::AR_inaccessible) {
15614           return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc);
15615         }
15616       }
15617     }
15618   }
15619   // Report error if a mapper is specified, but cannot be found.
15620   if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") {
15621     SemaRef.Diag(Loc, diag::err_omp_invalid_mapper)
15622         << Type << MapperId.getName();
15623     return ExprError();
15624   }
15625   return ExprEmpty();
15626 }
15627 
15628 namespace {
15629 // Utility struct that gathers all the related lists associated with a mappable
15630 // expression.
15631 struct MappableVarListInfo {
15632   // The list of expressions.
15633   ArrayRef<Expr *> VarList;
15634   // The list of processed expressions.
15635   SmallVector<Expr *, 16> ProcessedVarList;
15636   // The mappble components for each expression.
15637   OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
15638   // The base declaration of the variable.
15639   SmallVector<ValueDecl *, 16> VarBaseDeclarations;
15640   // The reference to the user-defined mapper associated with every expression.
15641   SmallVector<Expr *, 16> UDMapperList;
15642 
15643   MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
15644     // We have a list of components and base declarations for each entry in the
15645     // variable list.
15646     VarComponents.reserve(VarList.size());
15647     VarBaseDeclarations.reserve(VarList.size());
15648   }
15649 };
15650 }
15651 
15652 // Check the validity of the provided variable list for the provided clause kind
15653 // \a CKind. In the check process the valid expressions, mappable expression
15654 // components, variables, and user-defined mappers are extracted and used to
15655 // fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a
15656 // UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec,
15657 // and \a MapperId are expected to be valid if the clause kind is 'map'.
15658 static void checkMappableExpressionList(
15659     Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind,
15660     MappableVarListInfo &MVLI, SourceLocation StartLoc,
15661     CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId,
15662     ArrayRef<Expr *> UnresolvedMappers,
15663     OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
15664     bool IsMapTypeImplicit = false) {
15665   // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
15666   assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
15667          "Unexpected clause kind with mappable expressions!");
15668 
15669   // If the identifier of user-defined mapper is not specified, it is "default".
15670   // We do not change the actual name in this clause to distinguish whether a
15671   // mapper is specified explicitly, i.e., it is not explicitly specified when
15672   // MapperId.getName() is empty.
15673   if (!MapperId.getName() || MapperId.getName().isEmpty()) {
15674     auto &DeclNames = SemaRef.getASTContext().DeclarationNames;
15675     MapperId.setName(DeclNames.getIdentifier(
15676         &SemaRef.getASTContext().Idents.get("default")));
15677   }
15678 
15679   // Iterators to find the current unresolved mapper expression.
15680   auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end();
15681   bool UpdateUMIt = false;
15682   Expr *UnresolvedMapper = nullptr;
15683 
15684   // Keep track of the mappable components and base declarations in this clause.
15685   // Each entry in the list is going to have a list of components associated. We
15686   // record each set of the components so that we can build the clause later on.
15687   // In the end we should have the same amount of declarations and component
15688   // lists.
15689 
15690   for (Expr *RE : MVLI.VarList) {
15691     assert(RE && "Null expr in omp to/from/map clause");
15692     SourceLocation ELoc = RE->getExprLoc();
15693 
15694     // Find the current unresolved mapper expression.
15695     if (UpdateUMIt && UMIt != UMEnd) {
15696       UMIt++;
15697       assert(
15698           UMIt != UMEnd &&
15699           "Expect the size of UnresolvedMappers to match with that of VarList");
15700     }
15701     UpdateUMIt = true;
15702     if (UMIt != UMEnd)
15703       UnresolvedMapper = *UMIt;
15704 
15705     const Expr *VE = RE->IgnoreParenLValueCasts();
15706 
15707     if (VE->isValueDependent() || VE->isTypeDependent() ||
15708         VE->isInstantiationDependent() ||
15709         VE->containsUnexpandedParameterPack()) {
15710       // Try to find the associated user-defined mapper.
15711       ExprResult ER = buildUserDefinedMapperRef(
15712           SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
15713           VE->getType().getCanonicalType(), UnresolvedMapper);
15714       if (ER.isInvalid())
15715         continue;
15716       MVLI.UDMapperList.push_back(ER.get());
15717       // We can only analyze this information once the missing information is
15718       // resolved.
15719       MVLI.ProcessedVarList.push_back(RE);
15720       continue;
15721     }
15722 
15723     Expr *SimpleExpr = RE->IgnoreParenCasts();
15724 
15725     if (!RE->IgnoreParenImpCasts()->isLValue()) {
15726       SemaRef.Diag(ELoc,
15727                    diag::err_omp_expected_named_var_member_or_array_expression)
15728           << RE->getSourceRange();
15729       continue;
15730     }
15731 
15732     OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
15733     ValueDecl *CurDeclaration = nullptr;
15734 
15735     // Obtain the array or member expression bases if required. Also, fill the
15736     // components array with all the components identified in the process.
15737     const Expr *BE = checkMapClauseExpressionBase(
15738         SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false);
15739     if (!BE)
15740       continue;
15741 
15742     assert(!CurComponents.empty() &&
15743            "Invalid mappable expression information.");
15744 
15745     if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) {
15746       // Add store "this" pointer to class in DSAStackTy for future checking
15747       DSAS->addMappedClassesQualTypes(TE->getType());
15748       // Try to find the associated user-defined mapper.
15749       ExprResult ER = buildUserDefinedMapperRef(
15750           SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
15751           VE->getType().getCanonicalType(), UnresolvedMapper);
15752       if (ER.isInvalid())
15753         continue;
15754       MVLI.UDMapperList.push_back(ER.get());
15755       // Skip restriction checking for variable or field declarations
15756       MVLI.ProcessedVarList.push_back(RE);
15757       MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
15758       MVLI.VarComponents.back().append(CurComponents.begin(),
15759                                        CurComponents.end());
15760       MVLI.VarBaseDeclarations.push_back(nullptr);
15761       continue;
15762     }
15763 
15764     // For the following checks, we rely on the base declaration which is
15765     // expected to be associated with the last component. The declaration is
15766     // expected to be a variable or a field (if 'this' is being mapped).
15767     CurDeclaration = CurComponents.back().getAssociatedDeclaration();
15768     assert(CurDeclaration && "Null decl on map clause.");
15769     assert(
15770         CurDeclaration->isCanonicalDecl() &&
15771         "Expecting components to have associated only canonical declarations.");
15772 
15773     auto *VD = dyn_cast<VarDecl>(CurDeclaration);
15774     const auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
15775 
15776     assert((VD || FD) && "Only variables or fields are expected here!");
15777     (void)FD;
15778 
15779     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
15780     // threadprivate variables cannot appear in a map clause.
15781     // OpenMP 4.5 [2.10.5, target update Construct]
15782     // threadprivate variables cannot appear in a from clause.
15783     if (VD && DSAS->isThreadPrivate(VD)) {
15784       DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
15785       SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
15786           << getOpenMPClauseName(CKind);
15787       reportOriginalDsa(SemaRef, DSAS, VD, DVar);
15788       continue;
15789     }
15790 
15791     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
15792     //  A list item cannot appear in both a map clause and a data-sharing
15793     //  attribute clause on the same construct.
15794 
15795     // Check conflicts with other map clause expressions. We check the conflicts
15796     // with the current construct separately from the enclosing data
15797     // environment, because the restrictions are different. We only have to
15798     // check conflicts across regions for the map clauses.
15799     if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
15800                           /*CurrentRegionOnly=*/true, CurComponents, CKind))
15801       break;
15802     if (CKind == OMPC_map &&
15803         checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
15804                           /*CurrentRegionOnly=*/false, CurComponents, CKind))
15805       break;
15806 
15807     // OpenMP 4.5 [2.10.5, target update Construct]
15808     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
15809     //  If the type of a list item is a reference to a type T then the type will
15810     //  be considered to be T for all purposes of this clause.
15811     auto I = llvm::find_if(
15812         CurComponents,
15813         [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
15814           return MC.getAssociatedDeclaration();
15815         });
15816     assert(I != CurComponents.end() && "Null decl on map clause.");
15817     QualType Type =
15818         I->getAssociatedDeclaration()->getType().getNonReferenceType();
15819 
15820     // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
15821     // A list item in a to or from clause must have a mappable type.
15822     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
15823     //  A list item must have a mappable type.
15824     if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
15825                            DSAS, Type))
15826       continue;
15827 
15828     if (CKind == OMPC_map) {
15829       // target enter data
15830       // OpenMP [2.10.2, Restrictions, p. 99]
15831       // A map-type must be specified in all map clauses and must be either
15832       // to or alloc.
15833       OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
15834       if (DKind == OMPD_target_enter_data &&
15835           !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
15836         SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
15837             << (IsMapTypeImplicit ? 1 : 0)
15838             << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
15839             << getOpenMPDirectiveName(DKind);
15840         continue;
15841       }
15842 
15843       // target exit_data
15844       // OpenMP [2.10.3, Restrictions, p. 102]
15845       // A map-type must be specified in all map clauses and must be either
15846       // from, release, or delete.
15847       if (DKind == OMPD_target_exit_data &&
15848           !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
15849             MapType == OMPC_MAP_delete)) {
15850         SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
15851             << (IsMapTypeImplicit ? 1 : 0)
15852             << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
15853             << getOpenMPDirectiveName(DKind);
15854         continue;
15855       }
15856 
15857       // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
15858       // A list item cannot appear in both a map clause and a data-sharing
15859       // attribute clause on the same construct
15860       //
15861       // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
15862       // A list item cannot appear in both a map clause and a data-sharing
15863       // attribute clause on the same construct unless the construct is a
15864       // combined construct.
15865       if (VD && ((SemaRef.LangOpts.OpenMP <= 45 &&
15866                   isOpenMPTargetExecutionDirective(DKind)) ||
15867                  DKind == OMPD_target)) {
15868         DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
15869         if (isOpenMPPrivate(DVar.CKind)) {
15870           SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
15871               << getOpenMPClauseName(DVar.CKind)
15872               << getOpenMPClauseName(OMPC_map)
15873               << getOpenMPDirectiveName(DSAS->getCurrentDirective());
15874           reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar);
15875           continue;
15876         }
15877       }
15878     }
15879 
15880     // Try to find the associated user-defined mapper.
15881     ExprResult ER = buildUserDefinedMapperRef(
15882         SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
15883         Type.getCanonicalType(), UnresolvedMapper);
15884     if (ER.isInvalid())
15885       continue;
15886     MVLI.UDMapperList.push_back(ER.get());
15887 
15888     // Save the current expression.
15889     MVLI.ProcessedVarList.push_back(RE);
15890 
15891     // Store the components in the stack so that they can be used to check
15892     // against other clauses later on.
15893     DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
15894                                           /*WhereFoundClauseKind=*/OMPC_map);
15895 
15896     // Save the components and declaration to create the clause. For purposes of
15897     // the clause creation, any component list that has has base 'this' uses
15898     // null as base declaration.
15899     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
15900     MVLI.VarComponents.back().append(CurComponents.begin(),
15901                                      CurComponents.end());
15902     MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
15903                                                            : CurDeclaration);
15904   }
15905 }
15906 
15907 OMPClause *Sema::ActOnOpenMPMapClause(
15908     ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
15909     ArrayRef<SourceLocation> MapTypeModifiersLoc,
15910     CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId,
15911     OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc,
15912     SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
15913     const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) {
15914   OpenMPMapModifierKind Modifiers[] = {OMPC_MAP_MODIFIER_unknown,
15915                                        OMPC_MAP_MODIFIER_unknown,
15916                                        OMPC_MAP_MODIFIER_unknown};
15917   SourceLocation ModifiersLoc[OMPMapClause::NumberOfModifiers];
15918 
15919   // Process map-type-modifiers, flag errors for duplicate modifiers.
15920   unsigned Count = 0;
15921   for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) {
15922     if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown &&
15923         llvm::find(Modifiers, MapTypeModifiers[I]) != std::end(Modifiers)) {
15924       Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier);
15925       continue;
15926     }
15927     assert(Count < OMPMapClause::NumberOfModifiers &&
15928            "Modifiers exceed the allowed number of map type modifiers");
15929     Modifiers[Count] = MapTypeModifiers[I];
15930     ModifiersLoc[Count] = MapTypeModifiersLoc[I];
15931     ++Count;
15932   }
15933 
15934   MappableVarListInfo MVLI(VarList);
15935   checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, Locs.StartLoc,
15936                               MapperIdScopeSpec, MapperId, UnresolvedMappers,
15937                               MapType, IsMapTypeImplicit);
15938 
15939   // We need to produce a map clause even if we don't have variables so that
15940   // other diagnostics related with non-existing map clauses are accurate.
15941   return OMPMapClause::Create(Context, Locs, MVLI.ProcessedVarList,
15942                               MVLI.VarBaseDeclarations, MVLI.VarComponents,
15943                               MVLI.UDMapperList, Modifiers, ModifiersLoc,
15944                               MapperIdScopeSpec.getWithLocInContext(Context),
15945                               MapperId, MapType, IsMapTypeImplicit, MapLoc);
15946 }
15947 
15948 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
15949                                                TypeResult ParsedType) {
15950   assert(ParsedType.isUsable());
15951 
15952   QualType ReductionType = GetTypeFromParser(ParsedType.get());
15953   if (ReductionType.isNull())
15954     return QualType();
15955 
15956   // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
15957   // A type name in a declare reduction directive cannot be a function type, an
15958   // array type, a reference type, or a type qualified with const, volatile or
15959   // restrict.
15960   if (ReductionType.hasQualifiers()) {
15961     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
15962     return QualType();
15963   }
15964 
15965   if (ReductionType->isFunctionType()) {
15966     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
15967     return QualType();
15968   }
15969   if (ReductionType->isReferenceType()) {
15970     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
15971     return QualType();
15972   }
15973   if (ReductionType->isArrayType()) {
15974     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
15975     return QualType();
15976   }
15977   return ReductionType;
15978 }
15979 
15980 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
15981     Scope *S, DeclContext *DC, DeclarationName Name,
15982     ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
15983     AccessSpecifier AS, Decl *PrevDeclInScope) {
15984   SmallVector<Decl *, 8> Decls;
15985   Decls.reserve(ReductionTypes.size());
15986 
15987   LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
15988                       forRedeclarationInCurContext());
15989   // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
15990   // A reduction-identifier may not be re-declared in the current scope for the
15991   // same type or for a type that is compatible according to the base language
15992   // rules.
15993   llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
15994   OMPDeclareReductionDecl *PrevDRD = nullptr;
15995   bool InCompoundScope = true;
15996   if (S != nullptr) {
15997     // Find previous declaration with the same name not referenced in other
15998     // declarations.
15999     FunctionScopeInfo *ParentFn = getEnclosingFunction();
16000     InCompoundScope =
16001         (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
16002     LookupName(Lookup, S);
16003     FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
16004                          /*AllowInlineNamespace=*/false);
16005     llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
16006     LookupResult::Filter Filter = Lookup.makeFilter();
16007     while (Filter.hasNext()) {
16008       auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
16009       if (InCompoundScope) {
16010         auto I = UsedAsPrevious.find(PrevDecl);
16011         if (I == UsedAsPrevious.end())
16012           UsedAsPrevious[PrevDecl] = false;
16013         if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
16014           UsedAsPrevious[D] = true;
16015       }
16016       PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
16017           PrevDecl->getLocation();
16018     }
16019     Filter.done();
16020     if (InCompoundScope) {
16021       for (const auto &PrevData : UsedAsPrevious) {
16022         if (!PrevData.second) {
16023           PrevDRD = PrevData.first;
16024           break;
16025         }
16026       }
16027     }
16028   } else if (PrevDeclInScope != nullptr) {
16029     auto *PrevDRDInScope = PrevDRD =
16030         cast<OMPDeclareReductionDecl>(PrevDeclInScope);
16031     do {
16032       PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
16033           PrevDRDInScope->getLocation();
16034       PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
16035     } while (PrevDRDInScope != nullptr);
16036   }
16037   for (const auto &TyData : ReductionTypes) {
16038     const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
16039     bool Invalid = false;
16040     if (I != PreviousRedeclTypes.end()) {
16041       Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
16042           << TyData.first;
16043       Diag(I->second, diag::note_previous_definition);
16044       Invalid = true;
16045     }
16046     PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
16047     auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
16048                                                 Name, TyData.first, PrevDRD);
16049     DC->addDecl(DRD);
16050     DRD->setAccess(AS);
16051     Decls.push_back(DRD);
16052     if (Invalid)
16053       DRD->setInvalidDecl();
16054     else
16055       PrevDRD = DRD;
16056   }
16057 
16058   return DeclGroupPtrTy::make(
16059       DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
16060 }
16061 
16062 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
16063   auto *DRD = cast<OMPDeclareReductionDecl>(D);
16064 
16065   // Enter new function scope.
16066   PushFunctionScope();
16067   setFunctionHasBranchProtectedScope();
16068   getCurFunction()->setHasOMPDeclareReductionCombiner();
16069 
16070   if (S != nullptr)
16071     PushDeclContext(S, DRD);
16072   else
16073     CurContext = DRD;
16074 
16075   PushExpressionEvaluationContext(
16076       ExpressionEvaluationContext::PotentiallyEvaluated);
16077 
16078   QualType ReductionType = DRD->getType();
16079   // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
16080   // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
16081   // uses semantics of argument handles by value, but it should be passed by
16082   // reference. C lang does not support references, so pass all parameters as
16083   // pointers.
16084   // Create 'T omp_in;' variable.
16085   VarDecl *OmpInParm =
16086       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
16087   // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
16088   // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
16089   // uses semantics of argument handles by value, but it should be passed by
16090   // reference. C lang does not support references, so pass all parameters as
16091   // pointers.
16092   // Create 'T omp_out;' variable.
16093   VarDecl *OmpOutParm =
16094       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
16095   if (S != nullptr) {
16096     PushOnScopeChains(OmpInParm, S);
16097     PushOnScopeChains(OmpOutParm, S);
16098   } else {
16099     DRD->addDecl(OmpInParm);
16100     DRD->addDecl(OmpOutParm);
16101   }
16102   Expr *InE =
16103       ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation());
16104   Expr *OutE =
16105       ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation());
16106   DRD->setCombinerData(InE, OutE);
16107 }
16108 
16109 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
16110   auto *DRD = cast<OMPDeclareReductionDecl>(D);
16111   DiscardCleanupsInEvaluationContext();
16112   PopExpressionEvaluationContext();
16113 
16114   PopDeclContext();
16115   PopFunctionScopeInfo();
16116 
16117   if (Combiner != nullptr)
16118     DRD->setCombiner(Combiner);
16119   else
16120     DRD->setInvalidDecl();
16121 }
16122 
16123 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
16124   auto *DRD = cast<OMPDeclareReductionDecl>(D);
16125 
16126   // Enter new function scope.
16127   PushFunctionScope();
16128   setFunctionHasBranchProtectedScope();
16129 
16130   if (S != nullptr)
16131     PushDeclContext(S, DRD);
16132   else
16133     CurContext = DRD;
16134 
16135   PushExpressionEvaluationContext(
16136       ExpressionEvaluationContext::PotentiallyEvaluated);
16137 
16138   QualType ReductionType = DRD->getType();
16139   // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
16140   // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
16141   // uses semantics of argument handles by value, but it should be passed by
16142   // reference. C lang does not support references, so pass all parameters as
16143   // pointers.
16144   // Create 'T omp_priv;' variable.
16145   VarDecl *OmpPrivParm =
16146       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
16147   // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
16148   // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
16149   // uses semantics of argument handles by value, but it should be passed by
16150   // reference. C lang does not support references, so pass all parameters as
16151   // pointers.
16152   // Create 'T omp_orig;' variable.
16153   VarDecl *OmpOrigParm =
16154       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
16155   if (S != nullptr) {
16156     PushOnScopeChains(OmpPrivParm, S);
16157     PushOnScopeChains(OmpOrigParm, S);
16158   } else {
16159     DRD->addDecl(OmpPrivParm);
16160     DRD->addDecl(OmpOrigParm);
16161   }
16162   Expr *OrigE =
16163       ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation());
16164   Expr *PrivE =
16165       ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation());
16166   DRD->setInitializerData(OrigE, PrivE);
16167   return OmpPrivParm;
16168 }
16169 
16170 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
16171                                                      VarDecl *OmpPrivParm) {
16172   auto *DRD = cast<OMPDeclareReductionDecl>(D);
16173   DiscardCleanupsInEvaluationContext();
16174   PopExpressionEvaluationContext();
16175 
16176   PopDeclContext();
16177   PopFunctionScopeInfo();
16178 
16179   if (Initializer != nullptr) {
16180     DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
16181   } else if (OmpPrivParm->hasInit()) {
16182     DRD->setInitializer(OmpPrivParm->getInit(),
16183                         OmpPrivParm->isDirectInit()
16184                             ? OMPDeclareReductionDecl::DirectInit
16185                             : OMPDeclareReductionDecl::CopyInit);
16186   } else {
16187     DRD->setInvalidDecl();
16188   }
16189 }
16190 
16191 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
16192     Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
16193   for (Decl *D : DeclReductions.get()) {
16194     if (IsValid) {
16195       if (S)
16196         PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S,
16197                           /*AddToContext=*/false);
16198     } else {
16199       D->setInvalidDecl();
16200     }
16201   }
16202   return DeclReductions;
16203 }
16204 
16205 TypeResult Sema::ActOnOpenMPDeclareMapperVarDecl(Scope *S, Declarator &D) {
16206   TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
16207   QualType T = TInfo->getType();
16208   if (D.isInvalidType())
16209     return true;
16210 
16211   if (getLangOpts().CPlusPlus) {
16212     // Check that there are no default arguments (C++ only).
16213     CheckExtraCXXDefaultArguments(D);
16214   }
16215 
16216   return CreateParsedType(T, TInfo);
16217 }
16218 
16219 QualType Sema::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc,
16220                                             TypeResult ParsedType) {
16221   assert(ParsedType.isUsable() && "Expect usable parsed mapper type");
16222 
16223   QualType MapperType = GetTypeFromParser(ParsedType.get());
16224   assert(!MapperType.isNull() && "Expect valid mapper type");
16225 
16226   // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
16227   //  The type must be of struct, union or class type in C and C++
16228   if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) {
16229     Diag(TyLoc, diag::err_omp_mapper_wrong_type);
16230     return QualType();
16231   }
16232   return MapperType;
16233 }
16234 
16235 OMPDeclareMapperDecl *Sema::ActOnOpenMPDeclareMapperDirectiveStart(
16236     Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType,
16237     SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS,
16238     Decl *PrevDeclInScope) {
16239   LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPMapperName,
16240                       forRedeclarationInCurContext());
16241   // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
16242   //  A mapper-identifier may not be redeclared in the current scope for the
16243   //  same type or for a type that is compatible according to the base language
16244   //  rules.
16245   llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
16246   OMPDeclareMapperDecl *PrevDMD = nullptr;
16247   bool InCompoundScope = true;
16248   if (S != nullptr) {
16249     // Find previous declaration with the same name not referenced in other
16250     // declarations.
16251     FunctionScopeInfo *ParentFn = getEnclosingFunction();
16252     InCompoundScope =
16253         (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
16254     LookupName(Lookup, S);
16255     FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
16256                          /*AllowInlineNamespace=*/false);
16257     llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious;
16258     LookupResult::Filter Filter = Lookup.makeFilter();
16259     while (Filter.hasNext()) {
16260       auto *PrevDecl = cast<OMPDeclareMapperDecl>(Filter.next());
16261       if (InCompoundScope) {
16262         auto I = UsedAsPrevious.find(PrevDecl);
16263         if (I == UsedAsPrevious.end())
16264           UsedAsPrevious[PrevDecl] = false;
16265         if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope())
16266           UsedAsPrevious[D] = true;
16267       }
16268       PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
16269           PrevDecl->getLocation();
16270     }
16271     Filter.done();
16272     if (InCompoundScope) {
16273       for (const auto &PrevData : UsedAsPrevious) {
16274         if (!PrevData.second) {
16275           PrevDMD = PrevData.first;
16276           break;
16277         }
16278       }
16279     }
16280   } else if (PrevDeclInScope) {
16281     auto *PrevDMDInScope = PrevDMD =
16282         cast<OMPDeclareMapperDecl>(PrevDeclInScope);
16283     do {
16284       PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] =
16285           PrevDMDInScope->getLocation();
16286       PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope();
16287     } while (PrevDMDInScope != nullptr);
16288   }
16289   const auto I = PreviousRedeclTypes.find(MapperType.getCanonicalType());
16290   bool Invalid = false;
16291   if (I != PreviousRedeclTypes.end()) {
16292     Diag(StartLoc, diag::err_omp_declare_mapper_redefinition)
16293         << MapperType << Name;
16294     Diag(I->second, diag::note_previous_definition);
16295     Invalid = true;
16296   }
16297   auto *DMD = OMPDeclareMapperDecl::Create(Context, DC, StartLoc, Name,
16298                                            MapperType, VN, PrevDMD);
16299   DC->addDecl(DMD);
16300   DMD->setAccess(AS);
16301   if (Invalid)
16302     DMD->setInvalidDecl();
16303 
16304   // Enter new function scope.
16305   PushFunctionScope();
16306   setFunctionHasBranchProtectedScope();
16307 
16308   CurContext = DMD;
16309 
16310   return DMD;
16311 }
16312 
16313 void Sema::ActOnOpenMPDeclareMapperDirectiveVarDecl(OMPDeclareMapperDecl *DMD,
16314                                                     Scope *S,
16315                                                     QualType MapperType,
16316                                                     SourceLocation StartLoc,
16317                                                     DeclarationName VN) {
16318   VarDecl *VD = buildVarDecl(*this, StartLoc, MapperType, VN.getAsString());
16319   if (S)
16320     PushOnScopeChains(VD, S);
16321   else
16322     DMD->addDecl(VD);
16323   Expr *MapperVarRefExpr = buildDeclRefExpr(*this, VD, MapperType, StartLoc);
16324   DMD->setMapperVarRef(MapperVarRefExpr);
16325 }
16326 
16327 Sema::DeclGroupPtrTy
16328 Sema::ActOnOpenMPDeclareMapperDirectiveEnd(OMPDeclareMapperDecl *D, Scope *S,
16329                                            ArrayRef<OMPClause *> ClauseList) {
16330   PopDeclContext();
16331   PopFunctionScopeInfo();
16332 
16333   if (D) {
16334     if (S)
16335       PushOnScopeChains(D, S, /*AddToContext=*/false);
16336     D->CreateClauses(Context, ClauseList);
16337   }
16338 
16339   return DeclGroupPtrTy::make(DeclGroupRef(D));
16340 }
16341 
16342 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
16343                                            SourceLocation StartLoc,
16344                                            SourceLocation LParenLoc,
16345                                            SourceLocation EndLoc) {
16346   Expr *ValExpr = NumTeams;
16347   Stmt *HelperValStmt = nullptr;
16348 
16349   // OpenMP [teams Constrcut, Restrictions]
16350   // The num_teams expression must evaluate to a positive integer value.
16351   if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
16352                                  /*StrictlyPositive=*/true))
16353     return nullptr;
16354 
16355   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
16356   OpenMPDirectiveKind CaptureRegion =
16357       getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams, LangOpts.OpenMP);
16358   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
16359     ValExpr = MakeFullExpr(ValExpr).get();
16360     llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
16361     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
16362     HelperValStmt = buildPreInits(Context, Captures);
16363   }
16364 
16365   return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
16366                                          StartLoc, LParenLoc, EndLoc);
16367 }
16368 
16369 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
16370                                               SourceLocation StartLoc,
16371                                               SourceLocation LParenLoc,
16372                                               SourceLocation EndLoc) {
16373   Expr *ValExpr = ThreadLimit;
16374   Stmt *HelperValStmt = nullptr;
16375 
16376   // OpenMP [teams Constrcut, Restrictions]
16377   // The thread_limit expression must evaluate to a positive integer value.
16378   if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
16379                                  /*StrictlyPositive=*/true))
16380     return nullptr;
16381 
16382   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
16383   OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
16384       DKind, OMPC_thread_limit, LangOpts.OpenMP);
16385   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
16386     ValExpr = MakeFullExpr(ValExpr).get();
16387     llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
16388     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
16389     HelperValStmt = buildPreInits(Context, Captures);
16390   }
16391 
16392   return new (Context) OMPThreadLimitClause(
16393       ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
16394 }
16395 
16396 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
16397                                            SourceLocation StartLoc,
16398                                            SourceLocation LParenLoc,
16399                                            SourceLocation EndLoc) {
16400   Expr *ValExpr = Priority;
16401   Stmt *HelperValStmt = nullptr;
16402   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
16403 
16404   // OpenMP [2.9.1, task Constrcut]
16405   // The priority-value is a non-negative numerical scalar expression.
16406   if (!isNonNegativeIntegerValue(
16407           ValExpr, *this, OMPC_priority,
16408           /*StrictlyPositive=*/false, /*BuildCapture=*/true,
16409           DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt))
16410     return nullptr;
16411 
16412   return new (Context) OMPPriorityClause(ValExpr, HelperValStmt, CaptureRegion,
16413                                          StartLoc, LParenLoc, EndLoc);
16414 }
16415 
16416 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
16417                                             SourceLocation StartLoc,
16418                                             SourceLocation LParenLoc,
16419                                             SourceLocation EndLoc) {
16420   Expr *ValExpr = Grainsize;
16421   Stmt *HelperValStmt = nullptr;
16422   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
16423 
16424   // OpenMP [2.9.2, taskloop Constrcut]
16425   // The parameter of the grainsize clause must be a positive integer
16426   // expression.
16427   if (!isNonNegativeIntegerValue(
16428           ValExpr, *this, OMPC_grainsize,
16429           /*StrictlyPositive=*/true, /*BuildCapture=*/true,
16430           DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt))
16431     return nullptr;
16432 
16433   return new (Context) OMPGrainsizeClause(ValExpr, HelperValStmt, CaptureRegion,
16434                                           StartLoc, LParenLoc, EndLoc);
16435 }
16436 
16437 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
16438                                            SourceLocation StartLoc,
16439                                            SourceLocation LParenLoc,
16440                                            SourceLocation EndLoc) {
16441   Expr *ValExpr = NumTasks;
16442   Stmt *HelperValStmt = nullptr;
16443   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
16444 
16445   // OpenMP [2.9.2, taskloop Constrcut]
16446   // The parameter of the num_tasks clause must be a positive integer
16447   // expression.
16448   if (!isNonNegativeIntegerValue(
16449           ValExpr, *this, OMPC_num_tasks,
16450           /*StrictlyPositive=*/true, /*BuildCapture=*/true,
16451           DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt))
16452     return nullptr;
16453 
16454   return new (Context) OMPNumTasksClause(ValExpr, HelperValStmt, CaptureRegion,
16455                                          StartLoc, LParenLoc, EndLoc);
16456 }
16457 
16458 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
16459                                        SourceLocation LParenLoc,
16460                                        SourceLocation EndLoc) {
16461   // OpenMP [2.13.2, critical construct, Description]
16462   // ... where hint-expression is an integer constant expression that evaluates
16463   // to a valid lock hint.
16464   ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
16465   if (HintExpr.isInvalid())
16466     return nullptr;
16467   return new (Context)
16468       OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
16469 }
16470 
16471 OMPClause *Sema::ActOnOpenMPDistScheduleClause(
16472     OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
16473     SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
16474     SourceLocation EndLoc) {
16475   if (Kind == OMPC_DIST_SCHEDULE_unknown) {
16476     std::string Values;
16477     Values += "'";
16478     Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
16479     Values += "'";
16480     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
16481         << Values << getOpenMPClauseName(OMPC_dist_schedule);
16482     return nullptr;
16483   }
16484   Expr *ValExpr = ChunkSize;
16485   Stmt *HelperValStmt = nullptr;
16486   if (ChunkSize) {
16487     if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
16488         !ChunkSize->isInstantiationDependent() &&
16489         !ChunkSize->containsUnexpandedParameterPack()) {
16490       SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
16491       ExprResult Val =
16492           PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
16493       if (Val.isInvalid())
16494         return nullptr;
16495 
16496       ValExpr = Val.get();
16497 
16498       // OpenMP [2.7.1, Restrictions]
16499       //  chunk_size must be a loop invariant integer expression with a positive
16500       //  value.
16501       llvm::APSInt Result;
16502       if (ValExpr->isIntegerConstantExpr(Result, Context)) {
16503         if (Result.isSigned() && !Result.isStrictlyPositive()) {
16504           Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
16505               << "dist_schedule" << ChunkSize->getSourceRange();
16506           return nullptr;
16507         }
16508       } else if (getOpenMPCaptureRegionForClause(
16509                      DSAStack->getCurrentDirective(), OMPC_dist_schedule,
16510                      LangOpts.OpenMP) != OMPD_unknown &&
16511                  !CurContext->isDependentContext()) {
16512         ValExpr = MakeFullExpr(ValExpr).get();
16513         llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
16514         ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
16515         HelperValStmt = buildPreInits(Context, Captures);
16516       }
16517     }
16518   }
16519 
16520   return new (Context)
16521       OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
16522                             Kind, ValExpr, HelperValStmt);
16523 }
16524 
16525 OMPClause *Sema::ActOnOpenMPDefaultmapClause(
16526     OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
16527     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
16528     SourceLocation KindLoc, SourceLocation EndLoc) {
16529   if (getLangOpts().OpenMP < 50) {
16530     if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
16531         Kind != OMPC_DEFAULTMAP_scalar) {
16532       std::string Value;
16533       SourceLocation Loc;
16534       Value += "'";
16535       if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
16536         Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
16537                                                OMPC_DEFAULTMAP_MODIFIER_tofrom);
16538         Loc = MLoc;
16539       } else {
16540         Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
16541                                                OMPC_DEFAULTMAP_scalar);
16542         Loc = KindLoc;
16543       }
16544       Value += "'";
16545       Diag(Loc, diag::err_omp_unexpected_clause_value)
16546           << Value << getOpenMPClauseName(OMPC_defaultmap);
16547       return nullptr;
16548     }
16549   } else {
16550     bool isDefaultmapModifier = (M != OMPC_DEFAULTMAP_MODIFIER_unknown);
16551     bool isDefaultmapKind = (Kind != OMPC_DEFAULTMAP_unknown);
16552     if (!isDefaultmapKind || !isDefaultmapModifier) {
16553       std::string ModifierValue = "'alloc', 'from', 'to', 'tofrom', "
16554                                   "'firstprivate', 'none', 'default'";
16555       std::string KindValue = "'scalar', 'aggregate', 'pointer'";
16556       if (!isDefaultmapKind && isDefaultmapModifier) {
16557         Diag(KindLoc, diag::err_omp_unexpected_clause_value)
16558             << KindValue << getOpenMPClauseName(OMPC_defaultmap);
16559       } else if (isDefaultmapKind && !isDefaultmapModifier) {
16560         Diag(MLoc, diag::err_omp_unexpected_clause_value)
16561             << ModifierValue << getOpenMPClauseName(OMPC_defaultmap);
16562       } else {
16563         Diag(MLoc, diag::err_omp_unexpected_clause_value)
16564             << ModifierValue << getOpenMPClauseName(OMPC_defaultmap);
16565         Diag(KindLoc, diag::err_omp_unexpected_clause_value)
16566             << KindValue << getOpenMPClauseName(OMPC_defaultmap);
16567       }
16568       return nullptr;
16569     }
16570 
16571     // OpenMP [5.0, 2.12.5, Restrictions, p. 174]
16572     //  At most one defaultmap clause for each category can appear on the
16573     //  directive.
16574     if (DSAStack->checkDefaultmapCategory(Kind)) {
16575       Diag(StartLoc, diag::err_omp_one_defaultmap_each_category);
16576       return nullptr;
16577     }
16578   }
16579   DSAStack->setDefaultDMAAttr(M, Kind, StartLoc);
16580 
16581   return new (Context)
16582       OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
16583 }
16584 
16585 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
16586   DeclContext *CurLexicalContext = getCurLexicalContext();
16587   if (!CurLexicalContext->isFileContext() &&
16588       !CurLexicalContext->isExternCContext() &&
16589       !CurLexicalContext->isExternCXXContext() &&
16590       !isa<CXXRecordDecl>(CurLexicalContext) &&
16591       !isa<ClassTemplateDecl>(CurLexicalContext) &&
16592       !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
16593       !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
16594     Diag(Loc, diag::err_omp_region_not_file_context);
16595     return false;
16596   }
16597   ++DeclareTargetNestingLevel;
16598   return true;
16599 }
16600 
16601 void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
16602   assert(DeclareTargetNestingLevel > 0 &&
16603          "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
16604   --DeclareTargetNestingLevel;
16605 }
16606 
16607 NamedDecl *
16608 Sema::lookupOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec,
16609                                     const DeclarationNameInfo &Id,
16610                                     NamedDeclSetType &SameDirectiveDecls) {
16611   LookupResult Lookup(*this, Id, LookupOrdinaryName);
16612   LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
16613 
16614   if (Lookup.isAmbiguous())
16615     return nullptr;
16616   Lookup.suppressDiagnostics();
16617 
16618   if (!Lookup.isSingleResult()) {
16619     VarOrFuncDeclFilterCCC CCC(*this);
16620     if (TypoCorrection Corrected =
16621             CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC,
16622                         CTK_ErrorRecovery)) {
16623       diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
16624                                   << Id.getName());
16625       checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
16626       return nullptr;
16627     }
16628 
16629     Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
16630     return nullptr;
16631   }
16632 
16633   NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
16634   if (!isa<VarDecl>(ND) && !isa<FunctionDecl>(ND) &&
16635       !isa<FunctionTemplateDecl>(ND)) {
16636     Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
16637     return nullptr;
16638   }
16639   if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
16640     Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
16641   return ND;
16642 }
16643 
16644 void Sema::ActOnOpenMPDeclareTargetName(
16645     NamedDecl *ND, SourceLocation Loc, OMPDeclareTargetDeclAttr::MapTypeTy MT,
16646     OMPDeclareTargetDeclAttr::DevTypeTy DT) {
16647   assert((isa<VarDecl>(ND) || isa<FunctionDecl>(ND) ||
16648           isa<FunctionTemplateDecl>(ND)) &&
16649          "Expected variable, function or function template.");
16650 
16651   // Diagnose marking after use as it may lead to incorrect diagnosis and
16652   // codegen.
16653   if (LangOpts.OpenMP >= 50 &&
16654       (ND->isUsed(/*CheckUsedAttr=*/false) || ND->isReferenced()))
16655     Diag(Loc, diag::warn_omp_declare_target_after_first_use);
16656 
16657   Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
16658       OMPDeclareTargetDeclAttr::getDeviceType(cast<ValueDecl>(ND));
16659   if (DevTy.hasValue() && *DevTy != DT) {
16660     Diag(Loc, diag::err_omp_device_type_mismatch)
16661         << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(DT)
16662         << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(*DevTy);
16663     return;
16664   }
16665   Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
16666       OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(cast<ValueDecl>(ND));
16667   if (!Res) {
16668     auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT, DT,
16669                                                        SourceRange(Loc, Loc));
16670     ND->addAttr(A);
16671     if (ASTMutationListener *ML = Context.getASTMutationListener())
16672       ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
16673     checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Loc);
16674   } else if (*Res != MT) {
16675     Diag(Loc, diag::err_omp_declare_target_to_and_link) << ND;
16676   }
16677 }
16678 
16679 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
16680                                      Sema &SemaRef, Decl *D) {
16681   if (!D || !isa<VarDecl>(D))
16682     return;
16683   auto *VD = cast<VarDecl>(D);
16684   Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy =
16685       OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
16686   if (SemaRef.LangOpts.OpenMP >= 50 &&
16687       (SemaRef.getCurLambda(/*IgnoreNonLambdaCapturingScope=*/true) ||
16688        SemaRef.getCurBlock() || SemaRef.getCurCapturedRegion()) &&
16689       VD->hasGlobalStorage()) {
16690     llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy =
16691         OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
16692     if (!MapTy || *MapTy != OMPDeclareTargetDeclAttr::MT_To) {
16693       // OpenMP 5.0, 2.12.7 declare target Directive, Restrictions
16694       // If a lambda declaration and definition appears between a
16695       // declare target directive and the matching end declare target
16696       // directive, all variables that are captured by the lambda
16697       // expression must also appear in a to clause.
16698       SemaRef.Diag(VD->getLocation(),
16699                    diag::err_omp_lambda_capture_in_declare_target_not_to);
16700       SemaRef.Diag(SL, diag::note_var_explicitly_captured_here)
16701           << VD << 0 << SR;
16702       return;
16703     }
16704   }
16705   if (MapTy.hasValue())
16706     return;
16707   SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context);
16708   SemaRef.Diag(SL, diag::note_used_here) << SR;
16709 }
16710 
16711 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
16712                                    Sema &SemaRef, DSAStackTy *Stack,
16713                                    ValueDecl *VD) {
16714   return OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) ||
16715          checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(),
16716                            /*FullCheck=*/false);
16717 }
16718 
16719 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
16720                                             SourceLocation IdLoc) {
16721   if (!D || D->isInvalidDecl())
16722     return;
16723   SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
16724   SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
16725   if (auto *VD = dyn_cast<VarDecl>(D)) {
16726     // Only global variables can be marked as declare target.
16727     if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
16728         !VD->isStaticDataMember())
16729       return;
16730     // 2.10.6: threadprivate variable cannot appear in a declare target
16731     // directive.
16732     if (DSAStack->isThreadPrivate(VD)) {
16733       Diag(SL, diag::err_omp_threadprivate_in_target);
16734       reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
16735       return;
16736     }
16737   }
16738   if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
16739     D = FTD->getTemplatedDecl();
16740   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
16741     llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
16742         OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD);
16743     if (IdLoc.isValid() && Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) {
16744       Diag(IdLoc, diag::err_omp_function_in_link_clause);
16745       Diag(FD->getLocation(), diag::note_defined_here) << FD;
16746       return;
16747     }
16748     // Mark the function as must be emitted for the device.
16749     Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
16750         OMPDeclareTargetDeclAttr::getDeviceType(FD);
16751     if (LangOpts.OpenMPIsDevice && Res.hasValue() && IdLoc.isValid() &&
16752         *DevTy != OMPDeclareTargetDeclAttr::DT_Host)
16753       checkOpenMPDeviceFunction(IdLoc, FD, /*CheckForDelayedContext=*/false);
16754     if (!LangOpts.OpenMPIsDevice && Res.hasValue() && IdLoc.isValid() &&
16755         *DevTy != OMPDeclareTargetDeclAttr::DT_NoHost)
16756       checkOpenMPHostFunction(IdLoc, FD, /*CheckCaller=*/false);
16757   }
16758   if (auto *VD = dyn_cast<ValueDecl>(D)) {
16759     // Problem if any with var declared with incomplete type will be reported
16760     // as normal, so no need to check it here.
16761     if ((E || !VD->getType()->isIncompleteType()) &&
16762         !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD))
16763       return;
16764     if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
16765       // Checking declaration inside declare target region.
16766       if (isa<VarDecl>(D) || isa<FunctionDecl>(D) ||
16767           isa<FunctionTemplateDecl>(D)) {
16768         auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
16769             Context, OMPDeclareTargetDeclAttr::MT_To,
16770             OMPDeclareTargetDeclAttr::DT_Any, SourceRange(IdLoc, IdLoc));
16771         D->addAttr(A);
16772         if (ASTMutationListener *ML = Context.getASTMutationListener())
16773           ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
16774       }
16775       return;
16776     }
16777   }
16778   if (!E)
16779     return;
16780   checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
16781 }
16782 
16783 OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
16784                                      CXXScopeSpec &MapperIdScopeSpec,
16785                                      DeclarationNameInfo &MapperId,
16786                                      const OMPVarListLocTy &Locs,
16787                                      ArrayRef<Expr *> UnresolvedMappers) {
16788   MappableVarListInfo MVLI(VarList);
16789   checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, Locs.StartLoc,
16790                               MapperIdScopeSpec, MapperId, UnresolvedMappers);
16791   if (MVLI.ProcessedVarList.empty())
16792     return nullptr;
16793 
16794   return OMPToClause::Create(
16795       Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
16796       MVLI.VarComponents, MVLI.UDMapperList,
16797       MapperIdScopeSpec.getWithLocInContext(Context), MapperId);
16798 }
16799 
16800 OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
16801                                        CXXScopeSpec &MapperIdScopeSpec,
16802                                        DeclarationNameInfo &MapperId,
16803                                        const OMPVarListLocTy &Locs,
16804                                        ArrayRef<Expr *> UnresolvedMappers) {
16805   MappableVarListInfo MVLI(VarList);
16806   checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, Locs.StartLoc,
16807                               MapperIdScopeSpec, MapperId, UnresolvedMappers);
16808   if (MVLI.ProcessedVarList.empty())
16809     return nullptr;
16810 
16811   return OMPFromClause::Create(
16812       Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
16813       MVLI.VarComponents, MVLI.UDMapperList,
16814       MapperIdScopeSpec.getWithLocInContext(Context), MapperId);
16815 }
16816 
16817 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
16818                                                const OMPVarListLocTy &Locs) {
16819   MappableVarListInfo MVLI(VarList);
16820   SmallVector<Expr *, 8> PrivateCopies;
16821   SmallVector<Expr *, 8> Inits;
16822 
16823   for (Expr *RefExpr : VarList) {
16824     assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
16825     SourceLocation ELoc;
16826     SourceRange ERange;
16827     Expr *SimpleRefExpr = RefExpr;
16828     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
16829     if (Res.second) {
16830       // It will be analyzed later.
16831       MVLI.ProcessedVarList.push_back(RefExpr);
16832       PrivateCopies.push_back(nullptr);
16833       Inits.push_back(nullptr);
16834     }
16835     ValueDecl *D = Res.first;
16836     if (!D)
16837       continue;
16838 
16839     QualType Type = D->getType();
16840     Type = Type.getNonReferenceType().getUnqualifiedType();
16841 
16842     auto *VD = dyn_cast<VarDecl>(D);
16843 
16844     // Item should be a pointer or reference to pointer.
16845     if (!Type->isPointerType()) {
16846       Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
16847           << 0 << RefExpr->getSourceRange();
16848       continue;
16849     }
16850 
16851     // Build the private variable and the expression that refers to it.
16852     auto VDPrivate =
16853         buildVarDecl(*this, ELoc, Type, D->getName(),
16854                      D->hasAttrs() ? &D->getAttrs() : nullptr,
16855                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
16856     if (VDPrivate->isInvalidDecl())
16857       continue;
16858 
16859     CurContext->addDecl(VDPrivate);
16860     DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
16861         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
16862 
16863     // Add temporary variable to initialize the private copy of the pointer.
16864     VarDecl *VDInit =
16865         buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
16866     DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
16867         *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc());
16868     AddInitializerToDecl(VDPrivate,
16869                          DefaultLvalueConversion(VDInitRefExpr).get(),
16870                          /*DirectInit=*/false);
16871 
16872     // If required, build a capture to implement the privatization initialized
16873     // with the current list item value.
16874     DeclRefExpr *Ref = nullptr;
16875     if (!VD)
16876       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
16877     MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
16878     PrivateCopies.push_back(VDPrivateRefExpr);
16879     Inits.push_back(VDInitRefExpr);
16880 
16881     // We need to add a data sharing attribute for this variable to make sure it
16882     // is correctly captured. A variable that shows up in a use_device_ptr has
16883     // similar properties of a first private variable.
16884     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
16885 
16886     // Create a mappable component for the list item. List items in this clause
16887     // only need a component.
16888     MVLI.VarBaseDeclarations.push_back(D);
16889     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
16890     MVLI.VarComponents.back().push_back(
16891         OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
16892   }
16893 
16894   if (MVLI.ProcessedVarList.empty())
16895     return nullptr;
16896 
16897   return OMPUseDevicePtrClause::Create(
16898       Context, Locs, MVLI.ProcessedVarList, PrivateCopies, Inits,
16899       MVLI.VarBaseDeclarations, MVLI.VarComponents);
16900 }
16901 
16902 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
16903                                               const OMPVarListLocTy &Locs) {
16904   MappableVarListInfo MVLI(VarList);
16905   for (Expr *RefExpr : VarList) {
16906     assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
16907     SourceLocation ELoc;
16908     SourceRange ERange;
16909     Expr *SimpleRefExpr = RefExpr;
16910     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
16911     if (Res.second) {
16912       // It will be analyzed later.
16913       MVLI.ProcessedVarList.push_back(RefExpr);
16914     }
16915     ValueDecl *D = Res.first;
16916     if (!D)
16917       continue;
16918 
16919     QualType Type = D->getType();
16920     // item should be a pointer or array or reference to pointer or array
16921     if (!Type.getNonReferenceType()->isPointerType() &&
16922         !Type.getNonReferenceType()->isArrayType()) {
16923       Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
16924           << 0 << RefExpr->getSourceRange();
16925       continue;
16926     }
16927 
16928     // Check if the declaration in the clause does not show up in any data
16929     // sharing attribute.
16930     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
16931     if (isOpenMPPrivate(DVar.CKind)) {
16932       Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
16933           << getOpenMPClauseName(DVar.CKind)
16934           << getOpenMPClauseName(OMPC_is_device_ptr)
16935           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
16936       reportOriginalDsa(*this, DSAStack, D, DVar);
16937       continue;
16938     }
16939 
16940     const Expr *ConflictExpr;
16941     if (DSAStack->checkMappableExprComponentListsForDecl(
16942             D, /*CurrentRegionOnly=*/true,
16943             [&ConflictExpr](
16944                 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
16945                 OpenMPClauseKind) -> bool {
16946               ConflictExpr = R.front().getAssociatedExpression();
16947               return true;
16948             })) {
16949       Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
16950       Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
16951           << ConflictExpr->getSourceRange();
16952       continue;
16953     }
16954 
16955     // Store the components in the stack so that they can be used to check
16956     // against other clauses later on.
16957     OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
16958     DSAStack->addMappableExpressionComponents(
16959         D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
16960 
16961     // Record the expression we've just processed.
16962     MVLI.ProcessedVarList.push_back(SimpleRefExpr);
16963 
16964     // Create a mappable component for the list item. List items in this clause
16965     // only need a component. We use a null declaration to signal fields in
16966     // 'this'.
16967     assert((isa<DeclRefExpr>(SimpleRefExpr) ||
16968             isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
16969            "Unexpected device pointer expression!");
16970     MVLI.VarBaseDeclarations.push_back(
16971         isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
16972     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
16973     MVLI.VarComponents.back().push_back(MC);
16974   }
16975 
16976   if (MVLI.ProcessedVarList.empty())
16977     return nullptr;
16978 
16979   return OMPIsDevicePtrClause::Create(Context, Locs, MVLI.ProcessedVarList,
16980                                       MVLI.VarBaseDeclarations,
16981                                       MVLI.VarComponents);
16982 }
16983 
16984 OMPClause *Sema::ActOnOpenMPAllocateClause(
16985     Expr *Allocator, ArrayRef<Expr *> VarList, SourceLocation StartLoc,
16986     SourceLocation ColonLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
16987   if (Allocator) {
16988     // OpenMP [2.11.4 allocate Clause, Description]
16989     // allocator is an expression of omp_allocator_handle_t type.
16990     if (!findOMPAllocatorHandleT(*this, Allocator->getExprLoc(), DSAStack))
16991       return nullptr;
16992 
16993     ExprResult AllocatorRes = DefaultLvalueConversion(Allocator);
16994     if (AllocatorRes.isInvalid())
16995       return nullptr;
16996     AllocatorRes = PerformImplicitConversion(AllocatorRes.get(),
16997                                              DSAStack->getOMPAllocatorHandleT(),
16998                                              Sema::AA_Initializing,
16999                                              /*AllowExplicit=*/true);
17000     if (AllocatorRes.isInvalid())
17001       return nullptr;
17002     Allocator = AllocatorRes.get();
17003   } else {
17004     // OpenMP 5.0, 2.11.4 allocate Clause, Restrictions.
17005     // allocate clauses that appear on a target construct or on constructs in a
17006     // target region must specify an allocator expression unless a requires
17007     // directive with the dynamic_allocators clause is present in the same
17008     // compilation unit.
17009     if (LangOpts.OpenMPIsDevice &&
17010         !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
17011       targetDiag(StartLoc, diag::err_expected_allocator_expression);
17012   }
17013   // Analyze and build list of variables.
17014   SmallVector<Expr *, 8> Vars;
17015   for (Expr *RefExpr : VarList) {
17016     assert(RefExpr && "NULL expr in OpenMP private clause.");
17017     SourceLocation ELoc;
17018     SourceRange ERange;
17019     Expr *SimpleRefExpr = RefExpr;
17020     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
17021     if (Res.second) {
17022       // It will be analyzed later.
17023       Vars.push_back(RefExpr);
17024     }
17025     ValueDecl *D = Res.first;
17026     if (!D)
17027       continue;
17028 
17029     auto *VD = dyn_cast<VarDecl>(D);
17030     DeclRefExpr *Ref = nullptr;
17031     if (!VD && !CurContext->isDependentContext())
17032       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
17033     Vars.push_back((VD || CurContext->isDependentContext())
17034                        ? RefExpr->IgnoreParens()
17035                        : Ref);
17036   }
17037 
17038   if (Vars.empty())
17039     return nullptr;
17040 
17041   return OMPAllocateClause::Create(Context, StartLoc, LParenLoc, Allocator,
17042                                    ColonLoc, EndLoc, Vars);
17043 }
17044