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/PointerEmbeddedInt.h"
32 using namespace clang;
33 
34 //===----------------------------------------------------------------------===//
35 // Stack of data-sharing attributes for variables
36 //===----------------------------------------------------------------------===//
37 
38 static const Expr *checkMapClauseExpressionBase(
39     Sema &SemaRef, Expr *E,
40     OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
41     OpenMPClauseKind CKind, bool NoDiagnose);
42 
43 namespace {
44 /// Default data sharing attributes, which can be applied to directive.
45 enum DefaultDataSharingAttributes {
46   DSA_unspecified = 0, /// Data sharing attribute not specified.
47   DSA_none = 1 << 0,   /// Default data sharing attribute 'none'.
48   DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'.
49 };
50 
51 /// Attributes of the defaultmap clause.
52 enum DefaultMapAttributes {
53   DMA_unspecified,   /// Default mapping is not specified.
54   DMA_tofrom_scalar, /// Default mapping is 'tofrom:scalar'.
55 };
56 
57 /// Stack for tracking declarations used in OpenMP directives and
58 /// clauses and their data-sharing attributes.
59 class DSAStackTy {
60 public:
61   struct DSAVarData {
62     OpenMPDirectiveKind DKind = OMPD_unknown;
63     OpenMPClauseKind CKind = OMPC_unknown;
64     const Expr *RefExpr = nullptr;
65     DeclRefExpr *PrivateCopy = nullptr;
66     SourceLocation ImplicitDSALoc;
67     DSAVarData() = default;
68     DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
69                const Expr *RefExpr, DeclRefExpr *PrivateCopy,
70                SourceLocation ImplicitDSALoc)
71         : DKind(DKind), CKind(CKind), RefExpr(RefExpr),
72           PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc) {}
73   };
74   using OperatorOffsetTy =
75       llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>;
76   using DoacrossDependMapTy =
77       llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>;
78 
79 private:
80   struct DSAInfo {
81     OpenMPClauseKind Attributes = OMPC_unknown;
82     /// Pointer to a reference expression and a flag which shows that the
83     /// variable is marked as lastprivate(true) or not (false).
84     llvm::PointerIntPair<const Expr *, 1, bool> RefExpr;
85     DeclRefExpr *PrivateCopy = nullptr;
86   };
87   using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>;
88   using AlignedMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>;
89   using LCDeclInfo = std::pair<unsigned, VarDecl *>;
90   using LoopControlVariablesMapTy =
91       llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>;
92   /// Struct that associates a component with the clause kind where they are
93   /// found.
94   struct MappedExprComponentTy {
95     OMPClauseMappableExprCommon::MappableExprComponentLists Components;
96     OpenMPClauseKind Kind = OMPC_unknown;
97   };
98   using MappedExprComponentsTy =
99       llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>;
100   using CriticalsWithHintsTy =
101       llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>;
102   struct ReductionData {
103     using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>;
104     SourceRange ReductionRange;
105     llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp;
106     ReductionData() = default;
107     void set(BinaryOperatorKind BO, SourceRange RR) {
108       ReductionRange = RR;
109       ReductionOp = BO;
110     }
111     void set(const Expr *RefExpr, SourceRange RR) {
112       ReductionRange = RR;
113       ReductionOp = RefExpr;
114     }
115   };
116   using DeclReductionMapTy =
117       llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>;
118 
119   struct SharingMapTy {
120     DeclSAMapTy SharingMap;
121     DeclReductionMapTy ReductionMap;
122     AlignedMapTy AlignedMap;
123     MappedExprComponentsTy MappedExprComponents;
124     LoopControlVariablesMapTy LCVMap;
125     DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
126     SourceLocation DefaultAttrLoc;
127     DefaultMapAttributes DefaultMapAttr = DMA_unspecified;
128     SourceLocation DefaultMapAttrLoc;
129     OpenMPDirectiveKind Directive = OMPD_unknown;
130     DeclarationNameInfo DirectiveName;
131     Scope *CurScope = nullptr;
132     SourceLocation ConstructLoc;
133     /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
134     /// get the data (loop counters etc.) about enclosing loop-based construct.
135     /// This data is required during codegen.
136     DoacrossDependMapTy DoacrossDepends;
137     /// First argument (Expr *) contains optional argument of the
138     /// 'ordered' clause, the second one is true if the regions has 'ordered'
139     /// clause, false otherwise.
140     llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion;
141     unsigned AssociatedLoops = 1;
142     bool HasMutipleLoops = false;
143     const Decl *PossiblyLoopCounter = nullptr;
144     bool NowaitRegion = false;
145     bool CancelRegion = false;
146     bool LoopStart = false;
147     bool BodyComplete = false;
148     SourceLocation InnerTeamsRegionLoc;
149     /// Reference to the taskgroup task_reduction reference expression.
150     Expr *TaskgroupReductionRef = nullptr;
151     llvm::DenseSet<QualType> MappedClassesQualTypes;
152     /// List of globals marked as declare target link in this target region
153     /// (isOpenMPTargetExecutionDirective(Directive) == true).
154     llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls;
155     SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
156                  Scope *CurScope, SourceLocation Loc)
157         : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
158           ConstructLoc(Loc) {}
159     SharingMapTy() = default;
160   };
161 
162   using StackTy = SmallVector<SharingMapTy, 4>;
163 
164   /// Stack of used declaration and their data-sharing attributes.
165   DeclSAMapTy Threadprivates;
166   const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
167   SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack;
168   /// true, if check for DSA must be from parent directive, false, if
169   /// from current directive.
170   OpenMPClauseKind ClauseKindMode = OMPC_unknown;
171   Sema &SemaRef;
172   bool ForceCapturing = false;
173   /// true if all the vaiables in the target executable directives must be
174   /// captured by reference.
175   bool ForceCaptureByReferenceInTargetExecutable = false;
176   CriticalsWithHintsTy Criticals;
177   unsigned IgnoredStackElements = 0;
178 
179   /// Iterators over the stack iterate in order from innermost to outermost
180   /// directive.
181   using const_iterator = StackTy::const_reverse_iterator;
182   const_iterator begin() const {
183     return Stack.empty() ? const_iterator()
184                          : Stack.back().first.rbegin() + IgnoredStackElements;
185   }
186   const_iterator end() const {
187     return Stack.empty() ? const_iterator() : Stack.back().first.rend();
188   }
189   using iterator = StackTy::reverse_iterator;
190   iterator begin() {
191     return Stack.empty() ? iterator()
192                          : Stack.back().first.rbegin() + IgnoredStackElements;
193   }
194   iterator end() {
195     return Stack.empty() ? iterator() : Stack.back().first.rend();
196   }
197 
198   // Convenience operations to get at the elements of the stack.
199 
200   bool isStackEmpty() const {
201     return Stack.empty() ||
202            Stack.back().second != CurrentNonCapturingFunctionScope ||
203            Stack.back().first.size() <= IgnoredStackElements;
204   }
205   size_t getStackSize() const {
206     return isStackEmpty() ? 0
207                           : Stack.back().first.size() - IgnoredStackElements;
208   }
209 
210   SharingMapTy *getTopOfStackOrNull() {
211     size_t Size = getStackSize();
212     if (Size == 0)
213       return nullptr;
214     return &Stack.back().first[Size - 1];
215   }
216   const SharingMapTy *getTopOfStackOrNull() const {
217     return const_cast<DSAStackTy&>(*this).getTopOfStackOrNull();
218   }
219   SharingMapTy &getTopOfStack() {
220     assert(!isStackEmpty() && "no current directive");
221     return *getTopOfStackOrNull();
222   }
223   const SharingMapTy &getTopOfStack() const {
224     return const_cast<DSAStackTy&>(*this).getTopOfStack();
225   }
226 
227   SharingMapTy *getSecondOnStackOrNull() {
228     size_t Size = getStackSize();
229     if (Size <= 1)
230       return nullptr;
231     return &Stack.back().first[Size - 2];
232   }
233   const SharingMapTy *getSecondOnStackOrNull() const {
234     return const_cast<DSAStackTy&>(*this).getSecondOnStackOrNull();
235   }
236 
237   /// Get the stack element at a certain level (previously returned by
238   /// \c getNestingLevel).
239   ///
240   /// Note that nesting levels count from outermost to innermost, and this is
241   /// the reverse of our iteration order where new inner levels are pushed at
242   /// the front of the stack.
243   SharingMapTy &getStackElemAtLevel(unsigned Level) {
244     assert(Level < getStackSize() && "no such stack element");
245     return Stack.back().first[Level];
246   }
247   const SharingMapTy &getStackElemAtLevel(unsigned Level) const {
248     return const_cast<DSAStackTy&>(*this).getStackElemAtLevel(Level);
249   }
250 
251   DSAVarData getDSA(const_iterator &Iter, ValueDecl *D) const;
252 
253   /// Checks if the variable is a local for OpenMP region.
254   bool isOpenMPLocal(VarDecl *D, const_iterator Iter) const;
255 
256   /// Vector of previously declared requires directives
257   SmallVector<const OMPRequiresDecl *, 2> RequiresDecls;
258   /// omp_allocator_handle_t type.
259   QualType OMPAllocatorHandleT;
260   /// Expression for the predefined allocators.
261   Expr *OMPPredefinedAllocators[OMPAllocateDeclAttr::OMPUserDefinedMemAlloc] = {
262       nullptr};
263   /// Vector of previously encountered target directives
264   SmallVector<SourceLocation, 2> TargetLocations;
265 
266 public:
267   explicit DSAStackTy(Sema &S) : SemaRef(S) {}
268 
269   /// Sets omp_allocator_handle_t type.
270   void setOMPAllocatorHandleT(QualType Ty) { OMPAllocatorHandleT = Ty; }
271   /// Gets omp_allocator_handle_t type.
272   QualType getOMPAllocatorHandleT() const { return OMPAllocatorHandleT; }
273   /// Sets the given default allocator.
274   void setAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
275                     Expr *Allocator) {
276     OMPPredefinedAllocators[AllocatorKind] = Allocator;
277   }
278   /// Returns the specified default allocator.
279   Expr *getAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind) const {
280     return OMPPredefinedAllocators[AllocatorKind];
281   }
282 
283   bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
284   OpenMPClauseKind getClauseParsingMode() const {
285     assert(isClauseParsingMode() && "Must be in clause parsing mode.");
286     return ClauseKindMode;
287   }
288   void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
289 
290   bool isBodyComplete() const {
291     const SharingMapTy *Top = getTopOfStackOrNull();
292     return Top && Top->BodyComplete;
293   }
294   void setBodyComplete() {
295     getTopOfStack().BodyComplete = true;
296   }
297 
298   bool isForceVarCapturing() const { return ForceCapturing; }
299   void setForceVarCapturing(bool V) { ForceCapturing = V; }
300 
301   void setForceCaptureByReferenceInTargetExecutable(bool V) {
302     ForceCaptureByReferenceInTargetExecutable = V;
303   }
304   bool isForceCaptureByReferenceInTargetExecutable() const {
305     return ForceCaptureByReferenceInTargetExecutable;
306   }
307 
308   void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
309             Scope *CurScope, SourceLocation Loc) {
310     assert(!IgnoredStackElements &&
311            "cannot change stack while ignoring elements");
312     if (Stack.empty() ||
313         Stack.back().second != CurrentNonCapturingFunctionScope)
314       Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope);
315     Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc);
316     Stack.back().first.back().DefaultAttrLoc = Loc;
317   }
318 
319   void pop() {
320     assert(!IgnoredStackElements &&
321            "cannot change stack while ignoring elements");
322     assert(!Stack.back().first.empty() &&
323            "Data-sharing attributes stack is empty!");
324     Stack.back().first.pop_back();
325   }
326 
327   /// RAII object to temporarily leave the scope of a directive when we want to
328   /// logically operate in its parent.
329   class ParentDirectiveScope {
330     DSAStackTy &Self;
331     bool Active;
332   public:
333     ParentDirectiveScope(DSAStackTy &Self, bool Activate)
334         : Self(Self), Active(false) {
335       if (Activate)
336         enable();
337     }
338     ~ParentDirectiveScope() { disable(); }
339     void disable() {
340       if (Active) {
341         --Self.IgnoredStackElements;
342         Active = false;
343       }
344     }
345     void enable() {
346       if (!Active) {
347         ++Self.IgnoredStackElements;
348         Active = true;
349       }
350     }
351   };
352 
353   /// Marks that we're started loop parsing.
354   void loopInit() {
355     assert(isOpenMPLoopDirective(getCurrentDirective()) &&
356            "Expected loop-based directive.");
357     getTopOfStack().LoopStart = true;
358   }
359   /// Start capturing of the variables in the loop context.
360   void loopStart() {
361     assert(isOpenMPLoopDirective(getCurrentDirective()) &&
362            "Expected loop-based directive.");
363     getTopOfStack().LoopStart = false;
364   }
365   /// true, if variables are captured, false otherwise.
366   bool isLoopStarted() const {
367     assert(isOpenMPLoopDirective(getCurrentDirective()) &&
368            "Expected loop-based directive.");
369     return !getTopOfStack().LoopStart;
370   }
371   /// Marks (or clears) declaration as possibly loop counter.
372   void resetPossibleLoopCounter(const Decl *D = nullptr) {
373     getTopOfStack().PossiblyLoopCounter =
374         D ? D->getCanonicalDecl() : D;
375   }
376   /// Gets the possible loop counter decl.
377   const Decl *getPossiblyLoopCunter() const {
378     return getTopOfStack().PossiblyLoopCounter;
379   }
380   /// Start new OpenMP region stack in new non-capturing function.
381   void pushFunction() {
382     assert(!IgnoredStackElements &&
383            "cannot change stack while ignoring elements");
384     const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
385     assert(!isa<CapturingScopeInfo>(CurFnScope));
386     CurrentNonCapturingFunctionScope = CurFnScope;
387   }
388   /// Pop region stack for non-capturing function.
389   void popFunction(const FunctionScopeInfo *OldFSI) {
390     assert(!IgnoredStackElements &&
391            "cannot change stack while ignoring elements");
392     if (!Stack.empty() && Stack.back().second == OldFSI) {
393       assert(Stack.back().first.empty());
394       Stack.pop_back();
395     }
396     CurrentNonCapturingFunctionScope = nullptr;
397     for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) {
398       if (!isa<CapturingScopeInfo>(FSI)) {
399         CurrentNonCapturingFunctionScope = FSI;
400         break;
401       }
402     }
403   }
404 
405   void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) {
406     Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint);
407   }
408   const std::pair<const OMPCriticalDirective *, llvm::APSInt>
409   getCriticalWithHint(const DeclarationNameInfo &Name) const {
410     auto I = Criticals.find(Name.getAsString());
411     if (I != Criticals.end())
412       return I->second;
413     return std::make_pair(nullptr, llvm::APSInt());
414   }
415   /// If 'aligned' declaration for given variable \a D was not seen yet,
416   /// add it and return NULL; otherwise return previous occurrence's expression
417   /// for diagnostics.
418   const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE);
419 
420   /// Register specified variable as loop control variable.
421   void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture);
422   /// Check if the specified variable is a loop control variable for
423   /// current region.
424   /// \return The index of the loop control variable in the list of associated
425   /// for-loops (from outer to inner).
426   const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const;
427   /// Check if the specified variable is a loop control variable for
428   /// parent region.
429   /// \return The index of the loop control variable in the list of associated
430   /// for-loops (from outer to inner).
431   const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const;
432   /// Get the loop control variable for the I-th loop (or nullptr) in
433   /// parent directive.
434   const ValueDecl *getParentLoopControlVariable(unsigned I) const;
435 
436   /// Adds explicit data sharing attribute to the specified declaration.
437   void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
438               DeclRefExpr *PrivateCopy = nullptr);
439 
440   /// Adds additional information for the reduction items with the reduction id
441   /// represented as an operator.
442   void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
443                                  BinaryOperatorKind BOK);
444   /// Adds additional information for the reduction items with the reduction id
445   /// represented as reduction identifier.
446   void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
447                                  const Expr *ReductionRef);
448   /// Returns the location and reduction operation from the innermost parent
449   /// region for the given \p D.
450   const DSAVarData
451   getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
452                                    BinaryOperatorKind &BOK,
453                                    Expr *&TaskgroupDescriptor) const;
454   /// Returns the location and reduction operation from the innermost parent
455   /// region for the given \p D.
456   const DSAVarData
457   getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
458                                    const Expr *&ReductionRef,
459                                    Expr *&TaskgroupDescriptor) const;
460   /// Return reduction reference expression for the current taskgroup.
461   Expr *getTaskgroupReductionRef() const {
462     assert(getTopOfStack().Directive == OMPD_taskgroup &&
463            "taskgroup reference expression requested for non taskgroup "
464            "directive.");
465     return getTopOfStack().TaskgroupReductionRef;
466   }
467   /// Checks if the given \p VD declaration is actually a taskgroup reduction
468   /// descriptor variable at the \p Level of OpenMP regions.
469   bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const {
470     return getStackElemAtLevel(Level).TaskgroupReductionRef &&
471            cast<DeclRefExpr>(getStackElemAtLevel(Level).TaskgroupReductionRef)
472                    ->getDecl() == VD;
473   }
474 
475   /// Returns data sharing attributes from top of the stack for the
476   /// specified declaration.
477   const DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
478   /// Returns data-sharing attributes for the specified declaration.
479   const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const;
480   /// Checks if the specified variables has data-sharing attributes which
481   /// match specified \a CPred predicate in any directive which matches \a DPred
482   /// predicate.
483   const DSAVarData
484   hasDSA(ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
485          const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
486          bool FromParent) const;
487   /// Checks if the specified variables has data-sharing attributes which
488   /// match specified \a CPred predicate in any innermost directive which
489   /// matches \a DPred predicate.
490   const DSAVarData
491   hasInnermostDSA(ValueDecl *D,
492                   const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
493                   const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
494                   bool FromParent) const;
495   /// Checks if the specified variables has explicit data-sharing
496   /// attributes which match specified \a CPred predicate at the specified
497   /// OpenMP region.
498   bool hasExplicitDSA(const ValueDecl *D,
499                       const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
500                       unsigned Level, bool NotLastprivate = false) const;
501 
502   /// Returns true if the directive at level \Level matches in the
503   /// specified \a DPred predicate.
504   bool hasExplicitDirective(
505       const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
506       unsigned Level) const;
507 
508   /// Finds a directive which matches specified \a DPred predicate.
509   bool hasDirective(
510       const llvm::function_ref<bool(
511           OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)>
512           DPred,
513       bool FromParent) const;
514 
515   /// Returns currently analyzed directive.
516   OpenMPDirectiveKind getCurrentDirective() const {
517     const SharingMapTy *Top = getTopOfStackOrNull();
518     return Top ? Top->Directive : OMPD_unknown;
519   }
520   /// Returns directive kind at specified level.
521   OpenMPDirectiveKind getDirective(unsigned Level) const {
522     assert(!isStackEmpty() && "No directive at specified level.");
523     return getStackElemAtLevel(Level).Directive;
524   }
525   /// Returns the capture region at the specified level.
526   OpenMPDirectiveKind getCaptureRegion(unsigned Level,
527                                        unsigned OpenMPCaptureLevel) const {
528     SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
529     getOpenMPCaptureRegions(CaptureRegions, getDirective(Level));
530     return CaptureRegions[OpenMPCaptureLevel];
531   }
532   /// Returns parent directive.
533   OpenMPDirectiveKind getParentDirective() const {
534     const SharingMapTy *Parent = getSecondOnStackOrNull();
535     return Parent ? Parent->Directive : OMPD_unknown;
536   }
537 
538   /// Add requires decl to internal vector
539   void addRequiresDecl(OMPRequiresDecl *RD) {
540     RequiresDecls.push_back(RD);
541   }
542 
543   /// Checks if the defined 'requires' directive has specified type of clause.
544   template <typename ClauseType>
545   bool hasRequiresDeclWithClause() {
546     return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
547       return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
548         return isa<ClauseType>(C);
549       });
550     });
551   }
552 
553   /// Checks for a duplicate clause amongst previously declared requires
554   /// directives
555   bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const {
556     bool IsDuplicate = false;
557     for (OMPClause *CNew : ClauseList) {
558       for (const OMPRequiresDecl *D : RequiresDecls) {
559         for (const OMPClause *CPrev : D->clauselists()) {
560           if (CNew->getClauseKind() == CPrev->getClauseKind()) {
561             SemaRef.Diag(CNew->getBeginLoc(),
562                          diag::err_omp_requires_clause_redeclaration)
563                 << getOpenMPClauseName(CNew->getClauseKind());
564             SemaRef.Diag(CPrev->getBeginLoc(),
565                          diag::note_omp_requires_previous_clause)
566                 << getOpenMPClauseName(CPrev->getClauseKind());
567             IsDuplicate = true;
568           }
569         }
570       }
571     }
572     return IsDuplicate;
573   }
574 
575   /// Add location of previously encountered target to internal vector
576   void addTargetDirLocation(SourceLocation LocStart) {
577     TargetLocations.push_back(LocStart);
578   }
579 
580   // Return previously encountered target region locations.
581   ArrayRef<SourceLocation> getEncounteredTargetLocs() const {
582     return TargetLocations;
583   }
584 
585   /// Set default data sharing attribute to none.
586   void setDefaultDSANone(SourceLocation Loc) {
587     getTopOfStack().DefaultAttr = DSA_none;
588     getTopOfStack().DefaultAttrLoc = Loc;
589   }
590   /// Set default data sharing attribute to shared.
591   void setDefaultDSAShared(SourceLocation Loc) {
592     getTopOfStack().DefaultAttr = DSA_shared;
593     getTopOfStack().DefaultAttrLoc = Loc;
594   }
595   /// Set default data mapping attribute to 'tofrom:scalar'.
596   void setDefaultDMAToFromScalar(SourceLocation Loc) {
597     getTopOfStack().DefaultMapAttr = DMA_tofrom_scalar;
598     getTopOfStack().DefaultMapAttrLoc = Loc;
599   }
600 
601   DefaultDataSharingAttributes getDefaultDSA() const {
602     return isStackEmpty() ? DSA_unspecified
603                           : getTopOfStack().DefaultAttr;
604   }
605   SourceLocation getDefaultDSALocation() const {
606     return isStackEmpty() ? SourceLocation()
607                           : getTopOfStack().DefaultAttrLoc;
608   }
609   DefaultMapAttributes getDefaultDMA() const {
610     return isStackEmpty() ? DMA_unspecified
611                           : getTopOfStack().DefaultMapAttr;
612   }
613   DefaultMapAttributes getDefaultDMAAtLevel(unsigned Level) const {
614     return getStackElemAtLevel(Level).DefaultMapAttr;
615   }
616   SourceLocation getDefaultDMALocation() const {
617     return isStackEmpty() ? SourceLocation()
618                           : getTopOfStack().DefaultMapAttrLoc;
619   }
620 
621   /// Checks if the specified variable is a threadprivate.
622   bool isThreadPrivate(VarDecl *D) {
623     const DSAVarData DVar = getTopDSA(D, false);
624     return isOpenMPThreadPrivate(DVar.CKind);
625   }
626 
627   /// Marks current region as ordered (it has an 'ordered' clause).
628   void setOrderedRegion(bool IsOrdered, const Expr *Param,
629                         OMPOrderedClause *Clause) {
630     if (IsOrdered)
631       getTopOfStack().OrderedRegion.emplace(Param, Clause);
632     else
633       getTopOfStack().OrderedRegion.reset();
634   }
635   /// Returns true, if region is ordered (has associated 'ordered' clause),
636   /// false - otherwise.
637   bool isOrderedRegion() const {
638     if (const SharingMapTy *Top = getTopOfStackOrNull())
639       return Top->OrderedRegion.hasValue();
640     return false;
641   }
642   /// Returns optional parameter for the ordered region.
643   std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const {
644     if (const SharingMapTy *Top = getTopOfStackOrNull())
645       if (Top->OrderedRegion.hasValue())
646         return Top->OrderedRegion.getValue();
647     return std::make_pair(nullptr, nullptr);
648   }
649   /// Returns true, if parent region is ordered (has associated
650   /// 'ordered' clause), false - otherwise.
651   bool isParentOrderedRegion() const {
652     if (const SharingMapTy *Parent = getSecondOnStackOrNull())
653       return Parent->OrderedRegion.hasValue();
654     return false;
655   }
656   /// Returns optional parameter for the ordered region.
657   std::pair<const Expr *, OMPOrderedClause *>
658   getParentOrderedRegionParam() const {
659     if (const SharingMapTy *Parent = getSecondOnStackOrNull())
660       if (Parent->OrderedRegion.hasValue())
661         return Parent->OrderedRegion.getValue();
662     return std::make_pair(nullptr, nullptr);
663   }
664   /// Marks current region as nowait (it has a 'nowait' clause).
665   void setNowaitRegion(bool IsNowait = true) {
666     getTopOfStack().NowaitRegion = IsNowait;
667   }
668   /// Returns true, if parent region is nowait (has associated
669   /// 'nowait' clause), false - otherwise.
670   bool isParentNowaitRegion() const {
671     if (const SharingMapTy *Parent = getSecondOnStackOrNull())
672       return Parent->NowaitRegion;
673     return false;
674   }
675   /// Marks parent region as cancel region.
676   void setParentCancelRegion(bool Cancel = true) {
677     if (SharingMapTy *Parent = getSecondOnStackOrNull())
678       Parent->CancelRegion |= Cancel;
679   }
680   /// Return true if current region has inner cancel construct.
681   bool isCancelRegion() const {
682     const SharingMapTy *Top = getTopOfStackOrNull();
683     return Top ? Top->CancelRegion : false;
684   }
685 
686   /// Set collapse value for the region.
687   void setAssociatedLoops(unsigned Val) {
688     getTopOfStack().AssociatedLoops = Val;
689     if (Val > 1)
690       getTopOfStack().HasMutipleLoops = true;
691   }
692   /// Return collapse value for region.
693   unsigned getAssociatedLoops() const {
694     const SharingMapTy *Top = getTopOfStackOrNull();
695     return Top ? Top->AssociatedLoops : 0;
696   }
697   /// Returns true if the construct is associated with multiple loops.
698   bool hasMutipleLoops() const {
699     const SharingMapTy *Top = getTopOfStackOrNull();
700     return Top ? Top->HasMutipleLoops : false;
701   }
702 
703   /// Marks current target region as one with closely nested teams
704   /// region.
705   void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
706     if (SharingMapTy *Parent = getSecondOnStackOrNull())
707       Parent->InnerTeamsRegionLoc = TeamsRegionLoc;
708   }
709   /// Returns true, if current region has closely nested teams region.
710   bool hasInnerTeamsRegion() const {
711     return getInnerTeamsRegionLoc().isValid();
712   }
713   /// Returns location of the nested teams region (if any).
714   SourceLocation getInnerTeamsRegionLoc() const {
715     const SharingMapTy *Top = getTopOfStackOrNull();
716     return Top ? Top->InnerTeamsRegionLoc : SourceLocation();
717   }
718 
719   Scope *getCurScope() const {
720     const SharingMapTy *Top = getTopOfStackOrNull();
721     return Top ? Top->CurScope : nullptr;
722   }
723   SourceLocation getConstructLoc() const {
724     const SharingMapTy *Top = getTopOfStackOrNull();
725     return Top ? Top->ConstructLoc : SourceLocation();
726   }
727 
728   /// Do the check specified in \a Check to all component lists and return true
729   /// if any issue is found.
730   bool checkMappableExprComponentListsForDecl(
731       const ValueDecl *VD, bool CurrentRegionOnly,
732       const llvm::function_ref<
733           bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
734                OpenMPClauseKind)>
735           Check) const {
736     if (isStackEmpty())
737       return false;
738     auto SI = begin();
739     auto SE = end();
740 
741     if (SI == SE)
742       return false;
743 
744     if (CurrentRegionOnly)
745       SE = std::next(SI);
746     else
747       std::advance(SI, 1);
748 
749     for (; SI != SE; ++SI) {
750       auto MI = SI->MappedExprComponents.find(VD);
751       if (MI != SI->MappedExprComponents.end())
752         for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
753              MI->second.Components)
754           if (Check(L, MI->second.Kind))
755             return true;
756     }
757     return false;
758   }
759 
760   /// Do the check specified in \a Check to all component lists at a given level
761   /// and return true if any issue is found.
762   bool checkMappableExprComponentListsForDeclAtLevel(
763       const ValueDecl *VD, unsigned Level,
764       const llvm::function_ref<
765           bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
766                OpenMPClauseKind)>
767           Check) const {
768     if (getStackSize() <= Level)
769       return false;
770 
771     const SharingMapTy &StackElem = getStackElemAtLevel(Level);
772     auto MI = StackElem.MappedExprComponents.find(VD);
773     if (MI != StackElem.MappedExprComponents.end())
774       for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
775            MI->second.Components)
776         if (Check(L, MI->second.Kind))
777           return true;
778     return false;
779   }
780 
781   /// Create a new mappable expression component list associated with a given
782   /// declaration and initialize it with the provided list of components.
783   void addMappableExpressionComponents(
784       const ValueDecl *VD,
785       OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
786       OpenMPClauseKind WhereFoundClauseKind) {
787     MappedExprComponentTy &MEC = getTopOfStack().MappedExprComponents[VD];
788     // Create new entry and append the new components there.
789     MEC.Components.resize(MEC.Components.size() + 1);
790     MEC.Components.back().append(Components.begin(), Components.end());
791     MEC.Kind = WhereFoundClauseKind;
792   }
793 
794   unsigned getNestingLevel() const {
795     assert(!isStackEmpty());
796     return getStackSize() - 1;
797   }
798   void addDoacrossDependClause(OMPDependClause *C,
799                                const OperatorOffsetTy &OpsOffs) {
800     SharingMapTy *Parent = getSecondOnStackOrNull();
801     assert(Parent && isOpenMPWorksharingDirective(Parent->Directive));
802     Parent->DoacrossDepends.try_emplace(C, OpsOffs);
803   }
804   llvm::iterator_range<DoacrossDependMapTy::const_iterator>
805   getDoacrossDependClauses() const {
806     const SharingMapTy &StackElem = getTopOfStack();
807     if (isOpenMPWorksharingDirective(StackElem.Directive)) {
808       const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends;
809       return llvm::make_range(Ref.begin(), Ref.end());
810     }
811     return llvm::make_range(StackElem.DoacrossDepends.end(),
812                             StackElem.DoacrossDepends.end());
813   }
814 
815   // Store types of classes which have been explicitly mapped
816   void addMappedClassesQualTypes(QualType QT) {
817     SharingMapTy &StackElem = getTopOfStack();
818     StackElem.MappedClassesQualTypes.insert(QT);
819   }
820 
821   // Return set of mapped classes types
822   bool isClassPreviouslyMapped(QualType QT) const {
823     const SharingMapTy &StackElem = getTopOfStack();
824     return StackElem.MappedClassesQualTypes.count(QT) != 0;
825   }
826 
827   /// Adds global declare target to the parent target region.
828   void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) {
829     assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
830                E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link &&
831            "Expected declare target link global.");
832     for (auto &Elem : *this) {
833       if (isOpenMPTargetExecutionDirective(Elem.Directive)) {
834         Elem.DeclareTargetLinkVarDecls.push_back(E);
835         return;
836       }
837     }
838   }
839 
840   /// Returns the list of globals with declare target link if current directive
841   /// is target.
842   ArrayRef<DeclRefExpr *> getLinkGlobals() const {
843     assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) &&
844            "Expected target executable directive.");
845     return getTopOfStack().DeclareTargetLinkVarDecls;
846   }
847 };
848 
849 bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) {
850   return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind);
851 }
852 
853 bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) {
854   return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(DKind) ||
855          DKind == OMPD_unknown;
856 }
857 
858 } // namespace
859 
860 static const Expr *getExprAsWritten(const Expr *E) {
861   if (const auto *FE = dyn_cast<FullExpr>(E))
862     E = FE->getSubExpr();
863 
864   if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
865     E = MTE->GetTemporaryExpr();
866 
867   while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
868     E = Binder->getSubExpr();
869 
870   if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
871     E = ICE->getSubExprAsWritten();
872   return E->IgnoreParens();
873 }
874 
875 static Expr *getExprAsWritten(Expr *E) {
876   return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E)));
877 }
878 
879 static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
880   if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
881     if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
882       D = ME->getMemberDecl();
883   const auto *VD = dyn_cast<VarDecl>(D);
884   const auto *FD = dyn_cast<FieldDecl>(D);
885   if (VD != nullptr) {
886     VD = VD->getCanonicalDecl();
887     D = VD;
888   } else {
889     assert(FD);
890     FD = FD->getCanonicalDecl();
891     D = FD;
892   }
893   return D;
894 }
895 
896 static ValueDecl *getCanonicalDecl(ValueDecl *D) {
897   return const_cast<ValueDecl *>(
898       getCanonicalDecl(const_cast<const ValueDecl *>(D)));
899 }
900 
901 DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter,
902                                           ValueDecl *D) const {
903   D = getCanonicalDecl(D);
904   auto *VD = dyn_cast<VarDecl>(D);
905   const auto *FD = dyn_cast<FieldDecl>(D);
906   DSAVarData DVar;
907   if (Iter == end()) {
908     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
909     // in a region but not in construct]
910     //  File-scope or namespace-scope variables referenced in called routines
911     //  in the region are shared unless they appear in a threadprivate
912     //  directive.
913     if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD))
914       DVar.CKind = OMPC_shared;
915 
916     // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
917     // in a region but not in construct]
918     //  Variables with static storage duration that are declared in called
919     //  routines in the region are shared.
920     if (VD && VD->hasGlobalStorage())
921       DVar.CKind = OMPC_shared;
922 
923     // Non-static data members are shared by default.
924     if (FD)
925       DVar.CKind = OMPC_shared;
926 
927     return DVar;
928   }
929 
930   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
931   // in a Construct, C/C++, predetermined, p.1]
932   // Variables with automatic storage duration that are declared in a scope
933   // inside the construct are private.
934   if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
935       (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
936     DVar.CKind = OMPC_private;
937     return DVar;
938   }
939 
940   DVar.DKind = Iter->Directive;
941   // Explicitly specified attributes and local variables with predetermined
942   // attributes.
943   if (Iter->SharingMap.count(D)) {
944     const DSAInfo &Data = Iter->SharingMap.lookup(D);
945     DVar.RefExpr = Data.RefExpr.getPointer();
946     DVar.PrivateCopy = Data.PrivateCopy;
947     DVar.CKind = Data.Attributes;
948     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
949     return DVar;
950   }
951 
952   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
953   // in a Construct, C/C++, implicitly determined, p.1]
954   //  In a parallel or task construct, the data-sharing attributes of these
955   //  variables are determined by the default clause, if present.
956   switch (Iter->DefaultAttr) {
957   case DSA_shared:
958     DVar.CKind = OMPC_shared;
959     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
960     return DVar;
961   case DSA_none:
962     return DVar;
963   case DSA_unspecified:
964     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
965     // in a Construct, implicitly determined, p.2]
966     //  In a parallel construct, if no default clause is present, these
967     //  variables are shared.
968     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
969     if (isOpenMPParallelDirective(DVar.DKind) ||
970         isOpenMPTeamsDirective(DVar.DKind)) {
971       DVar.CKind = OMPC_shared;
972       return DVar;
973     }
974 
975     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
976     // in a Construct, implicitly determined, p.4]
977     //  In a task construct, if no default clause is present, a variable that in
978     //  the enclosing context is determined to be shared by all implicit tasks
979     //  bound to the current team is shared.
980     if (isOpenMPTaskingDirective(DVar.DKind)) {
981       DSAVarData DVarTemp;
982       const_iterator I = Iter, E = end();
983       do {
984         ++I;
985         // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
986         // Referenced in a Construct, implicitly determined, p.6]
987         //  In a task construct, if no default clause is present, a variable
988         //  whose data-sharing attribute is not determined by the rules above is
989         //  firstprivate.
990         DVarTemp = getDSA(I, D);
991         if (DVarTemp.CKind != OMPC_shared) {
992           DVar.RefExpr = nullptr;
993           DVar.CKind = OMPC_firstprivate;
994           return DVar;
995         }
996       } while (I != E && !isImplicitTaskingRegion(I->Directive));
997       DVar.CKind =
998           (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
999       return DVar;
1000     }
1001   }
1002   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1003   // in a Construct, implicitly determined, p.3]
1004   //  For constructs other than task, if no default clause is present, these
1005   //  variables inherit their data-sharing attributes from the enclosing
1006   //  context.
1007   return getDSA(++Iter, D);
1008 }
1009 
1010 const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
1011                                          const Expr *NewDE) {
1012   assert(!isStackEmpty() && "Data sharing attributes stack is empty");
1013   D = getCanonicalDecl(D);
1014   SharingMapTy &StackElem = getTopOfStack();
1015   auto It = StackElem.AlignedMap.find(D);
1016   if (It == StackElem.AlignedMap.end()) {
1017     assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
1018     StackElem.AlignedMap[D] = NewDE;
1019     return nullptr;
1020   }
1021   assert(It->second && "Unexpected nullptr expr in the aligned map");
1022   return It->second;
1023 }
1024 
1025 void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
1026   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1027   D = getCanonicalDecl(D);
1028   SharingMapTy &StackElem = getTopOfStack();
1029   StackElem.LCVMap.try_emplace(
1030       D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
1031 }
1032 
1033 const DSAStackTy::LCDeclInfo
1034 DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
1035   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1036   D = getCanonicalDecl(D);
1037   const SharingMapTy &StackElem = getTopOfStack();
1038   auto It = StackElem.LCVMap.find(D);
1039   if (It != StackElem.LCVMap.end())
1040     return It->second;
1041   return {0, nullptr};
1042 }
1043 
1044 const DSAStackTy::LCDeclInfo
1045 DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
1046   const SharingMapTy *Parent = getSecondOnStackOrNull();
1047   assert(Parent && "Data-sharing attributes stack is empty");
1048   D = getCanonicalDecl(D);
1049   auto It = Parent->LCVMap.find(D);
1050   if (It != Parent->LCVMap.end())
1051     return It->second;
1052   return {0, nullptr};
1053 }
1054 
1055 const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
1056   const SharingMapTy *Parent = getSecondOnStackOrNull();
1057   assert(Parent && "Data-sharing attributes stack is empty");
1058   if (Parent->LCVMap.size() < I)
1059     return nullptr;
1060   for (const auto &Pair : Parent->LCVMap)
1061     if (Pair.second.first == I)
1062       return Pair.first;
1063   return nullptr;
1064 }
1065 
1066 void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
1067                         DeclRefExpr *PrivateCopy) {
1068   D = getCanonicalDecl(D);
1069   if (A == OMPC_threadprivate) {
1070     DSAInfo &Data = Threadprivates[D];
1071     Data.Attributes = A;
1072     Data.RefExpr.setPointer(E);
1073     Data.PrivateCopy = nullptr;
1074   } else {
1075     DSAInfo &Data = getTopOfStack().SharingMap[D];
1076     assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
1077            (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
1078            (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
1079            (isLoopControlVariable(D).first && A == OMPC_private));
1080     if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
1081       Data.RefExpr.setInt(/*IntVal=*/true);
1082       return;
1083     }
1084     const bool IsLastprivate =
1085         A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
1086     Data.Attributes = A;
1087     Data.RefExpr.setPointerAndInt(E, IsLastprivate);
1088     Data.PrivateCopy = PrivateCopy;
1089     if (PrivateCopy) {
1090       DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()];
1091       Data.Attributes = A;
1092       Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
1093       Data.PrivateCopy = nullptr;
1094     }
1095   }
1096 }
1097 
1098 /// Build a variable declaration for OpenMP loop iteration variable.
1099 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
1100                              StringRef Name, const AttrVec *Attrs = nullptr,
1101                              DeclRefExpr *OrigRef = nullptr) {
1102   DeclContext *DC = SemaRef.CurContext;
1103   IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
1104   TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
1105   auto *Decl =
1106       VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
1107   if (Attrs) {
1108     for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
1109          I != E; ++I)
1110       Decl->addAttr(*I);
1111   }
1112   Decl->setImplicit();
1113   if (OrigRef) {
1114     Decl->addAttr(
1115         OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef));
1116   }
1117   return Decl;
1118 }
1119 
1120 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
1121                                      SourceLocation Loc,
1122                                      bool RefersToCapture = false) {
1123   D->setReferenced();
1124   D->markUsed(S.Context);
1125   return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
1126                              SourceLocation(), D, RefersToCapture, Loc, Ty,
1127                              VK_LValue);
1128 }
1129 
1130 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1131                                            BinaryOperatorKind BOK) {
1132   D = getCanonicalDecl(D);
1133   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1134   assert(
1135       getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
1136       "Additional reduction info may be specified only for reduction items.");
1137   ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
1138   assert(ReductionData.ReductionRange.isInvalid() &&
1139          getTopOfStack().Directive == OMPD_taskgroup &&
1140          "Additional reduction info may be specified only once for reduction "
1141          "items.");
1142   ReductionData.set(BOK, SR);
1143   Expr *&TaskgroupReductionRef =
1144       getTopOfStack().TaskgroupReductionRef;
1145   if (!TaskgroupReductionRef) {
1146     VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1147                                SemaRef.Context.VoidPtrTy, ".task_red.");
1148     TaskgroupReductionRef =
1149         buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
1150   }
1151 }
1152 
1153 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1154                                            const Expr *ReductionRef) {
1155   D = getCanonicalDecl(D);
1156   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1157   assert(
1158       getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
1159       "Additional reduction info may be specified only for reduction items.");
1160   ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
1161   assert(ReductionData.ReductionRange.isInvalid() &&
1162          getTopOfStack().Directive == OMPD_taskgroup &&
1163          "Additional reduction info may be specified only once for reduction "
1164          "items.");
1165   ReductionData.set(ReductionRef, SR);
1166   Expr *&TaskgroupReductionRef =
1167       getTopOfStack().TaskgroupReductionRef;
1168   if (!TaskgroupReductionRef) {
1169     VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1170                                SemaRef.Context.VoidPtrTy, ".task_red.");
1171     TaskgroupReductionRef =
1172         buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
1173   }
1174 }
1175 
1176 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1177     const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
1178     Expr *&TaskgroupDescriptor) const {
1179   D = getCanonicalDecl(D);
1180   assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1181   for (const_iterator I = begin() + 1, E = end(); I != E; ++I) {
1182     const DSAInfo &Data = I->SharingMap.lookup(D);
1183     if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
1184       continue;
1185     const ReductionData &ReductionData = I->ReductionMap.lookup(D);
1186     if (!ReductionData.ReductionOp ||
1187         ReductionData.ReductionOp.is<const Expr *>())
1188       return DSAVarData();
1189     SR = ReductionData.ReductionRange;
1190     BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
1191     assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1192                                        "expression for the descriptor is not "
1193                                        "set.");
1194     TaskgroupDescriptor = I->TaskgroupReductionRef;
1195     return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1196                       Data.PrivateCopy, I->DefaultAttrLoc);
1197   }
1198   return DSAVarData();
1199 }
1200 
1201 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1202     const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
1203     Expr *&TaskgroupDescriptor) const {
1204   D = getCanonicalDecl(D);
1205   assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1206   for (const_iterator I = begin() + 1, E = end(); I != E; ++I) {
1207     const DSAInfo &Data = I->SharingMap.lookup(D);
1208     if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
1209       continue;
1210     const ReductionData &ReductionData = I->ReductionMap.lookup(D);
1211     if (!ReductionData.ReductionOp ||
1212         !ReductionData.ReductionOp.is<const Expr *>())
1213       return DSAVarData();
1214     SR = ReductionData.ReductionRange;
1215     ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
1216     assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1217                                        "expression for the descriptor is not "
1218                                        "set.");
1219     TaskgroupDescriptor = I->TaskgroupReductionRef;
1220     return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1221                       Data.PrivateCopy, I->DefaultAttrLoc);
1222   }
1223   return DSAVarData();
1224 }
1225 
1226 bool DSAStackTy::isOpenMPLocal(VarDecl *D, const_iterator I) const {
1227   D = D->getCanonicalDecl();
1228   for (const_iterator E = end(); I != E; ++I) {
1229     if (isImplicitOrExplicitTaskingRegion(I->Directive) ||
1230         isOpenMPTargetExecutionDirective(I->Directive)) {
1231       Scope *TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
1232       Scope *CurScope = getCurScope();
1233       while (CurScope && CurScope != TopScope && !CurScope->isDeclScope(D))
1234         CurScope = CurScope->getParent();
1235       return CurScope != TopScope;
1236     }
1237   }
1238   return false;
1239 }
1240 
1241 static bool isConstNotMutableType(Sema &SemaRef, QualType Type,
1242                                   bool AcceptIfMutable = true,
1243                                   bool *IsClassType = nullptr) {
1244   ASTContext &Context = SemaRef.getASTContext();
1245   Type = Type.getNonReferenceType().getCanonicalType();
1246   bool IsConstant = Type.isConstant(Context);
1247   Type = Context.getBaseElementType(Type);
1248   const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus
1249                                 ? Type->getAsCXXRecordDecl()
1250                                 : nullptr;
1251   if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
1252     if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
1253       RD = CTD->getTemplatedDecl();
1254   if (IsClassType)
1255     *IsClassType = RD;
1256   return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD &&
1257                          RD->hasDefinition() && RD->hasMutableFields());
1258 }
1259 
1260 static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D,
1261                                       QualType Type, OpenMPClauseKind CKind,
1262                                       SourceLocation ELoc,
1263                                       bool AcceptIfMutable = true,
1264                                       bool ListItemNotVar = false) {
1265   ASTContext &Context = SemaRef.getASTContext();
1266   bool IsClassType;
1267   if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) {
1268     unsigned Diag = ListItemNotVar
1269                         ? diag::err_omp_const_list_item
1270                         : IsClassType ? diag::err_omp_const_not_mutable_variable
1271                                       : diag::err_omp_const_variable;
1272     SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind);
1273     if (!ListItemNotVar && D) {
1274       const VarDecl *VD = dyn_cast<VarDecl>(D);
1275       bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
1276                                VarDecl::DeclarationOnly;
1277       SemaRef.Diag(D->getLocation(),
1278                    IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1279           << D;
1280     }
1281     return true;
1282   }
1283   return false;
1284 }
1285 
1286 const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1287                                                    bool FromParent) {
1288   D = getCanonicalDecl(D);
1289   DSAVarData DVar;
1290 
1291   auto *VD = dyn_cast<VarDecl>(D);
1292   auto TI = Threadprivates.find(D);
1293   if (TI != Threadprivates.end()) {
1294     DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
1295     DVar.CKind = OMPC_threadprivate;
1296     return DVar;
1297   }
1298   if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
1299     DVar.RefExpr = buildDeclRefExpr(
1300         SemaRef, VD, D->getType().getNonReferenceType(),
1301         VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
1302     DVar.CKind = OMPC_threadprivate;
1303     addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1304     return DVar;
1305   }
1306   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1307   // in a Construct, C/C++, predetermined, p.1]
1308   //  Variables appearing in threadprivate directives are threadprivate.
1309   if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
1310        !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1311          SemaRef.getLangOpts().OpenMPUseTLS &&
1312          SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
1313       (VD && VD->getStorageClass() == SC_Register &&
1314        VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
1315     DVar.RefExpr = buildDeclRefExpr(
1316         SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation());
1317     DVar.CKind = OMPC_threadprivate;
1318     addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1319     return DVar;
1320   }
1321   if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
1322       VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
1323       !isLoopControlVariable(D).first) {
1324     const_iterator IterTarget =
1325         std::find_if(begin(), end(), [](const SharingMapTy &Data) {
1326           return isOpenMPTargetExecutionDirective(Data.Directive);
1327         });
1328     if (IterTarget != end()) {
1329       const_iterator ParentIterTarget = IterTarget + 1;
1330       for (const_iterator Iter = begin();
1331            Iter != ParentIterTarget; ++Iter) {
1332         if (isOpenMPLocal(VD, Iter)) {
1333           DVar.RefExpr =
1334               buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1335                                D->getLocation());
1336           DVar.CKind = OMPC_threadprivate;
1337           return DVar;
1338         }
1339       }
1340       if (!isClauseParsingMode() || IterTarget != begin()) {
1341         auto DSAIter = IterTarget->SharingMap.find(D);
1342         if (DSAIter != IterTarget->SharingMap.end() &&
1343             isOpenMPPrivate(DSAIter->getSecond().Attributes)) {
1344           DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1345           DVar.CKind = OMPC_threadprivate;
1346           return DVar;
1347         }
1348         const_iterator End = end();
1349         if (!SemaRef.isOpenMPCapturedByRef(
1350                 D, std::distance(ParentIterTarget, End),
1351                 /*OpenMPCaptureLevel=*/0)) {
1352           DVar.RefExpr =
1353               buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1354                                IterTarget->ConstructLoc);
1355           DVar.CKind = OMPC_threadprivate;
1356           return DVar;
1357         }
1358       }
1359     }
1360   }
1361 
1362   if (isStackEmpty())
1363     // Not in OpenMP execution region and top scope was already checked.
1364     return DVar;
1365 
1366   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1367   // in a Construct, C/C++, predetermined, p.4]
1368   //  Static data members are shared.
1369   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1370   // in a Construct, C/C++, predetermined, p.7]
1371   //  Variables with static storage duration that are declared in a scope
1372   //  inside the construct are shared.
1373   if (VD && VD->isStaticDataMember()) {
1374     // Check for explicitly specified attributes.
1375     const_iterator I = begin();
1376     const_iterator EndI = end();
1377     if (FromParent && I != EndI)
1378       ++I;
1379     auto It = I->SharingMap.find(D);
1380     if (It != I->SharingMap.end()) {
1381       const DSAInfo &Data = It->getSecond();
1382       DVar.RefExpr = Data.RefExpr.getPointer();
1383       DVar.PrivateCopy = Data.PrivateCopy;
1384       DVar.CKind = Data.Attributes;
1385       DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1386       DVar.DKind = I->Directive;
1387       return DVar;
1388     }
1389 
1390     DVar.CKind = OMPC_shared;
1391     return DVar;
1392   }
1393 
1394   auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; };
1395   // The predetermined shared attribute for const-qualified types having no
1396   // mutable members was removed after OpenMP 3.1.
1397   if (SemaRef.LangOpts.OpenMP <= 31) {
1398     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1399     // in a Construct, C/C++, predetermined, p.6]
1400     //  Variables with const qualified type having no mutable member are
1401     //  shared.
1402     if (isConstNotMutableType(SemaRef, D->getType())) {
1403       // Variables with const-qualified type having no mutable member may be
1404       // listed in a firstprivate clause, even if they are static data members.
1405       DSAVarData DVarTemp = hasInnermostDSA(
1406           D,
1407           [](OpenMPClauseKind C) {
1408             return C == OMPC_firstprivate || C == OMPC_shared;
1409           },
1410           MatchesAlways, FromParent);
1411       if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
1412         return DVarTemp;
1413 
1414       DVar.CKind = OMPC_shared;
1415       return DVar;
1416     }
1417   }
1418 
1419   // Explicitly specified attributes and local variables with predetermined
1420   // attributes.
1421   const_iterator I = begin();
1422   const_iterator EndI = end();
1423   if (FromParent && I != EndI)
1424     ++I;
1425   auto It = I->SharingMap.find(D);
1426   if (It != I->SharingMap.end()) {
1427     const DSAInfo &Data = It->getSecond();
1428     DVar.RefExpr = Data.RefExpr.getPointer();
1429     DVar.PrivateCopy = Data.PrivateCopy;
1430     DVar.CKind = Data.Attributes;
1431     DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1432     DVar.DKind = I->Directive;
1433   }
1434 
1435   return DVar;
1436 }
1437 
1438 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1439                                                         bool FromParent) const {
1440   if (isStackEmpty()) {
1441     const_iterator I;
1442     return getDSA(I, D);
1443   }
1444   D = getCanonicalDecl(D);
1445   const_iterator StartI = begin();
1446   const_iterator EndI = end();
1447   if (FromParent && StartI != EndI)
1448     ++StartI;
1449   return getDSA(StartI, D);
1450 }
1451 
1452 const DSAStackTy::DSAVarData
1453 DSAStackTy::hasDSA(ValueDecl *D,
1454                    const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1455                    const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1456                    bool FromParent) const {
1457   if (isStackEmpty())
1458     return {};
1459   D = getCanonicalDecl(D);
1460   const_iterator I = begin();
1461   const_iterator EndI = end();
1462   if (FromParent && I != EndI)
1463     ++I;
1464   for (; I != EndI; ++I) {
1465     if (!DPred(I->Directive) &&
1466         !isImplicitOrExplicitTaskingRegion(I->Directive))
1467       continue;
1468     const_iterator NewI = I;
1469     DSAVarData DVar = getDSA(NewI, D);
1470     if (I == NewI && CPred(DVar.CKind))
1471       return DVar;
1472   }
1473   return {};
1474 }
1475 
1476 const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
1477     ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1478     const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1479     bool FromParent) const {
1480   if (isStackEmpty())
1481     return {};
1482   D = getCanonicalDecl(D);
1483   const_iterator StartI = begin();
1484   const_iterator EndI = end();
1485   if (FromParent && StartI != EndI)
1486     ++StartI;
1487   if (StartI == EndI || !DPred(StartI->Directive))
1488     return {};
1489   const_iterator NewI = StartI;
1490   DSAVarData DVar = getDSA(NewI, D);
1491   return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData();
1492 }
1493 
1494 bool DSAStackTy::hasExplicitDSA(
1495     const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1496     unsigned Level, bool NotLastprivate) const {
1497   if (getStackSize() <= Level)
1498     return false;
1499   D = getCanonicalDecl(D);
1500   const SharingMapTy &StackElem = getStackElemAtLevel(Level);
1501   auto I = StackElem.SharingMap.find(D);
1502   if (I != StackElem.SharingMap.end() &&
1503       I->getSecond().RefExpr.getPointer() &&
1504       CPred(I->getSecond().Attributes) &&
1505       (!NotLastprivate || !I->getSecond().RefExpr.getInt()))
1506     return true;
1507   // Check predetermined rules for the loop control variables.
1508   auto LI = StackElem.LCVMap.find(D);
1509   if (LI != StackElem.LCVMap.end())
1510     return CPred(OMPC_private);
1511   return false;
1512 }
1513 
1514 bool DSAStackTy::hasExplicitDirective(
1515     const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1516     unsigned Level) const {
1517   if (getStackSize() <= Level)
1518     return false;
1519   const SharingMapTy &StackElem = getStackElemAtLevel(Level);
1520   return DPred(StackElem.Directive);
1521 }
1522 
1523 bool DSAStackTy::hasDirective(
1524     const llvm::function_ref<bool(OpenMPDirectiveKind,
1525                                   const DeclarationNameInfo &, SourceLocation)>
1526         DPred,
1527     bool FromParent) const {
1528   // We look only in the enclosing region.
1529   size_t Skip = FromParent ? 2 : 1;
1530   for (const_iterator I = begin() + std::min(Skip, getStackSize()), E = end();
1531        I != E; ++I) {
1532     if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1533       return true;
1534   }
1535   return false;
1536 }
1537 
1538 void Sema::InitDataSharingAttributesStack() {
1539   VarDataSharingAttributesStack = new DSAStackTy(*this);
1540 }
1541 
1542 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1543 
1544 void Sema::pushOpenMPFunctionRegion() {
1545   DSAStack->pushFunction();
1546 }
1547 
1548 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1549   DSAStack->popFunction(OldFSI);
1550 }
1551 
1552 static bool isOpenMPDeviceDelayedContext(Sema &S) {
1553   assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice &&
1554          "Expected OpenMP device compilation.");
1555   return !S.isInOpenMPTargetExecutionDirective() &&
1556          !S.isInOpenMPDeclareTargetContext();
1557 }
1558 
1559 namespace {
1560 /// Status of the function emission on the host/device.
1561 enum class FunctionEmissionStatus {
1562   Emitted,
1563   Discarded,
1564   Unknown,
1565 };
1566 } // anonymous namespace
1567 
1568 /// Do we know that we will eventually codegen the given function?
1569 static FunctionEmissionStatus isKnownDeviceEmitted(Sema &S, FunctionDecl *FD) {
1570   assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice &&
1571          "Expected OpenMP device compilation.");
1572   // Templates are emitted when they're instantiated.
1573   if (FD->isDependentContext())
1574     return FunctionEmissionStatus::Discarded;
1575 
1576   Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
1577       OMPDeclareTargetDeclAttr::getDeviceType(FD->getCanonicalDecl());
1578   if (DevTy.hasValue())
1579     return (*DevTy == OMPDeclareTargetDeclAttr::DT_Host)
1580                ? FunctionEmissionStatus::Discarded
1581                : FunctionEmissionStatus::Emitted;
1582 
1583   // Otherwise, the function is known-emitted if it's in our set of
1584   // known-emitted functions.
1585   return (S.DeviceKnownEmittedFns.count(FD) > 0)
1586              ? FunctionEmissionStatus::Emitted
1587              : FunctionEmissionStatus::Unknown;
1588 }
1589 
1590 Sema::DeviceDiagBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc,
1591                                                      unsigned DiagID) {
1592   assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice &&
1593          "Expected OpenMP device compilation.");
1594   FunctionEmissionStatus FES =
1595       isKnownDeviceEmitted(*this, getCurFunctionDecl());
1596   DeviceDiagBuilder::Kind Kind = DeviceDiagBuilder::K_Nop;
1597   switch (FES) {
1598   case FunctionEmissionStatus::Emitted:
1599     Kind = DeviceDiagBuilder::K_Immediate;
1600     break;
1601   case FunctionEmissionStatus::Unknown:
1602     Kind = isOpenMPDeviceDelayedContext(*this) ? DeviceDiagBuilder::K_Deferred
1603                                                : DeviceDiagBuilder::K_Immediate;
1604     break;
1605   case FunctionEmissionStatus::Discarded:
1606     Kind = DeviceDiagBuilder::K_Nop;
1607     break;
1608   }
1609 
1610   return DeviceDiagBuilder(Kind, Loc, DiagID, getCurFunctionDecl(), *this);
1611 }
1612 
1613 /// Do we know that we will eventually codegen the given function?
1614 static FunctionEmissionStatus isKnownHostEmitted(Sema &S, FunctionDecl *FD) {
1615   assert(S.LangOpts.OpenMP && !S.LangOpts.OpenMPIsDevice &&
1616          "Expected OpenMP host compilation.");
1617   // In OpenMP 4.5 all the functions are host functions.
1618   if (S.LangOpts.OpenMP <= 45)
1619     return FunctionEmissionStatus::Emitted;
1620 
1621   Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
1622       OMPDeclareTargetDeclAttr::getDeviceType(FD->getCanonicalDecl());
1623   if (DevTy.hasValue())
1624     return (*DevTy == OMPDeclareTargetDeclAttr::DT_NoHost)
1625                ? FunctionEmissionStatus::Discarded
1626                : FunctionEmissionStatus::Emitted;
1627 
1628   // Otherwise, the function is known-emitted if it's in our set of
1629   // known-emitted functions.
1630   return (S.DeviceKnownEmittedFns.count(FD) > 0)
1631              ? FunctionEmissionStatus::Emitted
1632              : FunctionEmissionStatus::Unknown;
1633 }
1634 
1635 Sema::DeviceDiagBuilder Sema::diagIfOpenMPHostCode(SourceLocation Loc,
1636                                                    unsigned DiagID) {
1637   assert(LangOpts.OpenMP && !LangOpts.OpenMPIsDevice &&
1638          "Expected OpenMP host compilation.");
1639   FunctionEmissionStatus FES =
1640       isKnownHostEmitted(*this, getCurFunctionDecl());
1641   DeviceDiagBuilder::Kind Kind = DeviceDiagBuilder::K_Nop;
1642   switch (FES) {
1643   case FunctionEmissionStatus::Emitted:
1644     Kind = DeviceDiagBuilder::K_Immediate;
1645     break;
1646   case FunctionEmissionStatus::Unknown:
1647     Kind = DeviceDiagBuilder::K_Deferred;
1648     break;
1649   case FunctionEmissionStatus::Discarded:
1650     Kind = DeviceDiagBuilder::K_Nop;
1651     break;
1652   }
1653 
1654   return DeviceDiagBuilder(Kind, Loc, DiagID, getCurFunctionDecl(), *this);
1655 }
1656 
1657 void Sema::checkOpenMPDeviceFunction(SourceLocation Loc, FunctionDecl *Callee,
1658                                      bool CheckForDelayedContext) {
1659   assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice &&
1660          "Expected OpenMP device compilation.");
1661   assert(Callee && "Callee may not be null.");
1662   Callee = Callee->getMostRecentDecl();
1663   FunctionDecl *Caller = getCurFunctionDecl();
1664 
1665   // host only function are not available on the device.
1666   if (Caller &&
1667       (isKnownDeviceEmitted(*this, Caller) == FunctionEmissionStatus::Emitted ||
1668        (!isOpenMPDeviceDelayedContext(*this) &&
1669         isKnownDeviceEmitted(*this, Caller) ==
1670             FunctionEmissionStatus::Unknown)) &&
1671       isKnownDeviceEmitted(*this, Callee) ==
1672           FunctionEmissionStatus::Discarded) {
1673     StringRef HostDevTy =
1674         getOpenMPSimpleClauseTypeName(OMPC_device_type, OMPC_DEVICE_TYPE_host);
1675     Diag(Loc, diag::err_omp_wrong_device_function_call) << HostDevTy << 0;
1676     Diag(Callee->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(),
1677          diag::note_omp_marked_device_type_here)
1678         << HostDevTy;
1679     return;
1680   }
1681   // If the caller is known-emitted, mark the callee as known-emitted.
1682   // Otherwise, mark the call in our call graph so we can traverse it later.
1683   if ((CheckForDelayedContext && !isOpenMPDeviceDelayedContext(*this)) ||
1684       (!Caller && !CheckForDelayedContext) ||
1685       (Caller &&
1686        isKnownDeviceEmitted(*this, Caller) == FunctionEmissionStatus::Emitted))
1687     markKnownEmitted(*this, Caller, Callee, Loc,
1688                      [CheckForDelayedContext](Sema &S, FunctionDecl *FD) {
1689                        return CheckForDelayedContext &&
1690                               isKnownDeviceEmitted(S, FD) ==
1691                                   FunctionEmissionStatus::Emitted;
1692                      });
1693   else if (Caller)
1694     DeviceCallGraph[Caller].insert({Callee, Loc});
1695 }
1696 
1697 void Sema::checkOpenMPHostFunction(SourceLocation Loc, FunctionDecl *Callee,
1698                                    bool CheckCaller) {
1699   assert(LangOpts.OpenMP && !LangOpts.OpenMPIsDevice &&
1700          "Expected OpenMP host compilation.");
1701   assert(Callee && "Callee may not be null.");
1702   Callee = Callee->getMostRecentDecl();
1703   FunctionDecl *Caller = getCurFunctionDecl();
1704 
1705   // device only function are not available on the host.
1706   if (Caller &&
1707       isKnownHostEmitted(*this, Caller) == FunctionEmissionStatus::Emitted &&
1708       isKnownHostEmitted(*this, Callee) == FunctionEmissionStatus::Discarded) {
1709     StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName(
1710         OMPC_device_type, OMPC_DEVICE_TYPE_nohost);
1711     Diag(Loc, diag::err_omp_wrong_device_function_call) << NoHostDevTy << 1;
1712     Diag(Callee->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(),
1713          diag::note_omp_marked_device_type_here)
1714         << NoHostDevTy;
1715     return;
1716   }
1717   // If the caller is known-emitted, mark the callee as known-emitted.
1718   // Otherwise, mark the call in our call graph so we can traverse it later.
1719   if ((!CheckCaller && !Caller) ||
1720       (Caller &&
1721        isKnownHostEmitted(*this, Caller) == FunctionEmissionStatus::Emitted))
1722     markKnownEmitted(
1723         *this, Caller, Callee, Loc, [CheckCaller](Sema &S, FunctionDecl *FD) {
1724           return CheckCaller &&
1725                  isKnownHostEmitted(S, FD) == FunctionEmissionStatus::Emitted;
1726         });
1727   else if (Caller)
1728     DeviceCallGraph[Caller].insert({Callee, Loc});
1729 }
1730 
1731 void Sema::checkOpenMPDeviceExpr(const Expr *E) {
1732   assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice &&
1733          "OpenMP device compilation mode is expected.");
1734   QualType Ty = E->getType();
1735   if ((Ty->isFloat16Type() && !Context.getTargetInfo().hasFloat16Type()) ||
1736       ((Ty->isFloat128Type() ||
1737         (Ty->isRealFloatingType() && Context.getTypeSize(Ty) == 128)) &&
1738        !Context.getTargetInfo().hasFloat128Type()) ||
1739       (Ty->isIntegerType() && Context.getTypeSize(Ty) == 128 &&
1740        !Context.getTargetInfo().hasInt128Type()))
1741     targetDiag(E->getExprLoc(), diag::err_omp_unsupported_type)
1742         << static_cast<unsigned>(Context.getTypeSize(Ty)) << Ty
1743         << Context.getTargetInfo().getTriple().str() << E->getSourceRange();
1744 }
1745 
1746 bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level,
1747                                  unsigned OpenMPCaptureLevel) const {
1748   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1749 
1750   ASTContext &Ctx = getASTContext();
1751   bool IsByRef = true;
1752 
1753   // Find the directive that is associated with the provided scope.
1754   D = cast<ValueDecl>(D->getCanonicalDecl());
1755   QualType Ty = D->getType();
1756 
1757   bool IsVariableUsedInMapClause = false;
1758   if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
1759     // This table summarizes how a given variable should be passed to the device
1760     // given its type and the clauses where it appears. This table is based on
1761     // the description in OpenMP 4.5 [2.10.4, target Construct] and
1762     // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
1763     //
1764     // =========================================================================
1765     // | type |  defaultmap   | pvt | first | is_device_ptr |    map   | res.  |
1766     // |      |(tofrom:scalar)|     |  pvt  |               |          |       |
1767     // =========================================================================
1768     // | scl  |               |     |       |       -       |          | bycopy|
1769     // | scl  |               |  -  |   x   |       -       |     -    | bycopy|
1770     // | scl  |               |  x  |   -   |       -       |     -    | null  |
1771     // | scl  |       x       |     |       |       -       |          | byref |
1772     // | scl  |       x       |  -  |   x   |       -       |     -    | bycopy|
1773     // | scl  |       x       |  x  |   -   |       -       |     -    | null  |
1774     // | scl  |               |  -  |   -   |       -       |     x    | byref |
1775     // | scl  |       x       |  -  |   -   |       -       |     x    | byref |
1776     //
1777     // | agg  |      n.a.     |     |       |       -       |          | byref |
1778     // | agg  |      n.a.     |  -  |   x   |       -       |     -    | byref |
1779     // | agg  |      n.a.     |  x  |   -   |       -       |     -    | null  |
1780     // | agg  |      n.a.     |  -  |   -   |       -       |     x    | byref |
1781     // | agg  |      n.a.     |  -  |   -   |       -       |    x[]   | byref |
1782     //
1783     // | ptr  |      n.a.     |     |       |       -       |          | bycopy|
1784     // | ptr  |      n.a.     |  -  |   x   |       -       |     -    | bycopy|
1785     // | ptr  |      n.a.     |  x  |   -   |       -       |     -    | null  |
1786     // | ptr  |      n.a.     |  -  |   -   |       -       |     x    | byref |
1787     // | ptr  |      n.a.     |  -  |   -   |       -       |    x[]   | bycopy|
1788     // | ptr  |      n.a.     |  -  |   -   |       x       |          | bycopy|
1789     // | ptr  |      n.a.     |  -  |   -   |       x       |     x    | bycopy|
1790     // | ptr  |      n.a.     |  -  |   -   |       x       |    x[]   | bycopy|
1791     // =========================================================================
1792     // Legend:
1793     //  scl - scalar
1794     //  ptr - pointer
1795     //  agg - aggregate
1796     //  x - applies
1797     //  - - invalid in this combination
1798     //  [] - mapped with an array section
1799     //  byref - should be mapped by reference
1800     //  byval - should be mapped by value
1801     //  null - initialize a local variable to null on the device
1802     //
1803     // Observations:
1804     //  - All scalar declarations that show up in a map clause have to be passed
1805     //    by reference, because they may have been mapped in the enclosing data
1806     //    environment.
1807     //  - If the scalar value does not fit the size of uintptr, it has to be
1808     //    passed by reference, regardless the result in the table above.
1809     //  - For pointers mapped by value that have either an implicit map or an
1810     //    array section, the runtime library may pass the NULL value to the
1811     //    device instead of the value passed to it by the compiler.
1812 
1813     if (Ty->isReferenceType())
1814       Ty = Ty->castAs<ReferenceType>()->getPointeeType();
1815 
1816     // Locate map clauses and see if the variable being captured is referred to
1817     // in any of those clauses. Here we only care about variables, not fields,
1818     // because fields are part of aggregates.
1819     bool IsVariableAssociatedWithSection = false;
1820 
1821     DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1822         D, Level,
1823         [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D](
1824             OMPClauseMappableExprCommon::MappableExprComponentListRef
1825                 MapExprComponents,
1826             OpenMPClauseKind WhereFoundClauseKind) {
1827           // Only the map clause information influences how a variable is
1828           // captured. E.g. is_device_ptr does not require changing the default
1829           // behavior.
1830           if (WhereFoundClauseKind != OMPC_map)
1831             return false;
1832 
1833           auto EI = MapExprComponents.rbegin();
1834           auto EE = MapExprComponents.rend();
1835 
1836           assert(EI != EE && "Invalid map expression!");
1837 
1838           if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
1839             IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
1840 
1841           ++EI;
1842           if (EI == EE)
1843             return false;
1844 
1845           if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
1846               isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
1847               isa<MemberExpr>(EI->getAssociatedExpression())) {
1848             IsVariableAssociatedWithSection = true;
1849             // There is nothing more we need to know about this variable.
1850             return true;
1851           }
1852 
1853           // Keep looking for more map info.
1854           return false;
1855         });
1856 
1857     if (IsVariableUsedInMapClause) {
1858       // If variable is identified in a map clause it is always captured by
1859       // reference except if it is a pointer that is dereferenced somehow.
1860       IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
1861     } else {
1862       // By default, all the data that has a scalar type is mapped by copy
1863       // (except for reduction variables).
1864       IsByRef =
1865           (DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
1866            !Ty->isAnyPointerType()) ||
1867           !Ty->isScalarType() ||
1868           DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar ||
1869           DSAStack->hasExplicitDSA(
1870               D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level);
1871     }
1872   }
1873 
1874   if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
1875     IsByRef =
1876         ((IsVariableUsedInMapClause &&
1877           DSAStack->getCaptureRegion(Level, OpenMPCaptureLevel) ==
1878               OMPD_target) ||
1879          !DSAStack->hasExplicitDSA(
1880              D,
1881              [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
1882              Level, /*NotLastprivate=*/true)) &&
1883         // If the variable is artificial and must be captured by value - try to
1884         // capture by value.
1885         !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
1886           !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue());
1887   }
1888 
1889   // When passing data by copy, we need to make sure it fits the uintptr size
1890   // and alignment, because the runtime library only deals with uintptr types.
1891   // If it does not fit the uintptr size, we need to pass the data by reference
1892   // instead.
1893   if (!IsByRef &&
1894       (Ctx.getTypeSizeInChars(Ty) >
1895            Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
1896        Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
1897     IsByRef = true;
1898   }
1899 
1900   return IsByRef;
1901 }
1902 
1903 unsigned Sema::getOpenMPNestingLevel() const {
1904   assert(getLangOpts().OpenMP);
1905   return DSAStack->getNestingLevel();
1906 }
1907 
1908 bool Sema::isInOpenMPTargetExecutionDirective() const {
1909   return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
1910           !DSAStack->isClauseParsingMode()) ||
1911          DSAStack->hasDirective(
1912              [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
1913                 SourceLocation) -> bool {
1914                return isOpenMPTargetExecutionDirective(K);
1915              },
1916              false);
1917 }
1918 
1919 VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo,
1920                                     unsigned StopAt) {
1921   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1922   D = getCanonicalDecl(D);
1923 
1924   // If we want to determine whether the variable should be captured from the
1925   // perspective of the current capturing scope, and we've already left all the
1926   // capturing scopes of the top directive on the stack, check from the
1927   // perspective of its parent directive (if any) instead.
1928   DSAStackTy::ParentDirectiveScope InParentDirectiveRAII(
1929       *DSAStack, CheckScopeInfo && DSAStack->isBodyComplete());
1930 
1931   // If we are attempting to capture a global variable in a directive with
1932   // 'target' we return true so that this global is also mapped to the device.
1933   //
1934   auto *VD = dyn_cast<VarDecl>(D);
1935   if (VD && !VD->hasLocalStorage() &&
1936       (getCurCapturedRegion() || getCurBlock() || getCurLambda())) {
1937     if (isInOpenMPDeclareTargetContext()) {
1938       // Try to mark variable as declare target if it is used in capturing
1939       // regions.
1940       if (LangOpts.OpenMP <= 45 &&
1941           !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
1942         checkDeclIsAllowedInOpenMPTarget(nullptr, VD);
1943       return nullptr;
1944     } else if (isInOpenMPTargetExecutionDirective()) {
1945       // If the declaration is enclosed in a 'declare target' directive,
1946       // then it should not be captured.
1947       //
1948       if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
1949         return nullptr;
1950       return VD;
1951     }
1952   }
1953 
1954   if (CheckScopeInfo) {
1955     bool OpenMPFound = false;
1956     for (unsigned I = StopAt + 1; I > 0; --I) {
1957       FunctionScopeInfo *FSI = FunctionScopes[I - 1];
1958       if(!isa<CapturingScopeInfo>(FSI))
1959         return nullptr;
1960       if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI))
1961         if (RSI->CapRegionKind == CR_OpenMP) {
1962           OpenMPFound = true;
1963           break;
1964         }
1965     }
1966     if (!OpenMPFound)
1967       return nullptr;
1968   }
1969 
1970   if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1971       (!DSAStack->isClauseParsingMode() ||
1972        DSAStack->getParentDirective() != OMPD_unknown)) {
1973     auto &&Info = DSAStack->isLoopControlVariable(D);
1974     if (Info.first ||
1975         (VD && VD->hasLocalStorage() &&
1976          isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) ||
1977         (VD && DSAStack->isForceVarCapturing()))
1978       return VD ? VD : Info.second;
1979     DSAStackTy::DSAVarData DVarPrivate =
1980         DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
1981     if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
1982       return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
1983     // Threadprivate variables must not be captured.
1984     if (isOpenMPThreadPrivate(DVarPrivate.CKind))
1985       return nullptr;
1986     // The variable is not private or it is the variable in the directive with
1987     // default(none) clause and not used in any clause.
1988     DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate,
1989                                    [](OpenMPDirectiveKind) { return true; },
1990                                    DSAStack->isClauseParsingMode());
1991     if (DVarPrivate.CKind != OMPC_unknown ||
1992         (VD && DSAStack->getDefaultDSA() == DSA_none))
1993       return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
1994   }
1995   return nullptr;
1996 }
1997 
1998 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
1999                                         unsigned Level) const {
2000   SmallVector<OpenMPDirectiveKind, 4> Regions;
2001   getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
2002   FunctionScopesIndex -= Regions.size();
2003 }
2004 
2005 void Sema::startOpenMPLoop() {
2006   assert(LangOpts.OpenMP && "OpenMP must be enabled.");
2007   if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()))
2008     DSAStack->loopInit();
2009 }
2010 
2011 bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const {
2012   assert(LangOpts.OpenMP && "OpenMP is not allowed");
2013   if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2014     if (DSAStack->getAssociatedLoops() > 0 &&
2015         !DSAStack->isLoopStarted()) {
2016       DSAStack->resetPossibleLoopCounter(D);
2017       DSAStack->loopStart();
2018       return true;
2019     }
2020     if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() ||
2021          DSAStack->isLoopControlVariable(D).first) &&
2022         !DSAStack->hasExplicitDSA(
2023             D, [](OpenMPClauseKind K) { return K != OMPC_private; }, Level) &&
2024         !isOpenMPSimdDirective(DSAStack->getCurrentDirective()))
2025       return true;
2026   }
2027   if (const auto *VD = dyn_cast<VarDecl>(D)) {
2028     if (DSAStack->isThreadPrivate(const_cast<VarDecl *>(VD)) &&
2029         DSAStack->isForceVarCapturing() &&
2030         !DSAStack->hasExplicitDSA(
2031             D, [](OpenMPClauseKind K) { return K == OMPC_copyin; }, Level))
2032       return true;
2033   }
2034   return DSAStack->hasExplicitDSA(
2035              D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) ||
2036          (DSAStack->isClauseParsingMode() &&
2037           DSAStack->getClauseParsingMode() == OMPC_private) ||
2038          // Consider taskgroup reduction descriptor variable a private to avoid
2039          // possible capture in the region.
2040          (DSAStack->hasExplicitDirective(
2041               [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; },
2042               Level) &&
2043           DSAStack->isTaskgroupReductionRef(D, Level));
2044 }
2045 
2046 void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
2047                                 unsigned Level) {
2048   assert(LangOpts.OpenMP && "OpenMP is not allowed");
2049   D = getCanonicalDecl(D);
2050   OpenMPClauseKind OMPC = OMPC_unknown;
2051   for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
2052     const unsigned NewLevel = I - 1;
2053     if (DSAStack->hasExplicitDSA(D,
2054                                  [&OMPC](const OpenMPClauseKind K) {
2055                                    if (isOpenMPPrivate(K)) {
2056                                      OMPC = K;
2057                                      return true;
2058                                    }
2059                                    return false;
2060                                  },
2061                                  NewLevel))
2062       break;
2063     if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
2064             D, NewLevel,
2065             [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
2066                OpenMPClauseKind) { return true; })) {
2067       OMPC = OMPC_map;
2068       break;
2069     }
2070     if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
2071                                        NewLevel)) {
2072       OMPC = OMPC_map;
2073       if (D->getType()->isScalarType() &&
2074           DSAStack->getDefaultDMAAtLevel(NewLevel) !=
2075               DefaultMapAttributes::DMA_tofrom_scalar)
2076         OMPC = OMPC_firstprivate;
2077       break;
2078     }
2079   }
2080   if (OMPC != OMPC_unknown)
2081     FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC));
2082 }
2083 
2084 bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D,
2085                                       unsigned Level) const {
2086   assert(LangOpts.OpenMP && "OpenMP is not allowed");
2087   // Return true if the current level is no longer enclosed in a target region.
2088 
2089   const auto *VD = dyn_cast<VarDecl>(D);
2090   return VD && !VD->hasLocalStorage() &&
2091          DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
2092                                         Level);
2093 }
2094 
2095 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
2096 
2097 void Sema::finalizeOpenMPDelayedAnalysis() {
2098   assert(LangOpts.OpenMP && "Expected OpenMP compilation mode.");
2099   // Diagnose implicit declare target functions and their callees.
2100   for (const auto &CallerCallees : DeviceCallGraph) {
2101     Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
2102         OMPDeclareTargetDeclAttr::getDeviceType(
2103             CallerCallees.getFirst()->getMostRecentDecl());
2104     // Ignore host functions during device analyzis.
2105     if (LangOpts.OpenMPIsDevice && DevTy &&
2106         *DevTy == OMPDeclareTargetDeclAttr::DT_Host)
2107       continue;
2108     // Ignore nohost functions during host analyzis.
2109     if (!LangOpts.OpenMPIsDevice && DevTy &&
2110         *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost)
2111       continue;
2112     for (const std::pair<CanonicalDeclPtr<FunctionDecl>, SourceLocation>
2113              &Callee : CallerCallees.getSecond()) {
2114       const FunctionDecl *FD = Callee.first->getMostRecentDecl();
2115       Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
2116           OMPDeclareTargetDeclAttr::getDeviceType(FD);
2117       if (LangOpts.OpenMPIsDevice && DevTy &&
2118           *DevTy == OMPDeclareTargetDeclAttr::DT_Host) {
2119         // Diagnose host function called during device codegen.
2120         StringRef HostDevTy = getOpenMPSimpleClauseTypeName(
2121             OMPC_device_type, OMPC_DEVICE_TYPE_host);
2122         Diag(Callee.second, diag::err_omp_wrong_device_function_call)
2123             << HostDevTy << 0;
2124         Diag(FD->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(),
2125              diag::note_omp_marked_device_type_here)
2126             << HostDevTy;
2127         continue;
2128       }
2129       if (!LangOpts.OpenMPIsDevice && DevTy &&
2130           *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) {
2131         // Diagnose nohost function called during host codegen.
2132         StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName(
2133             OMPC_device_type, OMPC_DEVICE_TYPE_nohost);
2134         Diag(Callee.second, diag::err_omp_wrong_device_function_call)
2135             << NoHostDevTy << 1;
2136         Diag(FD->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(),
2137              diag::note_omp_marked_device_type_here)
2138             << NoHostDevTy;
2139         continue;
2140       }
2141     }
2142   }
2143 }
2144 
2145 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
2146                                const DeclarationNameInfo &DirName,
2147                                Scope *CurScope, SourceLocation Loc) {
2148   DSAStack->push(DKind, DirName, CurScope, Loc);
2149   PushExpressionEvaluationContext(
2150       ExpressionEvaluationContext::PotentiallyEvaluated);
2151 }
2152 
2153 void Sema::StartOpenMPClause(OpenMPClauseKind K) {
2154   DSAStack->setClauseParsingMode(K);
2155 }
2156 
2157 void Sema::EndOpenMPClause() {
2158   DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
2159 }
2160 
2161 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
2162                                  ArrayRef<OMPClause *> Clauses);
2163 
2164 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
2165   // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
2166   //  A variable of class type (or array thereof) that appears in a lastprivate
2167   //  clause requires an accessible, unambiguous default constructor for the
2168   //  class type, unless the list item is also specified in a firstprivate
2169   //  clause.
2170   if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
2171     for (OMPClause *C : D->clauses()) {
2172       if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
2173         SmallVector<Expr *, 8> PrivateCopies;
2174         for (Expr *DE : Clause->varlists()) {
2175           if (DE->isValueDependent() || DE->isTypeDependent()) {
2176             PrivateCopies.push_back(nullptr);
2177             continue;
2178           }
2179           auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
2180           auto *VD = cast<VarDecl>(DRE->getDecl());
2181           QualType Type = VD->getType().getNonReferenceType();
2182           const DSAStackTy::DSAVarData DVar =
2183               DSAStack->getTopDSA(VD, /*FromParent=*/false);
2184           if (DVar.CKind == OMPC_lastprivate) {
2185             // Generate helper private variable and initialize it with the
2186             // default value. The address of the original variable is replaced
2187             // by the address of the new private variable in CodeGen. This new
2188             // variable is not added to IdResolver, so the code in the OpenMP
2189             // region uses original variable for proper diagnostics.
2190             VarDecl *VDPrivate = buildVarDecl(
2191                 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
2192                 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE);
2193             ActOnUninitializedDecl(VDPrivate);
2194             if (VDPrivate->isInvalidDecl()) {
2195               PrivateCopies.push_back(nullptr);
2196               continue;
2197             }
2198             PrivateCopies.push_back(buildDeclRefExpr(
2199                 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
2200           } else {
2201             // The variable is also a firstprivate, so initialization sequence
2202             // for private copy is generated already.
2203             PrivateCopies.push_back(nullptr);
2204           }
2205         }
2206         Clause->setPrivateCopies(PrivateCopies);
2207       }
2208     }
2209     // Check allocate clauses.
2210     if (!CurContext->isDependentContext())
2211       checkAllocateClauses(*this, DSAStack, D->clauses());
2212   }
2213 
2214   DSAStack->pop();
2215   DiscardCleanupsInEvaluationContext();
2216   PopExpressionEvaluationContext();
2217 }
2218 
2219 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
2220                                      Expr *NumIterations, Sema &SemaRef,
2221                                      Scope *S, DSAStackTy *Stack);
2222 
2223 namespace {
2224 
2225 class VarDeclFilterCCC final : public CorrectionCandidateCallback {
2226 private:
2227   Sema &SemaRef;
2228 
2229 public:
2230   explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
2231   bool ValidateCandidate(const TypoCorrection &Candidate) override {
2232     NamedDecl *ND = Candidate.getCorrectionDecl();
2233     if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
2234       return VD->hasGlobalStorage() &&
2235              SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
2236                                    SemaRef.getCurScope());
2237     }
2238     return false;
2239   }
2240 
2241   std::unique_ptr<CorrectionCandidateCallback> clone() override {
2242     return std::make_unique<VarDeclFilterCCC>(*this);
2243   }
2244 
2245 };
2246 
2247 class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
2248 private:
2249   Sema &SemaRef;
2250 
2251 public:
2252   explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
2253   bool ValidateCandidate(const TypoCorrection &Candidate) override {
2254     NamedDecl *ND = Candidate.getCorrectionDecl();
2255     if (ND && ((isa<VarDecl>(ND) && ND->getKind() == Decl::Var) ||
2256                isa<FunctionDecl>(ND))) {
2257       return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
2258                                    SemaRef.getCurScope());
2259     }
2260     return false;
2261   }
2262 
2263   std::unique_ptr<CorrectionCandidateCallback> clone() override {
2264     return std::make_unique<VarOrFuncDeclFilterCCC>(*this);
2265   }
2266 };
2267 
2268 } // namespace
2269 
2270 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
2271                                          CXXScopeSpec &ScopeSpec,
2272                                          const DeclarationNameInfo &Id,
2273                                          OpenMPDirectiveKind Kind) {
2274   LookupResult Lookup(*this, Id, LookupOrdinaryName);
2275   LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
2276 
2277   if (Lookup.isAmbiguous())
2278     return ExprError();
2279 
2280   VarDecl *VD;
2281   if (!Lookup.isSingleResult()) {
2282     VarDeclFilterCCC CCC(*this);
2283     if (TypoCorrection Corrected =
2284             CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC,
2285                         CTK_ErrorRecovery)) {
2286       diagnoseTypo(Corrected,
2287                    PDiag(Lookup.empty()
2288                              ? diag::err_undeclared_var_use_suggest
2289                              : diag::err_omp_expected_var_arg_suggest)
2290                        << Id.getName());
2291       VD = Corrected.getCorrectionDeclAs<VarDecl>();
2292     } else {
2293       Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
2294                                        : diag::err_omp_expected_var_arg)
2295           << Id.getName();
2296       return ExprError();
2297     }
2298   } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
2299     Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
2300     Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
2301     return ExprError();
2302   }
2303   Lookup.suppressDiagnostics();
2304 
2305   // OpenMP [2.9.2, Syntax, C/C++]
2306   //   Variables must be file-scope, namespace-scope, or static block-scope.
2307   if (Kind == OMPD_threadprivate && !VD->hasGlobalStorage()) {
2308     Diag(Id.getLoc(), diag::err_omp_global_var_arg)
2309         << getOpenMPDirectiveName(Kind) << !VD->isStaticLocal();
2310     bool IsDecl =
2311         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2312     Diag(VD->getLocation(),
2313          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2314         << VD;
2315     return ExprError();
2316   }
2317 
2318   VarDecl *CanonicalVD = VD->getCanonicalDecl();
2319   NamedDecl *ND = CanonicalVD;
2320   // OpenMP [2.9.2, Restrictions, C/C++, p.2]
2321   //   A threadprivate directive for file-scope variables must appear outside
2322   //   any definition or declaration.
2323   if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
2324       !getCurLexicalContext()->isTranslationUnit()) {
2325     Diag(Id.getLoc(), diag::err_omp_var_scope)
2326         << getOpenMPDirectiveName(Kind) << VD;
2327     bool IsDecl =
2328         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2329     Diag(VD->getLocation(),
2330          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2331         << VD;
2332     return ExprError();
2333   }
2334   // OpenMP [2.9.2, Restrictions, C/C++, p.3]
2335   //   A threadprivate directive for static class member variables must appear
2336   //   in the class definition, in the same scope in which the member
2337   //   variables are declared.
2338   if (CanonicalVD->isStaticDataMember() &&
2339       !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
2340     Diag(Id.getLoc(), diag::err_omp_var_scope)
2341         << getOpenMPDirectiveName(Kind) << VD;
2342     bool IsDecl =
2343         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2344     Diag(VD->getLocation(),
2345          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2346         << VD;
2347     return ExprError();
2348   }
2349   // OpenMP [2.9.2, Restrictions, C/C++, p.4]
2350   //   A threadprivate directive for namespace-scope variables must appear
2351   //   outside any definition or declaration other than the namespace
2352   //   definition itself.
2353   if (CanonicalVD->getDeclContext()->isNamespace() &&
2354       (!getCurLexicalContext()->isFileContext() ||
2355        !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
2356     Diag(Id.getLoc(), diag::err_omp_var_scope)
2357         << getOpenMPDirectiveName(Kind) << VD;
2358     bool IsDecl =
2359         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2360     Diag(VD->getLocation(),
2361          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2362         << VD;
2363     return ExprError();
2364   }
2365   // OpenMP [2.9.2, Restrictions, C/C++, p.6]
2366   //   A threadprivate directive for static block-scope variables must appear
2367   //   in the scope of the variable and not in a nested scope.
2368   if (CanonicalVD->isLocalVarDecl() && CurScope &&
2369       !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
2370     Diag(Id.getLoc(), diag::err_omp_var_scope)
2371         << getOpenMPDirectiveName(Kind) << VD;
2372     bool IsDecl =
2373         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2374     Diag(VD->getLocation(),
2375          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2376         << VD;
2377     return ExprError();
2378   }
2379 
2380   // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
2381   //   A threadprivate directive must lexically precede all references to any
2382   //   of the variables in its list.
2383   if (Kind == OMPD_threadprivate && VD->isUsed() &&
2384       !DSAStack->isThreadPrivate(VD)) {
2385     Diag(Id.getLoc(), diag::err_omp_var_used)
2386         << getOpenMPDirectiveName(Kind) << VD;
2387     return ExprError();
2388   }
2389 
2390   QualType ExprType = VD->getType().getNonReferenceType();
2391   return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
2392                              SourceLocation(), VD,
2393                              /*RefersToEnclosingVariableOrCapture=*/false,
2394                              Id.getLoc(), ExprType, VK_LValue);
2395 }
2396 
2397 Sema::DeclGroupPtrTy
2398 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
2399                                         ArrayRef<Expr *> VarList) {
2400   if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
2401     CurContext->addDecl(D);
2402     return DeclGroupPtrTy::make(DeclGroupRef(D));
2403   }
2404   return nullptr;
2405 }
2406 
2407 namespace {
2408 class LocalVarRefChecker final
2409     : public ConstStmtVisitor<LocalVarRefChecker, bool> {
2410   Sema &SemaRef;
2411 
2412 public:
2413   bool VisitDeclRefExpr(const DeclRefExpr *E) {
2414     if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
2415       if (VD->hasLocalStorage()) {
2416         SemaRef.Diag(E->getBeginLoc(),
2417                      diag::err_omp_local_var_in_threadprivate_init)
2418             << E->getSourceRange();
2419         SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
2420             << VD << VD->getSourceRange();
2421         return true;
2422       }
2423     }
2424     return false;
2425   }
2426   bool VisitStmt(const Stmt *S) {
2427     for (const Stmt *Child : S->children()) {
2428       if (Child && Visit(Child))
2429         return true;
2430     }
2431     return false;
2432   }
2433   explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
2434 };
2435 } // namespace
2436 
2437 OMPThreadPrivateDecl *
2438 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
2439   SmallVector<Expr *, 8> Vars;
2440   for (Expr *RefExpr : VarList) {
2441     auto *DE = cast<DeclRefExpr>(RefExpr);
2442     auto *VD = cast<VarDecl>(DE->getDecl());
2443     SourceLocation ILoc = DE->getExprLoc();
2444 
2445     // Mark variable as used.
2446     VD->setReferenced();
2447     VD->markUsed(Context);
2448 
2449     QualType QType = VD->getType();
2450     if (QType->isDependentType() || QType->isInstantiationDependentType()) {
2451       // It will be analyzed later.
2452       Vars.push_back(DE);
2453       continue;
2454     }
2455 
2456     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2457     //   A threadprivate variable must not have an incomplete type.
2458     if (RequireCompleteType(ILoc, VD->getType(),
2459                             diag::err_omp_threadprivate_incomplete_type)) {
2460       continue;
2461     }
2462 
2463     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2464     //   A threadprivate variable must not have a reference type.
2465     if (VD->getType()->isReferenceType()) {
2466       Diag(ILoc, diag::err_omp_ref_type_arg)
2467           << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
2468       bool IsDecl =
2469           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2470       Diag(VD->getLocation(),
2471            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2472           << VD;
2473       continue;
2474     }
2475 
2476     // Check if this is a TLS variable. If TLS is not being supported, produce
2477     // the corresponding diagnostic.
2478     if ((VD->getTLSKind() != VarDecl::TLS_None &&
2479          !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
2480            getLangOpts().OpenMPUseTLS &&
2481            getASTContext().getTargetInfo().isTLSSupported())) ||
2482         (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
2483          !VD->isLocalVarDecl())) {
2484       Diag(ILoc, diag::err_omp_var_thread_local)
2485           << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
2486       bool IsDecl =
2487           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2488       Diag(VD->getLocation(),
2489            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2490           << VD;
2491       continue;
2492     }
2493 
2494     // Check if initial value of threadprivate variable reference variable with
2495     // local storage (it is not supported by runtime).
2496     if (const Expr *Init = VD->getAnyInitializer()) {
2497       LocalVarRefChecker Checker(*this);
2498       if (Checker.Visit(Init))
2499         continue;
2500     }
2501 
2502     Vars.push_back(RefExpr);
2503     DSAStack->addDSA(VD, DE, OMPC_threadprivate);
2504     VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
2505         Context, SourceRange(Loc, Loc)));
2506     if (ASTMutationListener *ML = Context.getASTMutationListener())
2507       ML->DeclarationMarkedOpenMPThreadPrivate(VD);
2508   }
2509   OMPThreadPrivateDecl *D = nullptr;
2510   if (!Vars.empty()) {
2511     D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
2512                                      Vars);
2513     D->setAccess(AS_public);
2514   }
2515   return D;
2516 }
2517 
2518 static OMPAllocateDeclAttr::AllocatorTypeTy
2519 getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) {
2520   if (!Allocator)
2521     return OMPAllocateDeclAttr::OMPDefaultMemAlloc;
2522   if (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
2523       Allocator->isInstantiationDependent() ||
2524       Allocator->containsUnexpandedParameterPack())
2525     return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
2526   auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
2527   const Expr *AE = Allocator->IgnoreParenImpCasts();
2528   for (int I = OMPAllocateDeclAttr::OMPDefaultMemAlloc;
2529        I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
2530     auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
2531     const Expr *DefAllocator = Stack->getAllocator(AllocatorKind);
2532     llvm::FoldingSetNodeID AEId, DAEId;
2533     AE->Profile(AEId, S.getASTContext(), /*Canonical=*/true);
2534     DefAllocator->Profile(DAEId, S.getASTContext(), /*Canonical=*/true);
2535     if (AEId == DAEId) {
2536       AllocatorKindRes = AllocatorKind;
2537       break;
2538     }
2539   }
2540   return AllocatorKindRes;
2541 }
2542 
2543 static bool checkPreviousOMPAllocateAttribute(
2544     Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD,
2545     OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) {
2546   if (!VD->hasAttr<OMPAllocateDeclAttr>())
2547     return false;
2548   const auto *A = VD->getAttr<OMPAllocateDeclAttr>();
2549   Expr *PrevAllocator = A->getAllocator();
2550   OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind =
2551       getAllocatorKind(S, Stack, PrevAllocator);
2552   bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind;
2553   if (AllocatorsMatch &&
2554       AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc &&
2555       Allocator && PrevAllocator) {
2556     const Expr *AE = Allocator->IgnoreParenImpCasts();
2557     const Expr *PAE = PrevAllocator->IgnoreParenImpCasts();
2558     llvm::FoldingSetNodeID AEId, PAEId;
2559     AE->Profile(AEId, S.Context, /*Canonical=*/true);
2560     PAE->Profile(PAEId, S.Context, /*Canonical=*/true);
2561     AllocatorsMatch = AEId == PAEId;
2562   }
2563   if (!AllocatorsMatch) {
2564     SmallString<256> AllocatorBuffer;
2565     llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer);
2566     if (Allocator)
2567       Allocator->printPretty(AllocatorStream, nullptr, S.getPrintingPolicy());
2568     SmallString<256> PrevAllocatorBuffer;
2569     llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer);
2570     if (PrevAllocator)
2571       PrevAllocator->printPretty(PrevAllocatorStream, nullptr,
2572                                  S.getPrintingPolicy());
2573 
2574     SourceLocation AllocatorLoc =
2575         Allocator ? Allocator->getExprLoc() : RefExpr->getExprLoc();
2576     SourceRange AllocatorRange =
2577         Allocator ? Allocator->getSourceRange() : RefExpr->getSourceRange();
2578     SourceLocation PrevAllocatorLoc =
2579         PrevAllocator ? PrevAllocator->getExprLoc() : A->getLocation();
2580     SourceRange PrevAllocatorRange =
2581         PrevAllocator ? PrevAllocator->getSourceRange() : A->getRange();
2582     S.Diag(AllocatorLoc, diag::warn_omp_used_different_allocator)
2583         << (Allocator ? 1 : 0) << AllocatorStream.str()
2584         << (PrevAllocator ? 1 : 0) << PrevAllocatorStream.str()
2585         << AllocatorRange;
2586     S.Diag(PrevAllocatorLoc, diag::note_omp_previous_allocator)
2587         << PrevAllocatorRange;
2588     return true;
2589   }
2590   return false;
2591 }
2592 
2593 static void
2594 applyOMPAllocateAttribute(Sema &S, VarDecl *VD,
2595                           OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
2596                           Expr *Allocator, SourceRange SR) {
2597   if (VD->hasAttr<OMPAllocateDeclAttr>())
2598     return;
2599   if (Allocator &&
2600       (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
2601        Allocator->isInstantiationDependent() ||
2602        Allocator->containsUnexpandedParameterPack()))
2603     return;
2604   auto *A = OMPAllocateDeclAttr::CreateImplicit(S.Context, AllocatorKind,
2605                                                 Allocator, SR);
2606   VD->addAttr(A);
2607   if (ASTMutationListener *ML = S.Context.getASTMutationListener())
2608     ML->DeclarationMarkedOpenMPAllocate(VD, A);
2609 }
2610 
2611 Sema::DeclGroupPtrTy Sema::ActOnOpenMPAllocateDirective(
2612     SourceLocation Loc, ArrayRef<Expr *> VarList,
2613     ArrayRef<OMPClause *> Clauses, DeclContext *Owner) {
2614   assert(Clauses.size() <= 1 && "Expected at most one clause.");
2615   Expr *Allocator = nullptr;
2616   if (Clauses.empty()) {
2617     // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions.
2618     // allocate directives that appear in a target region must specify an
2619     // allocator clause unless a requires directive with the dynamic_allocators
2620     // clause is present in the same compilation unit.
2621     if (LangOpts.OpenMPIsDevice &&
2622         !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
2623       targetDiag(Loc, diag::err_expected_allocator_clause);
2624   } else {
2625     Allocator = cast<OMPAllocatorClause>(Clauses.back())->getAllocator();
2626   }
2627   OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
2628       getAllocatorKind(*this, DSAStack, Allocator);
2629   SmallVector<Expr *, 8> Vars;
2630   for (Expr *RefExpr : VarList) {
2631     auto *DE = cast<DeclRefExpr>(RefExpr);
2632     auto *VD = cast<VarDecl>(DE->getDecl());
2633 
2634     // Check if this is a TLS variable or global register.
2635     if (VD->getTLSKind() != VarDecl::TLS_None ||
2636         VD->hasAttr<OMPThreadPrivateDeclAttr>() ||
2637         (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
2638          !VD->isLocalVarDecl()))
2639       continue;
2640 
2641     // If the used several times in the allocate directive, the same allocator
2642     // must be used.
2643     if (checkPreviousOMPAllocateAttribute(*this, DSAStack, RefExpr, VD,
2644                                           AllocatorKind, Allocator))
2645       continue;
2646 
2647     // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++
2648     // If a list item has a static storage type, the allocator expression in the
2649     // allocator clause must be a constant expression that evaluates to one of
2650     // the predefined memory allocator values.
2651     if (Allocator && VD->hasGlobalStorage()) {
2652       if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) {
2653         Diag(Allocator->getExprLoc(),
2654              diag::err_omp_expected_predefined_allocator)
2655             << Allocator->getSourceRange();
2656         bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2657                       VarDecl::DeclarationOnly;
2658         Diag(VD->getLocation(),
2659              IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2660             << VD;
2661         continue;
2662       }
2663     }
2664 
2665     Vars.push_back(RefExpr);
2666     applyOMPAllocateAttribute(*this, VD, AllocatorKind, Allocator,
2667                               DE->getSourceRange());
2668   }
2669   if (Vars.empty())
2670     return nullptr;
2671   if (!Owner)
2672     Owner = getCurLexicalContext();
2673   auto *D = OMPAllocateDecl::Create(Context, Owner, Loc, Vars, Clauses);
2674   D->setAccess(AS_public);
2675   Owner->addDecl(D);
2676   return DeclGroupPtrTy::make(DeclGroupRef(D));
2677 }
2678 
2679 Sema::DeclGroupPtrTy
2680 Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc,
2681                                    ArrayRef<OMPClause *> ClauseList) {
2682   OMPRequiresDecl *D = nullptr;
2683   if (!CurContext->isFileContext()) {
2684     Diag(Loc, diag::err_omp_invalid_scope) << "requires";
2685   } else {
2686     D = CheckOMPRequiresDecl(Loc, ClauseList);
2687     if (D) {
2688       CurContext->addDecl(D);
2689       DSAStack->addRequiresDecl(D);
2690     }
2691   }
2692   return DeclGroupPtrTy::make(DeclGroupRef(D));
2693 }
2694 
2695 OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc,
2696                                             ArrayRef<OMPClause *> ClauseList) {
2697   /// For target specific clauses, the requires directive cannot be
2698   /// specified after the handling of any of the target regions in the
2699   /// current compilation unit.
2700   ArrayRef<SourceLocation> TargetLocations =
2701       DSAStack->getEncounteredTargetLocs();
2702   if (!TargetLocations.empty()) {
2703     for (const OMPClause *CNew : ClauseList) {
2704       // Check if any of the requires clauses affect target regions.
2705       if (isa<OMPUnifiedSharedMemoryClause>(CNew) ||
2706           isa<OMPUnifiedAddressClause>(CNew) ||
2707           isa<OMPReverseOffloadClause>(CNew) ||
2708           isa<OMPDynamicAllocatorsClause>(CNew)) {
2709         Diag(Loc, diag::err_omp_target_before_requires)
2710             << getOpenMPClauseName(CNew->getClauseKind());
2711         for (SourceLocation TargetLoc : TargetLocations) {
2712           Diag(TargetLoc, diag::note_omp_requires_encountered_target);
2713         }
2714       }
2715     }
2716   }
2717 
2718   if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
2719     return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc,
2720                                    ClauseList);
2721   return nullptr;
2722 }
2723 
2724 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
2725                               const ValueDecl *D,
2726                               const DSAStackTy::DSAVarData &DVar,
2727                               bool IsLoopIterVar = false) {
2728   if (DVar.RefExpr) {
2729     SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
2730         << getOpenMPClauseName(DVar.CKind);
2731     return;
2732   }
2733   enum {
2734     PDSA_StaticMemberShared,
2735     PDSA_StaticLocalVarShared,
2736     PDSA_LoopIterVarPrivate,
2737     PDSA_LoopIterVarLinear,
2738     PDSA_LoopIterVarLastprivate,
2739     PDSA_ConstVarShared,
2740     PDSA_GlobalVarShared,
2741     PDSA_TaskVarFirstprivate,
2742     PDSA_LocalVarPrivate,
2743     PDSA_Implicit
2744   } Reason = PDSA_Implicit;
2745   bool ReportHint = false;
2746   auto ReportLoc = D->getLocation();
2747   auto *VD = dyn_cast<VarDecl>(D);
2748   if (IsLoopIterVar) {
2749     if (DVar.CKind == OMPC_private)
2750       Reason = PDSA_LoopIterVarPrivate;
2751     else if (DVar.CKind == OMPC_lastprivate)
2752       Reason = PDSA_LoopIterVarLastprivate;
2753     else
2754       Reason = PDSA_LoopIterVarLinear;
2755   } else if (isOpenMPTaskingDirective(DVar.DKind) &&
2756              DVar.CKind == OMPC_firstprivate) {
2757     Reason = PDSA_TaskVarFirstprivate;
2758     ReportLoc = DVar.ImplicitDSALoc;
2759   } else if (VD && VD->isStaticLocal())
2760     Reason = PDSA_StaticLocalVarShared;
2761   else if (VD && VD->isStaticDataMember())
2762     Reason = PDSA_StaticMemberShared;
2763   else if (VD && VD->isFileVarDecl())
2764     Reason = PDSA_GlobalVarShared;
2765   else if (D->getType().isConstant(SemaRef.getASTContext()))
2766     Reason = PDSA_ConstVarShared;
2767   else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
2768     ReportHint = true;
2769     Reason = PDSA_LocalVarPrivate;
2770   }
2771   if (Reason != PDSA_Implicit) {
2772     SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
2773         << Reason << ReportHint
2774         << getOpenMPDirectiveName(Stack->getCurrentDirective());
2775   } else if (DVar.ImplicitDSALoc.isValid()) {
2776     SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
2777         << getOpenMPClauseName(DVar.CKind);
2778   }
2779 }
2780 
2781 namespace {
2782 class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
2783   DSAStackTy *Stack;
2784   Sema &SemaRef;
2785   bool ErrorFound = false;
2786   CapturedStmt *CS = nullptr;
2787   llvm::SmallVector<Expr *, 4> ImplicitFirstprivate;
2788   llvm::SmallVector<Expr *, 4> ImplicitMap;
2789   Sema::VarsWithInheritedDSAType VarsWithInheritedDSA;
2790   llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
2791 
2792   void VisitSubCaptures(OMPExecutableDirective *S) {
2793     // Check implicitly captured variables.
2794     if (!S->hasAssociatedStmt() || !S->getAssociatedStmt())
2795       return;
2796     visitSubCaptures(S->getInnermostCapturedStmt());
2797   }
2798 
2799 public:
2800   void VisitDeclRefExpr(DeclRefExpr *E) {
2801     if (E->isTypeDependent() || E->isValueDependent() ||
2802         E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2803       return;
2804     if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
2805       // Check the datasharing rules for the expressions in the clauses.
2806       if (!CS) {
2807         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
2808           if (!CED->hasAttr<OMPCaptureNoInitAttr>()) {
2809             Visit(CED->getInit());
2810             return;
2811           }
2812       } else if (VD->isImplicit() || isa<OMPCapturedExprDecl>(VD))
2813         // Do not analyze internal variables and do not enclose them into
2814         // implicit clauses.
2815         return;
2816       VD = VD->getCanonicalDecl();
2817       // Skip internally declared variables.
2818       if (VD->hasLocalStorage() && CS && !CS->capturesVariable(VD))
2819         return;
2820 
2821       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
2822       // Check if the variable has explicit DSA set and stop analysis if it so.
2823       if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second)
2824         return;
2825 
2826       // Skip internally declared static variables.
2827       llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
2828           OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
2829       if (VD->hasGlobalStorage() && CS && !CS->capturesVariable(VD) &&
2830           (Stack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() ||
2831            !Res || *Res != OMPDeclareTargetDeclAttr::MT_Link))
2832         return;
2833 
2834       SourceLocation ELoc = E->getExprLoc();
2835       OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
2836       // The default(none) clause requires that each variable that is referenced
2837       // in the construct, and does not have a predetermined data-sharing
2838       // attribute, must have its data-sharing attribute explicitly determined
2839       // by being listed in a data-sharing attribute clause.
2840       if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
2841           isImplicitOrExplicitTaskingRegion(DKind) &&
2842           VarsWithInheritedDSA.count(VD) == 0) {
2843         VarsWithInheritedDSA[VD] = E;
2844         return;
2845       }
2846 
2847       if (isOpenMPTargetExecutionDirective(DKind) &&
2848           !Stack->isLoopControlVariable(VD).first) {
2849         if (!Stack->checkMappableExprComponentListsForDecl(
2850                 VD, /*CurrentRegionOnly=*/true,
2851                 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2852                        StackComponents,
2853                    OpenMPClauseKind) {
2854                   // Variable is used if it has been marked as an array, array
2855                   // section or the variable iself.
2856                   return StackComponents.size() == 1 ||
2857                          std::all_of(
2858                              std::next(StackComponents.rbegin()),
2859                              StackComponents.rend(),
2860                              [](const OMPClauseMappableExprCommon::
2861                                     MappableComponent &MC) {
2862                                return MC.getAssociatedDeclaration() ==
2863                                           nullptr &&
2864                                       (isa<OMPArraySectionExpr>(
2865                                            MC.getAssociatedExpression()) ||
2866                                        isa<ArraySubscriptExpr>(
2867                                            MC.getAssociatedExpression()));
2868                              });
2869                 })) {
2870           bool IsFirstprivate = false;
2871           // By default lambdas are captured as firstprivates.
2872           if (const auto *RD =
2873                   VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
2874             IsFirstprivate = RD->isLambda();
2875           IsFirstprivate =
2876               IsFirstprivate ||
2877               (VD->getType().getNonReferenceType()->isScalarType() &&
2878                Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res);
2879           if (IsFirstprivate)
2880             ImplicitFirstprivate.emplace_back(E);
2881           else
2882             ImplicitMap.emplace_back(E);
2883           return;
2884         }
2885       }
2886 
2887       // OpenMP [2.9.3.6, Restrictions, p.2]
2888       //  A list item that appears in a reduction clause of the innermost
2889       //  enclosing worksharing or parallel construct may not be accessed in an
2890       //  explicit task.
2891       DVar = Stack->hasInnermostDSA(
2892           VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2893           [](OpenMPDirectiveKind K) {
2894             return isOpenMPParallelDirective(K) ||
2895                    isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2896           },
2897           /*FromParent=*/true);
2898       if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
2899         ErrorFound = true;
2900         SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
2901         reportOriginalDsa(SemaRef, Stack, VD, DVar);
2902         return;
2903       }
2904 
2905       // Define implicit data-sharing attributes for task.
2906       DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false);
2907       if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
2908           !Stack->isLoopControlVariable(VD).first) {
2909         ImplicitFirstprivate.push_back(E);
2910         return;
2911       }
2912 
2913       // Store implicitly used globals with declare target link for parent
2914       // target.
2915       if (!isOpenMPTargetExecutionDirective(DKind) && Res &&
2916           *Res == OMPDeclareTargetDeclAttr::MT_Link) {
2917         Stack->addToParentTargetRegionLinkGlobals(E);
2918         return;
2919       }
2920     }
2921   }
2922   void VisitMemberExpr(MemberExpr *E) {
2923     if (E->isTypeDependent() || E->isValueDependent() ||
2924         E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2925       return;
2926     auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
2927     OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
2928     if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParens())) {
2929       if (!FD)
2930         return;
2931       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false);
2932       // Check if the variable has explicit DSA set and stop analysis if it
2933       // so.
2934       if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second)
2935         return;
2936 
2937       if (isOpenMPTargetExecutionDirective(DKind) &&
2938           !Stack->isLoopControlVariable(FD).first &&
2939           !Stack->checkMappableExprComponentListsForDecl(
2940               FD, /*CurrentRegionOnly=*/true,
2941               [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2942                      StackComponents,
2943                  OpenMPClauseKind) {
2944                 return isa<CXXThisExpr>(
2945                     cast<MemberExpr>(
2946                         StackComponents.back().getAssociatedExpression())
2947                         ->getBase()
2948                         ->IgnoreParens());
2949               })) {
2950         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
2951         //  A bit-field cannot appear in a map clause.
2952         //
2953         if (FD->isBitField())
2954           return;
2955 
2956         // Check to see if the member expression is referencing a class that
2957         // has already been explicitly mapped
2958         if (Stack->isClassPreviouslyMapped(TE->getType()))
2959           return;
2960 
2961         ImplicitMap.emplace_back(E);
2962         return;
2963       }
2964 
2965       SourceLocation ELoc = E->getExprLoc();
2966       // OpenMP [2.9.3.6, Restrictions, p.2]
2967       //  A list item that appears in a reduction clause of the innermost
2968       //  enclosing worksharing or parallel construct may not be accessed in
2969       //  an  explicit task.
2970       DVar = Stack->hasInnermostDSA(
2971           FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2972           [](OpenMPDirectiveKind K) {
2973             return isOpenMPParallelDirective(K) ||
2974                    isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2975           },
2976           /*FromParent=*/true);
2977       if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
2978         ErrorFound = true;
2979         SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
2980         reportOriginalDsa(SemaRef, Stack, FD, DVar);
2981         return;
2982       }
2983 
2984       // Define implicit data-sharing attributes for task.
2985       DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false);
2986       if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
2987           !Stack->isLoopControlVariable(FD).first) {
2988         // Check if there is a captured expression for the current field in the
2989         // region. Do not mark it as firstprivate unless there is no captured
2990         // expression.
2991         // TODO: try to make it firstprivate.
2992         if (DVar.CKind != OMPC_unknown)
2993           ImplicitFirstprivate.push_back(E);
2994       }
2995       return;
2996     }
2997     if (isOpenMPTargetExecutionDirective(DKind)) {
2998       OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
2999       if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
3000                                         /*NoDiagnose=*/true))
3001         return;
3002       const auto *VD = cast<ValueDecl>(
3003           CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
3004       if (!Stack->checkMappableExprComponentListsForDecl(
3005               VD, /*CurrentRegionOnly=*/true,
3006               [&CurComponents](
3007                   OMPClauseMappableExprCommon::MappableExprComponentListRef
3008                       StackComponents,
3009                   OpenMPClauseKind) {
3010                 auto CCI = CurComponents.rbegin();
3011                 auto CCE = CurComponents.rend();
3012                 for (const auto &SC : llvm::reverse(StackComponents)) {
3013                   // Do both expressions have the same kind?
3014                   if (CCI->getAssociatedExpression()->getStmtClass() !=
3015                       SC.getAssociatedExpression()->getStmtClass())
3016                     if (!(isa<OMPArraySectionExpr>(
3017                               SC.getAssociatedExpression()) &&
3018                           isa<ArraySubscriptExpr>(
3019                               CCI->getAssociatedExpression())))
3020                       return false;
3021 
3022                   const Decl *CCD = CCI->getAssociatedDeclaration();
3023                   const Decl *SCD = SC.getAssociatedDeclaration();
3024                   CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
3025                   SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
3026                   if (SCD != CCD)
3027                     return false;
3028                   std::advance(CCI, 1);
3029                   if (CCI == CCE)
3030                     break;
3031                 }
3032                 return true;
3033               })) {
3034         Visit(E->getBase());
3035       }
3036     } else {
3037       Visit(E->getBase());
3038     }
3039   }
3040   void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
3041     for (OMPClause *C : S->clauses()) {
3042       // Skip analysis of arguments of implicitly defined firstprivate clause
3043       // for task|target directives.
3044       // Skip analysis of arguments of implicitly defined map clause for target
3045       // directives.
3046       if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
3047                  C->isImplicit())) {
3048         for (Stmt *CC : C->children()) {
3049           if (CC)
3050             Visit(CC);
3051         }
3052       }
3053     }
3054     // Check implicitly captured variables.
3055     VisitSubCaptures(S);
3056   }
3057   void VisitStmt(Stmt *S) {
3058     for (Stmt *C : S->children()) {
3059       if (C) {
3060         // Check implicitly captured variables in the task-based directives to
3061         // check if they must be firstprivatized.
3062         Visit(C);
3063       }
3064     }
3065   }
3066 
3067   void visitSubCaptures(CapturedStmt *S) {
3068     for (const CapturedStmt::Capture &Cap : S->captures()) {
3069       if (!Cap.capturesVariable() && !Cap.capturesVariableByCopy())
3070         continue;
3071       VarDecl *VD = Cap.getCapturedVar();
3072       // Do not try to map the variable if it or its sub-component was mapped
3073       // already.
3074       if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
3075           Stack->checkMappableExprComponentListsForDecl(
3076               VD, /*CurrentRegionOnly=*/true,
3077               [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
3078                  OpenMPClauseKind) { return true; }))
3079         continue;
3080       DeclRefExpr *DRE = buildDeclRefExpr(
3081           SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context),
3082           Cap.getLocation(), /*RefersToCapture=*/true);
3083       Visit(DRE);
3084     }
3085   }
3086   bool isErrorFound() const { return ErrorFound; }
3087   ArrayRef<Expr *> getImplicitFirstprivate() const {
3088     return ImplicitFirstprivate;
3089   }
3090   ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; }
3091   const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
3092     return VarsWithInheritedDSA;
3093   }
3094 
3095   DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
3096       : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {
3097     // Process declare target link variables for the target directives.
3098     if (isOpenMPTargetExecutionDirective(S->getCurrentDirective())) {
3099       for (DeclRefExpr *E : Stack->getLinkGlobals())
3100         Visit(E);
3101     }
3102   }
3103 };
3104 } // namespace
3105 
3106 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
3107   switch (DKind) {
3108   case OMPD_parallel:
3109   case OMPD_parallel_for:
3110   case OMPD_parallel_for_simd:
3111   case OMPD_parallel_sections:
3112   case OMPD_teams:
3113   case OMPD_teams_distribute:
3114   case OMPD_teams_distribute_simd: {
3115     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3116     QualType KmpInt32PtrTy =
3117         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3118     Sema::CapturedParamNameType Params[] = {
3119         std::make_pair(".global_tid.", KmpInt32PtrTy),
3120         std::make_pair(".bound_tid.", KmpInt32PtrTy),
3121         std::make_pair(StringRef(), QualType()) // __context with shared vars
3122     };
3123     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3124                              Params);
3125     break;
3126   }
3127   case OMPD_target_teams:
3128   case OMPD_target_parallel:
3129   case OMPD_target_parallel_for:
3130   case OMPD_target_parallel_for_simd:
3131   case OMPD_target_teams_distribute:
3132   case OMPD_target_teams_distribute_simd: {
3133     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3134     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3135     QualType KmpInt32PtrTy =
3136         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3137     QualType Args[] = {VoidPtrTy};
3138     FunctionProtoType::ExtProtoInfo EPI;
3139     EPI.Variadic = true;
3140     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3141     Sema::CapturedParamNameType Params[] = {
3142         std::make_pair(".global_tid.", KmpInt32Ty),
3143         std::make_pair(".part_id.", KmpInt32PtrTy),
3144         std::make_pair(".privates.", VoidPtrTy),
3145         std::make_pair(
3146             ".copy_fn.",
3147             Context.getPointerType(CopyFnType).withConst().withRestrict()),
3148         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3149         std::make_pair(StringRef(), QualType()) // __context with shared vars
3150     };
3151     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3152                              Params, /*OpenMPCaptureLevel=*/0);
3153     // Mark this captured region as inlined, because we don't use outlined
3154     // function directly.
3155     getCurCapturedRegion()->TheCapturedDecl->addAttr(
3156         AlwaysInlineAttr::CreateImplicit(
3157             Context, {}, AttributeCommonInfo::AS_Keyword,
3158             AlwaysInlineAttr::Keyword_forceinline));
3159     Sema::CapturedParamNameType ParamsTarget[] = {
3160         std::make_pair(StringRef(), QualType()) // __context with shared vars
3161     };
3162     // Start a captured region for 'target' with no implicit parameters.
3163     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3164                              ParamsTarget, /*OpenMPCaptureLevel=*/1);
3165     Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
3166         std::make_pair(".global_tid.", KmpInt32PtrTy),
3167         std::make_pair(".bound_tid.", KmpInt32PtrTy),
3168         std::make_pair(StringRef(), QualType()) // __context with shared vars
3169     };
3170     // Start a captured region for 'teams' or 'parallel'.  Both regions have
3171     // the same implicit parameters.
3172     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3173                              ParamsTeamsOrParallel, /*OpenMPCaptureLevel=*/2);
3174     break;
3175   }
3176   case OMPD_target:
3177   case OMPD_target_simd: {
3178     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3179     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3180     QualType KmpInt32PtrTy =
3181         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3182     QualType Args[] = {VoidPtrTy};
3183     FunctionProtoType::ExtProtoInfo EPI;
3184     EPI.Variadic = true;
3185     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3186     Sema::CapturedParamNameType Params[] = {
3187         std::make_pair(".global_tid.", KmpInt32Ty),
3188         std::make_pair(".part_id.", KmpInt32PtrTy),
3189         std::make_pair(".privates.", VoidPtrTy),
3190         std::make_pair(
3191             ".copy_fn.",
3192             Context.getPointerType(CopyFnType).withConst().withRestrict()),
3193         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3194         std::make_pair(StringRef(), QualType()) // __context with shared vars
3195     };
3196     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3197                              Params, /*OpenMPCaptureLevel=*/0);
3198     // Mark this captured region as inlined, because we don't use outlined
3199     // function directly.
3200     getCurCapturedRegion()->TheCapturedDecl->addAttr(
3201         AlwaysInlineAttr::CreateImplicit(
3202             Context, {}, AttributeCommonInfo::AS_Keyword,
3203             AlwaysInlineAttr::Keyword_forceinline));
3204     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3205                              std::make_pair(StringRef(), QualType()),
3206                              /*OpenMPCaptureLevel=*/1);
3207     break;
3208   }
3209   case OMPD_simd:
3210   case OMPD_for:
3211   case OMPD_for_simd:
3212   case OMPD_sections:
3213   case OMPD_section:
3214   case OMPD_single:
3215   case OMPD_master:
3216   case OMPD_critical:
3217   case OMPD_taskgroup:
3218   case OMPD_distribute:
3219   case OMPD_distribute_simd:
3220   case OMPD_ordered:
3221   case OMPD_atomic:
3222   case OMPD_target_data: {
3223     Sema::CapturedParamNameType Params[] = {
3224         std::make_pair(StringRef(), QualType()) // __context with shared vars
3225     };
3226     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3227                              Params);
3228     break;
3229   }
3230   case OMPD_task: {
3231     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3232     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3233     QualType KmpInt32PtrTy =
3234         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3235     QualType Args[] = {VoidPtrTy};
3236     FunctionProtoType::ExtProtoInfo EPI;
3237     EPI.Variadic = true;
3238     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3239     Sema::CapturedParamNameType Params[] = {
3240         std::make_pair(".global_tid.", KmpInt32Ty),
3241         std::make_pair(".part_id.", KmpInt32PtrTy),
3242         std::make_pair(".privates.", VoidPtrTy),
3243         std::make_pair(
3244             ".copy_fn.",
3245             Context.getPointerType(CopyFnType).withConst().withRestrict()),
3246         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3247         std::make_pair(StringRef(), QualType()) // __context with shared vars
3248     };
3249     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3250                              Params);
3251     // Mark this captured region as inlined, because we don't use outlined
3252     // function directly.
3253     getCurCapturedRegion()->TheCapturedDecl->addAttr(
3254         AlwaysInlineAttr::CreateImplicit(
3255             Context, {}, AttributeCommonInfo::AS_Keyword,
3256             AlwaysInlineAttr::Keyword_forceinline));
3257     break;
3258   }
3259   case OMPD_taskloop:
3260   case OMPD_taskloop_simd: {
3261     QualType KmpInt32Ty =
3262         Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
3263             .withConst();
3264     QualType KmpUInt64Ty =
3265         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
3266             .withConst();
3267     QualType KmpInt64Ty =
3268         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
3269             .withConst();
3270     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3271     QualType KmpInt32PtrTy =
3272         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3273     QualType Args[] = {VoidPtrTy};
3274     FunctionProtoType::ExtProtoInfo EPI;
3275     EPI.Variadic = true;
3276     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3277     Sema::CapturedParamNameType Params[] = {
3278         std::make_pair(".global_tid.", KmpInt32Ty),
3279         std::make_pair(".part_id.", KmpInt32PtrTy),
3280         std::make_pair(".privates.", VoidPtrTy),
3281         std::make_pair(
3282             ".copy_fn.",
3283             Context.getPointerType(CopyFnType).withConst().withRestrict()),
3284         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3285         std::make_pair(".lb.", KmpUInt64Ty),
3286         std::make_pair(".ub.", KmpUInt64Ty),
3287         std::make_pair(".st.", KmpInt64Ty),
3288         std::make_pair(".liter.", KmpInt32Ty),
3289         std::make_pair(".reductions.", VoidPtrTy),
3290         std::make_pair(StringRef(), QualType()) // __context with shared vars
3291     };
3292     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3293                              Params);
3294     // Mark this captured region as inlined, because we don't use outlined
3295     // function directly.
3296     getCurCapturedRegion()->TheCapturedDecl->addAttr(
3297         AlwaysInlineAttr::CreateImplicit(
3298             Context, {}, AttributeCommonInfo::AS_Keyword,
3299             AlwaysInlineAttr::Keyword_forceinline));
3300     break;
3301   }
3302   case OMPD_distribute_parallel_for_simd:
3303   case OMPD_distribute_parallel_for: {
3304     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3305     QualType KmpInt32PtrTy =
3306         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3307     Sema::CapturedParamNameType Params[] = {
3308         std::make_pair(".global_tid.", KmpInt32PtrTy),
3309         std::make_pair(".bound_tid.", KmpInt32PtrTy),
3310         std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
3311         std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
3312         std::make_pair(StringRef(), QualType()) // __context with shared vars
3313     };
3314     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3315                              Params);
3316     break;
3317   }
3318   case OMPD_target_teams_distribute_parallel_for:
3319   case OMPD_target_teams_distribute_parallel_for_simd: {
3320     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3321     QualType KmpInt32PtrTy =
3322         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3323     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3324 
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     Sema::CapturedParamNameType ParamsTarget[] = {
3348         std::make_pair(StringRef(), QualType()) // __context with shared vars
3349     };
3350     // Start a captured region for 'target' with no implicit parameters.
3351     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3352                              ParamsTarget, /*OpenMPCaptureLevel=*/1);
3353 
3354     Sema::CapturedParamNameType ParamsTeams[] = {
3355         std::make_pair(".global_tid.", KmpInt32PtrTy),
3356         std::make_pair(".bound_tid.", KmpInt32PtrTy),
3357         std::make_pair(StringRef(), QualType()) // __context with shared vars
3358     };
3359     // Start a captured region for 'target' with no implicit parameters.
3360     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3361                              ParamsTeams, /*OpenMPCaptureLevel=*/2);
3362 
3363     Sema::CapturedParamNameType ParamsParallel[] = {
3364         std::make_pair(".global_tid.", KmpInt32PtrTy),
3365         std::make_pair(".bound_tid.", KmpInt32PtrTy),
3366         std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
3367         std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
3368         std::make_pair(StringRef(), QualType()) // __context with shared vars
3369     };
3370     // Start a captured region for 'teams' or 'parallel'.  Both regions have
3371     // the same implicit parameters.
3372     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3373                              ParamsParallel, /*OpenMPCaptureLevel=*/3);
3374     break;
3375   }
3376 
3377   case OMPD_teams_distribute_parallel_for:
3378   case OMPD_teams_distribute_parallel_for_simd: {
3379     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3380     QualType KmpInt32PtrTy =
3381         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3382 
3383     Sema::CapturedParamNameType ParamsTeams[] = {
3384         std::make_pair(".global_tid.", KmpInt32PtrTy),
3385         std::make_pair(".bound_tid.", KmpInt32PtrTy),
3386         std::make_pair(StringRef(), QualType()) // __context with shared vars
3387     };
3388     // Start a captured region for 'target' with no implicit parameters.
3389     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3390                              ParamsTeams, /*OpenMPCaptureLevel=*/0);
3391 
3392     Sema::CapturedParamNameType ParamsParallel[] = {
3393         std::make_pair(".global_tid.", KmpInt32PtrTy),
3394         std::make_pair(".bound_tid.", KmpInt32PtrTy),
3395         std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
3396         std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
3397         std::make_pair(StringRef(), QualType()) // __context with shared vars
3398     };
3399     // Start a captured region for 'teams' or 'parallel'.  Both regions have
3400     // the same implicit parameters.
3401     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3402                              ParamsParallel, /*OpenMPCaptureLevel=*/1);
3403     break;
3404   }
3405   case OMPD_target_update:
3406   case OMPD_target_enter_data:
3407   case OMPD_target_exit_data: {
3408     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3409     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3410     QualType KmpInt32PtrTy =
3411         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3412     QualType Args[] = {VoidPtrTy};
3413     FunctionProtoType::ExtProtoInfo EPI;
3414     EPI.Variadic = true;
3415     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3416     Sema::CapturedParamNameType Params[] = {
3417         std::make_pair(".global_tid.", KmpInt32Ty),
3418         std::make_pair(".part_id.", KmpInt32PtrTy),
3419         std::make_pair(".privates.", VoidPtrTy),
3420         std::make_pair(
3421             ".copy_fn.",
3422             Context.getPointerType(CopyFnType).withConst().withRestrict()),
3423         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3424         std::make_pair(StringRef(), QualType()) // __context with shared vars
3425     };
3426     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3427                              Params);
3428     // Mark this captured region as inlined, because we don't use outlined
3429     // function directly.
3430     getCurCapturedRegion()->TheCapturedDecl->addAttr(
3431         AlwaysInlineAttr::CreateImplicit(
3432             Context, {}, AttributeCommonInfo::AS_Keyword,
3433             AlwaysInlineAttr::Keyword_forceinline));
3434     break;
3435   }
3436   case OMPD_threadprivate:
3437   case OMPD_allocate:
3438   case OMPD_taskyield:
3439   case OMPD_barrier:
3440   case OMPD_taskwait:
3441   case OMPD_cancellation_point:
3442   case OMPD_cancel:
3443   case OMPD_flush:
3444   case OMPD_declare_reduction:
3445   case OMPD_declare_mapper:
3446   case OMPD_declare_simd:
3447   case OMPD_declare_target:
3448   case OMPD_end_declare_target:
3449   case OMPD_requires:
3450   case OMPD_declare_variant:
3451     llvm_unreachable("OpenMP Directive is not allowed");
3452   case OMPD_unknown:
3453     llvm_unreachable("Unknown OpenMP directive");
3454   }
3455 }
3456 
3457 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
3458   SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
3459   getOpenMPCaptureRegions(CaptureRegions, DKind);
3460   return CaptureRegions.size();
3461 }
3462 
3463 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
3464                                              Expr *CaptureExpr, bool WithInit,
3465                                              bool AsExpression) {
3466   assert(CaptureExpr);
3467   ASTContext &C = S.getASTContext();
3468   Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
3469   QualType Ty = Init->getType();
3470   if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
3471     if (S.getLangOpts().CPlusPlus) {
3472       Ty = C.getLValueReferenceType(Ty);
3473     } else {
3474       Ty = C.getPointerType(Ty);
3475       ExprResult Res =
3476           S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
3477       if (!Res.isUsable())
3478         return nullptr;
3479       Init = Res.get();
3480     }
3481     WithInit = true;
3482   }
3483   auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
3484                                           CaptureExpr->getBeginLoc());
3485   if (!WithInit)
3486     CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C));
3487   S.CurContext->addHiddenDecl(CED);
3488   S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
3489   return CED;
3490 }
3491 
3492 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
3493                                  bool WithInit) {
3494   OMPCapturedExprDecl *CD;
3495   if (VarDecl *VD = S.isOpenMPCapturedDecl(D))
3496     CD = cast<OMPCapturedExprDecl>(VD);
3497   else
3498     CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
3499                           /*AsExpression=*/false);
3500   return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
3501                           CaptureExpr->getExprLoc());
3502 }
3503 
3504 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
3505   CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
3506   if (!Ref) {
3507     OMPCapturedExprDecl *CD = buildCaptureDecl(
3508         S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
3509         /*WithInit=*/true, /*AsExpression=*/true);
3510     Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
3511                            CaptureExpr->getExprLoc());
3512   }
3513   ExprResult Res = Ref;
3514   if (!S.getLangOpts().CPlusPlus &&
3515       CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
3516       Ref->getType()->isPointerType()) {
3517     Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
3518     if (!Res.isUsable())
3519       return ExprError();
3520   }
3521   return S.DefaultLvalueConversion(Res.get());
3522 }
3523 
3524 namespace {
3525 // OpenMP directives parsed in this section are represented as a
3526 // CapturedStatement with an associated statement.  If a syntax error
3527 // is detected during the parsing of the associated statement, the
3528 // compiler must abort processing and close the CapturedStatement.
3529 //
3530 // Combined directives such as 'target parallel' have more than one
3531 // nested CapturedStatements.  This RAII ensures that we unwind out
3532 // of all the nested CapturedStatements when an error is found.
3533 class CaptureRegionUnwinderRAII {
3534 private:
3535   Sema &S;
3536   bool &ErrorFound;
3537   OpenMPDirectiveKind DKind = OMPD_unknown;
3538 
3539 public:
3540   CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
3541                             OpenMPDirectiveKind DKind)
3542       : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
3543   ~CaptureRegionUnwinderRAII() {
3544     if (ErrorFound) {
3545       int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
3546       while (--ThisCaptureLevel >= 0)
3547         S.ActOnCapturedRegionError();
3548     }
3549   }
3550 };
3551 } // namespace
3552 
3553 void Sema::tryCaptureOpenMPLambdas(ValueDecl *V) {
3554   // Capture variables captured by reference in lambdas for target-based
3555   // directives.
3556   if (!CurContext->isDependentContext() &&
3557       (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) ||
3558        isOpenMPTargetDataManagementDirective(
3559            DSAStack->getCurrentDirective()))) {
3560     QualType Type = V->getType();
3561     if (const auto *RD = Type.getCanonicalType()
3562                              .getNonReferenceType()
3563                              ->getAsCXXRecordDecl()) {
3564       bool SavedForceCaptureByReferenceInTargetExecutable =
3565           DSAStack->isForceCaptureByReferenceInTargetExecutable();
3566       DSAStack->setForceCaptureByReferenceInTargetExecutable(
3567           /*V=*/true);
3568       if (RD->isLambda()) {
3569         llvm::DenseMap<const VarDecl *, FieldDecl *> Captures;
3570         FieldDecl *ThisCapture;
3571         RD->getCaptureFields(Captures, ThisCapture);
3572         for (const LambdaCapture &LC : RD->captures()) {
3573           if (LC.getCaptureKind() == LCK_ByRef) {
3574             VarDecl *VD = LC.getCapturedVar();
3575             DeclContext *VDC = VD->getDeclContext();
3576             if (!VDC->Encloses(CurContext))
3577               continue;
3578             MarkVariableReferenced(LC.getLocation(), VD);
3579           } else if (LC.getCaptureKind() == LCK_This) {
3580             QualType ThisTy = getCurrentThisType();
3581             if (!ThisTy.isNull() &&
3582                 Context.typesAreCompatible(ThisTy, ThisCapture->getType()))
3583               CheckCXXThisCapture(LC.getLocation());
3584           }
3585         }
3586       }
3587       DSAStack->setForceCaptureByReferenceInTargetExecutable(
3588           SavedForceCaptureByReferenceInTargetExecutable);
3589     }
3590   }
3591 }
3592 
3593 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
3594                                       ArrayRef<OMPClause *> Clauses) {
3595   bool ErrorFound = false;
3596   CaptureRegionUnwinderRAII CaptureRegionUnwinder(
3597       *this, ErrorFound, DSAStack->getCurrentDirective());
3598   if (!S.isUsable()) {
3599     ErrorFound = true;
3600     return StmtError();
3601   }
3602 
3603   SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
3604   getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
3605   OMPOrderedClause *OC = nullptr;
3606   OMPScheduleClause *SC = nullptr;
3607   SmallVector<const OMPLinearClause *, 4> LCs;
3608   SmallVector<const OMPClauseWithPreInit *, 4> PICs;
3609   // This is required for proper codegen.
3610   for (OMPClause *Clause : Clauses) {
3611     if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
3612         Clause->getClauseKind() == OMPC_in_reduction) {
3613       // Capture taskgroup task_reduction descriptors inside the tasking regions
3614       // with the corresponding in_reduction items.
3615       auto *IRC = cast<OMPInReductionClause>(Clause);
3616       for (Expr *E : IRC->taskgroup_descriptors())
3617         if (E)
3618           MarkDeclarationsReferencedInExpr(E);
3619     }
3620     if (isOpenMPPrivate(Clause->getClauseKind()) ||
3621         Clause->getClauseKind() == OMPC_copyprivate ||
3622         (getLangOpts().OpenMPUseTLS &&
3623          getASTContext().getTargetInfo().isTLSSupported() &&
3624          Clause->getClauseKind() == OMPC_copyin)) {
3625       DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
3626       // Mark all variables in private list clauses as used in inner region.
3627       for (Stmt *VarRef : Clause->children()) {
3628         if (auto *E = cast_or_null<Expr>(VarRef)) {
3629           MarkDeclarationsReferencedInExpr(E);
3630         }
3631       }
3632       DSAStack->setForceVarCapturing(/*V=*/false);
3633     } else if (CaptureRegions.size() > 1 ||
3634                CaptureRegions.back() != OMPD_unknown) {
3635       if (auto *C = OMPClauseWithPreInit::get(Clause))
3636         PICs.push_back(C);
3637       if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
3638         if (Expr *E = C->getPostUpdateExpr())
3639           MarkDeclarationsReferencedInExpr(E);
3640       }
3641     }
3642     if (Clause->getClauseKind() == OMPC_schedule)
3643       SC = cast<OMPScheduleClause>(Clause);
3644     else if (Clause->getClauseKind() == OMPC_ordered)
3645       OC = cast<OMPOrderedClause>(Clause);
3646     else if (Clause->getClauseKind() == OMPC_linear)
3647       LCs.push_back(cast<OMPLinearClause>(Clause));
3648   }
3649   // OpenMP, 2.7.1 Loop Construct, Restrictions
3650   // The nonmonotonic modifier cannot be specified if an ordered clause is
3651   // specified.
3652   if (SC &&
3653       (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
3654        SC->getSecondScheduleModifier() ==
3655            OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
3656       OC) {
3657     Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
3658              ? SC->getFirstScheduleModifierLoc()
3659              : SC->getSecondScheduleModifierLoc(),
3660          diag::err_omp_schedule_nonmonotonic_ordered)
3661         << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
3662     ErrorFound = true;
3663   }
3664   if (!LCs.empty() && OC && OC->getNumForLoops()) {
3665     for (const OMPLinearClause *C : LCs) {
3666       Diag(C->getBeginLoc(), diag::err_omp_linear_ordered)
3667           << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
3668     }
3669     ErrorFound = true;
3670   }
3671   if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
3672       isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
3673       OC->getNumForLoops()) {
3674     Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd)
3675         << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
3676     ErrorFound = true;
3677   }
3678   if (ErrorFound) {
3679     return StmtError();
3680   }
3681   StmtResult SR = S;
3682   unsigned CompletedRegions = 0;
3683   for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
3684     // Mark all variables in private list clauses as used in inner region.
3685     // Required for proper codegen of combined directives.
3686     // TODO: add processing for other clauses.
3687     if (ThisCaptureRegion != OMPD_unknown) {
3688       for (const clang::OMPClauseWithPreInit *C : PICs) {
3689         OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
3690         // Find the particular capture region for the clause if the
3691         // directive is a combined one with multiple capture regions.
3692         // If the directive is not a combined one, the capture region
3693         // associated with the clause is OMPD_unknown and is generated
3694         // only once.
3695         if (CaptureRegion == ThisCaptureRegion ||
3696             CaptureRegion == OMPD_unknown) {
3697           if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
3698             for (Decl *D : DS->decls())
3699               MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
3700           }
3701         }
3702       }
3703     }
3704     if (++CompletedRegions == CaptureRegions.size())
3705       DSAStack->setBodyComplete();
3706     SR = ActOnCapturedRegionEnd(SR.get());
3707   }
3708   return SR;
3709 }
3710 
3711 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
3712                               OpenMPDirectiveKind CancelRegion,
3713                               SourceLocation StartLoc) {
3714   // CancelRegion is only needed for cancel and cancellation_point.
3715   if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
3716     return false;
3717 
3718   if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
3719       CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
3720     return false;
3721 
3722   SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
3723       << getOpenMPDirectiveName(CancelRegion);
3724   return true;
3725 }
3726 
3727 static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
3728                                   OpenMPDirectiveKind CurrentRegion,
3729                                   const DeclarationNameInfo &CurrentName,
3730                                   OpenMPDirectiveKind CancelRegion,
3731                                   SourceLocation StartLoc) {
3732   if (Stack->getCurScope()) {
3733     OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
3734     OpenMPDirectiveKind OffendingRegion = ParentRegion;
3735     bool NestingProhibited = false;
3736     bool CloseNesting = true;
3737     bool OrphanSeen = false;
3738     enum {
3739       NoRecommend,
3740       ShouldBeInParallelRegion,
3741       ShouldBeInOrderedRegion,
3742       ShouldBeInTargetRegion,
3743       ShouldBeInTeamsRegion
3744     } Recommend = NoRecommend;
3745     if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
3746       // OpenMP [2.16, Nesting of Regions]
3747       // OpenMP constructs may not be nested inside a simd region.
3748       // OpenMP [2.8.1,simd Construct, Restrictions]
3749       // An ordered construct with the simd clause is the only OpenMP
3750       // construct that can appear in the simd region.
3751       // Allowing a SIMD construct nested in another SIMD construct is an
3752       // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
3753       // message.
3754       SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3755                                  ? diag::err_omp_prohibited_region_simd
3756                                  : diag::warn_omp_nesting_simd);
3757       return CurrentRegion != OMPD_simd;
3758     }
3759     if (ParentRegion == OMPD_atomic) {
3760       // OpenMP [2.16, Nesting of Regions]
3761       // OpenMP constructs may not be nested inside an atomic region.
3762       SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3763       return true;
3764     }
3765     if (CurrentRegion == OMPD_section) {
3766       // OpenMP [2.7.2, sections Construct, Restrictions]
3767       // Orphaned section directives are prohibited. That is, the section
3768       // directives must appear within the sections construct and must not be
3769       // encountered elsewhere in the sections region.
3770       if (ParentRegion != OMPD_sections &&
3771           ParentRegion != OMPD_parallel_sections) {
3772         SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3773             << (ParentRegion != OMPD_unknown)
3774             << getOpenMPDirectiveName(ParentRegion);
3775         return true;
3776       }
3777       return false;
3778     }
3779     // Allow some constructs (except teams and cancellation constructs) to be
3780     // orphaned (they could be used in functions, called from OpenMP regions
3781     // with the required preconditions).
3782     if (ParentRegion == OMPD_unknown &&
3783         !isOpenMPNestingTeamsDirective(CurrentRegion) &&
3784         CurrentRegion != OMPD_cancellation_point &&
3785         CurrentRegion != OMPD_cancel)
3786       return false;
3787     if (CurrentRegion == OMPD_cancellation_point ||
3788         CurrentRegion == OMPD_cancel) {
3789       // OpenMP [2.16, Nesting of Regions]
3790       // A cancellation point construct for which construct-type-clause is
3791       // taskgroup must be nested inside a task construct. A cancellation
3792       // point construct for which construct-type-clause is not taskgroup must
3793       // be closely nested inside an OpenMP construct that matches the type
3794       // specified in construct-type-clause.
3795       // A cancel construct for which construct-type-clause is taskgroup must be
3796       // nested inside a task construct. A cancel construct for which
3797       // construct-type-clause is not taskgroup must be closely nested inside an
3798       // OpenMP construct that matches the type specified in
3799       // construct-type-clause.
3800       NestingProhibited =
3801           !((CancelRegion == OMPD_parallel &&
3802              (ParentRegion == OMPD_parallel ||
3803               ParentRegion == OMPD_target_parallel)) ||
3804             (CancelRegion == OMPD_for &&
3805              (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
3806               ParentRegion == OMPD_target_parallel_for ||
3807               ParentRegion == OMPD_distribute_parallel_for ||
3808               ParentRegion == OMPD_teams_distribute_parallel_for ||
3809               ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
3810             (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3811             (CancelRegion == OMPD_sections &&
3812              (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3813               ParentRegion == OMPD_parallel_sections)));
3814       OrphanSeen = ParentRegion == OMPD_unknown;
3815     } else if (CurrentRegion == OMPD_master) {
3816       // OpenMP [2.16, Nesting of Regions]
3817       // A master region may not be closely nested inside a worksharing,
3818       // atomic, or explicit task region.
3819       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3820                           isOpenMPTaskingDirective(ParentRegion);
3821     } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3822       // OpenMP [2.16, Nesting of Regions]
3823       // A critical region may not be nested (closely or otherwise) inside a
3824       // critical region with the same name. Note that this restriction is not
3825       // sufficient to prevent deadlock.
3826       SourceLocation PreviousCriticalLoc;
3827       bool DeadLock = Stack->hasDirective(
3828           [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
3829                                               const DeclarationNameInfo &DNI,
3830                                               SourceLocation Loc) {
3831             if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
3832               PreviousCriticalLoc = Loc;
3833               return true;
3834             }
3835             return false;
3836           },
3837           false /* skip top directive */);
3838       if (DeadLock) {
3839         SemaRef.Diag(StartLoc,
3840                      diag::err_omp_prohibited_region_critical_same_name)
3841             << CurrentName.getName();
3842         if (PreviousCriticalLoc.isValid())
3843           SemaRef.Diag(PreviousCriticalLoc,
3844                        diag::note_omp_previous_critical_region);
3845         return true;
3846       }
3847     } else if (CurrentRegion == OMPD_barrier) {
3848       // OpenMP [2.16, Nesting of Regions]
3849       // A barrier region may not be closely nested inside a worksharing,
3850       // explicit task, critical, ordered, atomic, or master region.
3851       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3852                           isOpenMPTaskingDirective(ParentRegion) ||
3853                           ParentRegion == OMPD_master ||
3854                           ParentRegion == OMPD_critical ||
3855                           ParentRegion == OMPD_ordered;
3856     } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
3857                !isOpenMPParallelDirective(CurrentRegion) &&
3858                !isOpenMPTeamsDirective(CurrentRegion)) {
3859       // OpenMP [2.16, Nesting of Regions]
3860       // A worksharing region may not be closely nested inside a worksharing,
3861       // explicit task, critical, ordered, atomic, or master region.
3862       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3863                           isOpenMPTaskingDirective(ParentRegion) ||
3864                           ParentRegion == OMPD_master ||
3865                           ParentRegion == OMPD_critical ||
3866                           ParentRegion == OMPD_ordered;
3867       Recommend = ShouldBeInParallelRegion;
3868     } else if (CurrentRegion == OMPD_ordered) {
3869       // OpenMP [2.16, Nesting of Regions]
3870       // An ordered region may not be closely nested inside a critical,
3871       // atomic, or explicit task region.
3872       // An ordered region must be closely nested inside a loop region (or
3873       // parallel loop region) with an ordered clause.
3874       // OpenMP [2.8.1,simd Construct, Restrictions]
3875       // An ordered construct with the simd clause is the only OpenMP construct
3876       // that can appear in the simd region.
3877       NestingProhibited = ParentRegion == OMPD_critical ||
3878                           isOpenMPTaskingDirective(ParentRegion) ||
3879                           !(isOpenMPSimdDirective(ParentRegion) ||
3880                             Stack->isParentOrderedRegion());
3881       Recommend = ShouldBeInOrderedRegion;
3882     } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
3883       // OpenMP [2.16, Nesting of Regions]
3884       // If specified, a teams construct must be contained within a target
3885       // construct.
3886       NestingProhibited =
3887           (SemaRef.LangOpts.OpenMP <= 45 && ParentRegion != OMPD_target) ||
3888           (SemaRef.LangOpts.OpenMP >= 50 && ParentRegion != OMPD_unknown &&
3889            ParentRegion != OMPD_target);
3890       OrphanSeen = ParentRegion == OMPD_unknown;
3891       Recommend = ShouldBeInTargetRegion;
3892     }
3893     if (!NestingProhibited &&
3894         !isOpenMPTargetExecutionDirective(CurrentRegion) &&
3895         !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
3896         (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
3897       // OpenMP [2.16, Nesting of Regions]
3898       // distribute, parallel, parallel sections, parallel workshare, and the
3899       // parallel loop and parallel loop SIMD constructs are the only OpenMP
3900       // constructs that can be closely nested in the teams region.
3901       NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3902                           !isOpenMPDistributeDirective(CurrentRegion);
3903       Recommend = ShouldBeInParallelRegion;
3904     }
3905     if (!NestingProhibited &&
3906         isOpenMPNestingDistributeDirective(CurrentRegion)) {
3907       // OpenMP 4.5 [2.17 Nesting of Regions]
3908       // The region associated with the distribute construct must be strictly
3909       // nested inside a teams region
3910       NestingProhibited =
3911           (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
3912       Recommend = ShouldBeInTeamsRegion;
3913     }
3914     if (!NestingProhibited &&
3915         (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3916          isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3917       // OpenMP 4.5 [2.17 Nesting of Regions]
3918       // If a target, target update, target data, target enter data, or
3919       // target exit data construct is encountered during execution of a
3920       // target region, the behavior is unspecified.
3921       NestingProhibited = Stack->hasDirective(
3922           [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
3923                              SourceLocation) {
3924             if (isOpenMPTargetExecutionDirective(K)) {
3925               OffendingRegion = K;
3926               return true;
3927             }
3928             return false;
3929           },
3930           false /* don't skip top directive */);
3931       CloseNesting = false;
3932     }
3933     if (NestingProhibited) {
3934       if (OrphanSeen) {
3935         SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
3936             << getOpenMPDirectiveName(CurrentRegion) << Recommend;
3937       } else {
3938         SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
3939             << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3940             << Recommend << getOpenMPDirectiveName(CurrentRegion);
3941       }
3942       return true;
3943     }
3944   }
3945   return false;
3946 }
3947 
3948 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3949                            ArrayRef<OMPClause *> Clauses,
3950                            ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3951   bool ErrorFound = false;
3952   unsigned NamedModifiersNumber = 0;
3953   SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3954       OMPD_unknown + 1);
3955   SmallVector<SourceLocation, 4> NameModifierLoc;
3956   for (const OMPClause *C : Clauses) {
3957     if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3958       // At most one if clause without a directive-name-modifier can appear on
3959       // the directive.
3960       OpenMPDirectiveKind CurNM = IC->getNameModifier();
3961       if (FoundNameModifiers[CurNM]) {
3962         S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
3963             << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3964             << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3965         ErrorFound = true;
3966       } else if (CurNM != OMPD_unknown) {
3967         NameModifierLoc.push_back(IC->getNameModifierLoc());
3968         ++NamedModifiersNumber;
3969       }
3970       FoundNameModifiers[CurNM] = IC;
3971       if (CurNM == OMPD_unknown)
3972         continue;
3973       // Check if the specified name modifier is allowed for the current
3974       // directive.
3975       // At most one if clause with the particular directive-name-modifier can
3976       // appear on the directive.
3977       bool MatchFound = false;
3978       for (auto NM : AllowedNameModifiers) {
3979         if (CurNM == NM) {
3980           MatchFound = true;
3981           break;
3982         }
3983       }
3984       if (!MatchFound) {
3985         S.Diag(IC->getNameModifierLoc(),
3986                diag::err_omp_wrong_if_directive_name_modifier)
3987             << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3988         ErrorFound = true;
3989       }
3990     }
3991   }
3992   // If any if clause on the directive includes a directive-name-modifier then
3993   // all if clauses on the directive must include a directive-name-modifier.
3994   if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3995     if (NamedModifiersNumber == AllowedNameModifiers.size()) {
3996       S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
3997              diag::err_omp_no_more_if_clause);
3998     } else {
3999       std::string Values;
4000       std::string Sep(", ");
4001       unsigned AllowedCnt = 0;
4002       unsigned TotalAllowedNum =
4003           AllowedNameModifiers.size() - NamedModifiersNumber;
4004       for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
4005            ++Cnt) {
4006         OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
4007         if (!FoundNameModifiers[NM]) {
4008           Values += "'";
4009           Values += getOpenMPDirectiveName(NM);
4010           Values += "'";
4011           if (AllowedCnt + 2 == TotalAllowedNum)
4012             Values += " or ";
4013           else if (AllowedCnt + 1 != TotalAllowedNum)
4014             Values += Sep;
4015           ++AllowedCnt;
4016         }
4017       }
4018       S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
4019              diag::err_omp_unnamed_if_clause)
4020           << (TotalAllowedNum > 1) << Values;
4021     }
4022     for (SourceLocation Loc : NameModifierLoc) {
4023       S.Diag(Loc, diag::note_omp_previous_named_if_clause);
4024     }
4025     ErrorFound = true;
4026   }
4027   return ErrorFound;
4028 }
4029 
4030 static std::pair<ValueDecl *, bool>
4031 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
4032                SourceRange &ERange, bool AllowArraySection = false) {
4033   if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
4034       RefExpr->containsUnexpandedParameterPack())
4035     return std::make_pair(nullptr, true);
4036 
4037   // OpenMP [3.1, C/C++]
4038   //  A list item is a variable name.
4039   // OpenMP  [2.9.3.3, Restrictions, p.1]
4040   //  A variable that is part of another variable (as an array or
4041   //  structure element) cannot appear in a private clause.
4042   RefExpr = RefExpr->IgnoreParens();
4043   enum {
4044     NoArrayExpr = -1,
4045     ArraySubscript = 0,
4046     OMPArraySection = 1
4047   } IsArrayExpr = NoArrayExpr;
4048   if (AllowArraySection) {
4049     if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
4050       Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
4051       while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
4052         Base = TempASE->getBase()->IgnoreParenImpCasts();
4053       RefExpr = Base;
4054       IsArrayExpr = ArraySubscript;
4055     } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
4056       Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
4057       while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
4058         Base = TempOASE->getBase()->IgnoreParenImpCasts();
4059       while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
4060         Base = TempASE->getBase()->IgnoreParenImpCasts();
4061       RefExpr = Base;
4062       IsArrayExpr = OMPArraySection;
4063     }
4064   }
4065   ELoc = RefExpr->getExprLoc();
4066   ERange = RefExpr->getSourceRange();
4067   RefExpr = RefExpr->IgnoreParenImpCasts();
4068   auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
4069   auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
4070   if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
4071       (S.getCurrentThisType().isNull() || !ME ||
4072        !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
4073        !isa<FieldDecl>(ME->getMemberDecl()))) {
4074     if (IsArrayExpr != NoArrayExpr) {
4075       S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
4076                                                          << ERange;
4077     } else {
4078       S.Diag(ELoc,
4079              AllowArraySection
4080                  ? diag::err_omp_expected_var_name_member_expr_or_array_item
4081                  : diag::err_omp_expected_var_name_member_expr)
4082           << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
4083     }
4084     return std::make_pair(nullptr, false);
4085   }
4086   return std::make_pair(
4087       getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
4088 }
4089 
4090 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
4091                                  ArrayRef<OMPClause *> Clauses) {
4092   assert(!S.CurContext->isDependentContext() &&
4093          "Expected non-dependent context.");
4094   auto AllocateRange =
4095       llvm::make_filter_range(Clauses, OMPAllocateClause::classof);
4096   llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>>
4097       DeclToCopy;
4098   auto PrivateRange = llvm::make_filter_range(Clauses, [](const OMPClause *C) {
4099     return isOpenMPPrivate(C->getClauseKind());
4100   });
4101   for (OMPClause *Cl : PrivateRange) {
4102     MutableArrayRef<Expr *>::iterator I, It, Et;
4103     if (Cl->getClauseKind() == OMPC_private) {
4104       auto *PC = cast<OMPPrivateClause>(Cl);
4105       I = PC->private_copies().begin();
4106       It = PC->varlist_begin();
4107       Et = PC->varlist_end();
4108     } else if (Cl->getClauseKind() == OMPC_firstprivate) {
4109       auto *PC = cast<OMPFirstprivateClause>(Cl);
4110       I = PC->private_copies().begin();
4111       It = PC->varlist_begin();
4112       Et = PC->varlist_end();
4113     } else if (Cl->getClauseKind() == OMPC_lastprivate) {
4114       auto *PC = cast<OMPLastprivateClause>(Cl);
4115       I = PC->private_copies().begin();
4116       It = PC->varlist_begin();
4117       Et = PC->varlist_end();
4118     } else if (Cl->getClauseKind() == OMPC_linear) {
4119       auto *PC = cast<OMPLinearClause>(Cl);
4120       I = PC->privates().begin();
4121       It = PC->varlist_begin();
4122       Et = PC->varlist_end();
4123     } else if (Cl->getClauseKind() == OMPC_reduction) {
4124       auto *PC = cast<OMPReductionClause>(Cl);
4125       I = PC->privates().begin();
4126       It = PC->varlist_begin();
4127       Et = PC->varlist_end();
4128     } else if (Cl->getClauseKind() == OMPC_task_reduction) {
4129       auto *PC = cast<OMPTaskReductionClause>(Cl);
4130       I = PC->privates().begin();
4131       It = PC->varlist_begin();
4132       Et = PC->varlist_end();
4133     } else if (Cl->getClauseKind() == OMPC_in_reduction) {
4134       auto *PC = cast<OMPInReductionClause>(Cl);
4135       I = PC->privates().begin();
4136       It = PC->varlist_begin();
4137       Et = PC->varlist_end();
4138     } else {
4139       llvm_unreachable("Expected private clause.");
4140     }
4141     for (Expr *E : llvm::make_range(It, Et)) {
4142       if (!*I) {
4143         ++I;
4144         continue;
4145       }
4146       SourceLocation ELoc;
4147       SourceRange ERange;
4148       Expr *SimpleRefExpr = E;
4149       auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
4150                                 /*AllowArraySection=*/true);
4151       DeclToCopy.try_emplace(Res.first,
4152                              cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()));
4153       ++I;
4154     }
4155   }
4156   for (OMPClause *C : AllocateRange) {
4157     auto *AC = cast<OMPAllocateClause>(C);
4158     OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
4159         getAllocatorKind(S, Stack, AC->getAllocator());
4160     // OpenMP, 2.11.4 allocate Clause, Restrictions.
4161     // For task, taskloop or target directives, allocation requests to memory
4162     // allocators with the trait access set to thread result in unspecified
4163     // behavior.
4164     if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc &&
4165         (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
4166          isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()))) {
4167       S.Diag(AC->getAllocator()->getExprLoc(),
4168              diag::warn_omp_allocate_thread_on_task_target_directive)
4169           << getOpenMPDirectiveName(Stack->getCurrentDirective());
4170     }
4171     for (Expr *E : AC->varlists()) {
4172       SourceLocation ELoc;
4173       SourceRange ERange;
4174       Expr *SimpleRefExpr = E;
4175       auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange);
4176       ValueDecl *VD = Res.first;
4177       DSAStackTy::DSAVarData Data = Stack->getTopDSA(VD, /*FromParent=*/false);
4178       if (!isOpenMPPrivate(Data.CKind)) {
4179         S.Diag(E->getExprLoc(),
4180                diag::err_omp_expected_private_copy_for_allocate);
4181         continue;
4182       }
4183       VarDecl *PrivateVD = DeclToCopy[VD];
4184       if (checkPreviousOMPAllocateAttribute(S, Stack, E, PrivateVD,
4185                                             AllocatorKind, AC->getAllocator()))
4186         continue;
4187       applyOMPAllocateAttribute(S, PrivateVD, AllocatorKind, AC->getAllocator(),
4188                                 E->getSourceRange());
4189     }
4190   }
4191 }
4192 
4193 StmtResult Sema::ActOnOpenMPExecutableDirective(
4194     OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
4195     OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
4196     Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
4197   StmtResult Res = StmtError();
4198   // First check CancelRegion which is then used in checkNestingOfRegions.
4199   if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
4200       checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
4201                             StartLoc))
4202     return StmtError();
4203 
4204   llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
4205   VarsWithInheritedDSAType VarsWithInheritedDSA;
4206   bool ErrorFound = false;
4207   ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
4208   if (AStmt && !CurContext->isDependentContext()) {
4209     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4210 
4211     // Check default data sharing attributes for referenced variables.
4212     DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
4213     int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
4214     Stmt *S = AStmt;
4215     while (--ThisCaptureLevel >= 0)
4216       S = cast<CapturedStmt>(S)->getCapturedStmt();
4217     DSAChecker.Visit(S);
4218     if (!isOpenMPTargetDataManagementDirective(Kind) &&
4219         !isOpenMPTaskingDirective(Kind)) {
4220       // Visit subcaptures to generate implicit clauses for captured vars.
4221       auto *CS = cast<CapturedStmt>(AStmt);
4222       SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
4223       getOpenMPCaptureRegions(CaptureRegions, Kind);
4224       // Ignore outer tasking regions for target directives.
4225       if (CaptureRegions.size() > 1 && CaptureRegions.front() == OMPD_task)
4226         CS = cast<CapturedStmt>(CS->getCapturedStmt());
4227       DSAChecker.visitSubCaptures(CS);
4228     }
4229     if (DSAChecker.isErrorFound())
4230       return StmtError();
4231     // Generate list of implicitly defined firstprivate variables.
4232     VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
4233 
4234     SmallVector<Expr *, 4> ImplicitFirstprivates(
4235         DSAChecker.getImplicitFirstprivate().begin(),
4236         DSAChecker.getImplicitFirstprivate().end());
4237     SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(),
4238                                         DSAChecker.getImplicitMap().end());
4239     // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
4240     for (OMPClause *C : Clauses) {
4241       if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
4242         for (Expr *E : IRC->taskgroup_descriptors())
4243           if (E)
4244             ImplicitFirstprivates.emplace_back(E);
4245       }
4246     }
4247     if (!ImplicitFirstprivates.empty()) {
4248       if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
4249               ImplicitFirstprivates, SourceLocation(), SourceLocation(),
4250               SourceLocation())) {
4251         ClausesWithImplicit.push_back(Implicit);
4252         ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
4253                      ImplicitFirstprivates.size();
4254       } else {
4255         ErrorFound = true;
4256       }
4257     }
4258     if (!ImplicitMaps.empty()) {
4259       CXXScopeSpec MapperIdScopeSpec;
4260       DeclarationNameInfo MapperId;
4261       if (OMPClause *Implicit = ActOnOpenMPMapClause(
4262               llvm::None, llvm::None, MapperIdScopeSpec, MapperId,
4263               OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true, SourceLocation(),
4264               SourceLocation(), ImplicitMaps, OMPVarListLocTy())) {
4265         ClausesWithImplicit.emplace_back(Implicit);
4266         ErrorFound |=
4267             cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size();
4268       } else {
4269         ErrorFound = true;
4270       }
4271     }
4272   }
4273 
4274   llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
4275   switch (Kind) {
4276   case OMPD_parallel:
4277     Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
4278                                        EndLoc);
4279     AllowedNameModifiers.push_back(OMPD_parallel);
4280     break;
4281   case OMPD_simd:
4282     Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
4283                                    VarsWithInheritedDSA);
4284     break;
4285   case OMPD_for:
4286     Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
4287                                   VarsWithInheritedDSA);
4288     break;
4289   case OMPD_for_simd:
4290     Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
4291                                       EndLoc, VarsWithInheritedDSA);
4292     break;
4293   case OMPD_sections:
4294     Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
4295                                        EndLoc);
4296     break;
4297   case OMPD_section:
4298     assert(ClausesWithImplicit.empty() &&
4299            "No clauses are allowed for 'omp section' directive");
4300     Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
4301     break;
4302   case OMPD_single:
4303     Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
4304                                      EndLoc);
4305     break;
4306   case OMPD_master:
4307     assert(ClausesWithImplicit.empty() &&
4308            "No clauses are allowed for 'omp master' directive");
4309     Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
4310     break;
4311   case OMPD_critical:
4312     Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
4313                                        StartLoc, EndLoc);
4314     break;
4315   case OMPD_parallel_for:
4316     Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
4317                                           EndLoc, VarsWithInheritedDSA);
4318     AllowedNameModifiers.push_back(OMPD_parallel);
4319     break;
4320   case OMPD_parallel_for_simd:
4321     Res = ActOnOpenMPParallelForSimdDirective(
4322         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4323     AllowedNameModifiers.push_back(OMPD_parallel);
4324     break;
4325   case OMPD_parallel_sections:
4326     Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
4327                                                StartLoc, EndLoc);
4328     AllowedNameModifiers.push_back(OMPD_parallel);
4329     break;
4330   case OMPD_task:
4331     Res =
4332         ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
4333     AllowedNameModifiers.push_back(OMPD_task);
4334     break;
4335   case OMPD_taskyield:
4336     assert(ClausesWithImplicit.empty() &&
4337            "No clauses are allowed for 'omp taskyield' directive");
4338     assert(AStmt == nullptr &&
4339            "No associated statement allowed for 'omp taskyield' directive");
4340     Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
4341     break;
4342   case OMPD_barrier:
4343     assert(ClausesWithImplicit.empty() &&
4344            "No clauses are allowed for 'omp barrier' directive");
4345     assert(AStmt == nullptr &&
4346            "No associated statement allowed for 'omp barrier' directive");
4347     Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
4348     break;
4349   case OMPD_taskwait:
4350     assert(ClausesWithImplicit.empty() &&
4351            "No clauses are allowed for 'omp taskwait' directive");
4352     assert(AStmt == nullptr &&
4353            "No associated statement allowed for 'omp taskwait' directive");
4354     Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
4355     break;
4356   case OMPD_taskgroup:
4357     Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
4358                                         EndLoc);
4359     break;
4360   case OMPD_flush:
4361     assert(AStmt == nullptr &&
4362            "No associated statement allowed for 'omp flush' directive");
4363     Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
4364     break;
4365   case OMPD_ordered:
4366     Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
4367                                       EndLoc);
4368     break;
4369   case OMPD_atomic:
4370     Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
4371                                      EndLoc);
4372     break;
4373   case OMPD_teams:
4374     Res =
4375         ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
4376     break;
4377   case OMPD_target:
4378     Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
4379                                      EndLoc);
4380     AllowedNameModifiers.push_back(OMPD_target);
4381     break;
4382   case OMPD_target_parallel:
4383     Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
4384                                              StartLoc, EndLoc);
4385     AllowedNameModifiers.push_back(OMPD_target);
4386     AllowedNameModifiers.push_back(OMPD_parallel);
4387     break;
4388   case OMPD_target_parallel_for:
4389     Res = ActOnOpenMPTargetParallelForDirective(
4390         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4391     AllowedNameModifiers.push_back(OMPD_target);
4392     AllowedNameModifiers.push_back(OMPD_parallel);
4393     break;
4394   case OMPD_cancellation_point:
4395     assert(ClausesWithImplicit.empty() &&
4396            "No clauses are allowed for 'omp cancellation point' directive");
4397     assert(AStmt == nullptr && "No associated statement allowed for 'omp "
4398                                "cancellation point' directive");
4399     Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
4400     break;
4401   case OMPD_cancel:
4402     assert(AStmt == nullptr &&
4403            "No associated statement allowed for 'omp cancel' directive");
4404     Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
4405                                      CancelRegion);
4406     AllowedNameModifiers.push_back(OMPD_cancel);
4407     break;
4408   case OMPD_target_data:
4409     Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
4410                                          EndLoc);
4411     AllowedNameModifiers.push_back(OMPD_target_data);
4412     break;
4413   case OMPD_target_enter_data:
4414     Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
4415                                               EndLoc, AStmt);
4416     AllowedNameModifiers.push_back(OMPD_target_enter_data);
4417     break;
4418   case OMPD_target_exit_data:
4419     Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
4420                                              EndLoc, AStmt);
4421     AllowedNameModifiers.push_back(OMPD_target_exit_data);
4422     break;
4423   case OMPD_taskloop:
4424     Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
4425                                        EndLoc, VarsWithInheritedDSA);
4426     AllowedNameModifiers.push_back(OMPD_taskloop);
4427     break;
4428   case OMPD_taskloop_simd:
4429     Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
4430                                            EndLoc, VarsWithInheritedDSA);
4431     AllowedNameModifiers.push_back(OMPD_taskloop);
4432     break;
4433   case OMPD_distribute:
4434     Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
4435                                          EndLoc, VarsWithInheritedDSA);
4436     break;
4437   case OMPD_target_update:
4438     Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
4439                                            EndLoc, AStmt);
4440     AllowedNameModifiers.push_back(OMPD_target_update);
4441     break;
4442   case OMPD_distribute_parallel_for:
4443     Res = ActOnOpenMPDistributeParallelForDirective(
4444         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4445     AllowedNameModifiers.push_back(OMPD_parallel);
4446     break;
4447   case OMPD_distribute_parallel_for_simd:
4448     Res = ActOnOpenMPDistributeParallelForSimdDirective(
4449         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4450     AllowedNameModifiers.push_back(OMPD_parallel);
4451     break;
4452   case OMPD_distribute_simd:
4453     Res = ActOnOpenMPDistributeSimdDirective(
4454         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4455     break;
4456   case OMPD_target_parallel_for_simd:
4457     Res = ActOnOpenMPTargetParallelForSimdDirective(
4458         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4459     AllowedNameModifiers.push_back(OMPD_target);
4460     AllowedNameModifiers.push_back(OMPD_parallel);
4461     break;
4462   case OMPD_target_simd:
4463     Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
4464                                          EndLoc, VarsWithInheritedDSA);
4465     AllowedNameModifiers.push_back(OMPD_target);
4466     break;
4467   case OMPD_teams_distribute:
4468     Res = ActOnOpenMPTeamsDistributeDirective(
4469         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4470     break;
4471   case OMPD_teams_distribute_simd:
4472     Res = ActOnOpenMPTeamsDistributeSimdDirective(
4473         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4474     break;
4475   case OMPD_teams_distribute_parallel_for_simd:
4476     Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
4477         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4478     AllowedNameModifiers.push_back(OMPD_parallel);
4479     break;
4480   case OMPD_teams_distribute_parallel_for:
4481     Res = ActOnOpenMPTeamsDistributeParallelForDirective(
4482         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4483     AllowedNameModifiers.push_back(OMPD_parallel);
4484     break;
4485   case OMPD_target_teams:
4486     Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
4487                                           EndLoc);
4488     AllowedNameModifiers.push_back(OMPD_target);
4489     break;
4490   case OMPD_target_teams_distribute:
4491     Res = ActOnOpenMPTargetTeamsDistributeDirective(
4492         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4493     AllowedNameModifiers.push_back(OMPD_target);
4494     break;
4495   case OMPD_target_teams_distribute_parallel_for:
4496     Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
4497         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4498     AllowedNameModifiers.push_back(OMPD_target);
4499     AllowedNameModifiers.push_back(OMPD_parallel);
4500     break;
4501   case OMPD_target_teams_distribute_parallel_for_simd:
4502     Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
4503         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4504     AllowedNameModifiers.push_back(OMPD_target);
4505     AllowedNameModifiers.push_back(OMPD_parallel);
4506     break;
4507   case OMPD_target_teams_distribute_simd:
4508     Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
4509         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4510     AllowedNameModifiers.push_back(OMPD_target);
4511     break;
4512   case OMPD_declare_target:
4513   case OMPD_end_declare_target:
4514   case OMPD_threadprivate:
4515   case OMPD_allocate:
4516   case OMPD_declare_reduction:
4517   case OMPD_declare_mapper:
4518   case OMPD_declare_simd:
4519   case OMPD_requires:
4520   case OMPD_declare_variant:
4521     llvm_unreachable("OpenMP Directive is not allowed");
4522   case OMPD_unknown:
4523     llvm_unreachable("Unknown OpenMP directive");
4524   }
4525 
4526   ErrorFound = Res.isInvalid() || ErrorFound;
4527 
4528   // Check variables in the clauses if default(none) was specified.
4529   if (DSAStack->getDefaultDSA() == DSA_none) {
4530     DSAAttrChecker DSAChecker(DSAStack, *this, nullptr);
4531     for (OMPClause *C : Clauses) {
4532       switch (C->getClauseKind()) {
4533       case OMPC_num_threads:
4534       case OMPC_dist_schedule:
4535         // Do not analyse if no parent teams directive.
4536         if (isOpenMPTeamsDirective(DSAStack->getCurrentDirective()))
4537           break;
4538         continue;
4539       case OMPC_if:
4540         if (isOpenMPTeamsDirective(DSAStack->getCurrentDirective()) &&
4541             cast<OMPIfClause>(C)->getNameModifier() != OMPD_target)
4542           break;
4543         continue;
4544       case OMPC_schedule:
4545         break;
4546       case OMPC_ordered:
4547       case OMPC_device:
4548       case OMPC_num_teams:
4549       case OMPC_thread_limit:
4550       case OMPC_priority:
4551       case OMPC_grainsize:
4552       case OMPC_num_tasks:
4553       case OMPC_hint:
4554       case OMPC_collapse:
4555       case OMPC_safelen:
4556       case OMPC_simdlen:
4557       case OMPC_final:
4558       case OMPC_default:
4559       case OMPC_proc_bind:
4560       case OMPC_private:
4561       case OMPC_firstprivate:
4562       case OMPC_lastprivate:
4563       case OMPC_shared:
4564       case OMPC_reduction:
4565       case OMPC_task_reduction:
4566       case OMPC_in_reduction:
4567       case OMPC_linear:
4568       case OMPC_aligned:
4569       case OMPC_copyin:
4570       case OMPC_copyprivate:
4571       case OMPC_nowait:
4572       case OMPC_untied:
4573       case OMPC_mergeable:
4574       case OMPC_allocate:
4575       case OMPC_read:
4576       case OMPC_write:
4577       case OMPC_update:
4578       case OMPC_capture:
4579       case OMPC_seq_cst:
4580       case OMPC_depend:
4581       case OMPC_threads:
4582       case OMPC_simd:
4583       case OMPC_map:
4584       case OMPC_nogroup:
4585       case OMPC_defaultmap:
4586       case OMPC_to:
4587       case OMPC_from:
4588       case OMPC_use_device_ptr:
4589       case OMPC_is_device_ptr:
4590         continue;
4591       case OMPC_allocator:
4592       case OMPC_flush:
4593       case OMPC_threadprivate:
4594       case OMPC_uniform:
4595       case OMPC_unknown:
4596       case OMPC_unified_address:
4597       case OMPC_unified_shared_memory:
4598       case OMPC_reverse_offload:
4599       case OMPC_dynamic_allocators:
4600       case OMPC_atomic_default_mem_order:
4601       case OMPC_device_type:
4602         llvm_unreachable("Unexpected clause");
4603       }
4604       for (Stmt *CC : C->children()) {
4605         if (CC)
4606           DSAChecker.Visit(CC);
4607       }
4608     }
4609     for (auto &P : DSAChecker.getVarsWithInheritedDSA())
4610       VarsWithInheritedDSA[P.getFirst()] = P.getSecond();
4611   }
4612   for (const auto &P : VarsWithInheritedDSA) {
4613     if (P.getFirst()->isImplicit() || isa<OMPCapturedExprDecl>(P.getFirst()))
4614       continue;
4615     ErrorFound = true;
4616     Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
4617         << P.first << P.second->getSourceRange();
4618     Diag(DSAStack->getDefaultDSALocation(), diag::note_omp_default_dsa_none);
4619   }
4620 
4621   if (!AllowedNameModifiers.empty())
4622     ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
4623                  ErrorFound;
4624 
4625   if (ErrorFound)
4626     return StmtError();
4627 
4628   if (!(Res.getAs<OMPExecutableDirective>()->isStandaloneDirective())) {
4629     Res.getAs<OMPExecutableDirective>()
4630         ->getStructuredBlock()
4631         ->setIsOMPStructuredBlock(true);
4632   }
4633 
4634   if (!CurContext->isDependentContext() &&
4635       isOpenMPTargetExecutionDirective(Kind) &&
4636       !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() ||
4637         DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() ||
4638         DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() ||
4639         DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) {
4640     // Register target to DSA Stack.
4641     DSAStack->addTargetDirLocation(StartLoc);
4642   }
4643 
4644   return Res;
4645 }
4646 
4647 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
4648     DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
4649     ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
4650     ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
4651     ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
4652   assert(Aligneds.size() == Alignments.size());
4653   assert(Linears.size() == LinModifiers.size());
4654   assert(Linears.size() == Steps.size());
4655   if (!DG || DG.get().isNull())
4656     return DeclGroupPtrTy();
4657 
4658   const int SimdId = 0;
4659   if (!DG.get().isSingleDecl()) {
4660     Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant)
4661         << SimdId;
4662     return DG;
4663   }
4664   Decl *ADecl = DG.get().getSingleDecl();
4665   if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
4666     ADecl = FTD->getTemplatedDecl();
4667 
4668   auto *FD = dyn_cast<FunctionDecl>(ADecl);
4669   if (!FD) {
4670     Diag(ADecl->getLocation(), diag::err_omp_function_expected) << SimdId;
4671     return DeclGroupPtrTy();
4672   }
4673 
4674   // OpenMP [2.8.2, declare simd construct, Description]
4675   // The parameter of the simdlen clause must be a constant positive integer
4676   // expression.
4677   ExprResult SL;
4678   if (Simdlen)
4679     SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
4680   // OpenMP [2.8.2, declare simd construct, Description]
4681   // The special this pointer can be used as if was one of the arguments to the
4682   // function in any of the linear, aligned, or uniform clauses.
4683   // The uniform clause declares one or more arguments to have an invariant
4684   // value for all concurrent invocations of the function in the execution of a
4685   // single SIMD loop.
4686   llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
4687   const Expr *UniformedLinearThis = nullptr;
4688   for (const Expr *E : Uniforms) {
4689     E = E->IgnoreParenImpCasts();
4690     if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
4691       if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
4692         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
4693             FD->getParamDecl(PVD->getFunctionScopeIndex())
4694                     ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
4695           UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E);
4696           continue;
4697         }
4698     if (isa<CXXThisExpr>(E)) {
4699       UniformedLinearThis = E;
4700       continue;
4701     }
4702     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
4703         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
4704   }
4705   // OpenMP [2.8.2, declare simd construct, Description]
4706   // The aligned clause declares that the object to which each list item points
4707   // is aligned to the number of bytes expressed in the optional parameter of
4708   // the aligned clause.
4709   // The special this pointer can be used as if was one of the arguments to the
4710   // function in any of the linear, aligned, or uniform clauses.
4711   // The type of list items appearing in the aligned clause must be array,
4712   // pointer, reference to array, or reference to pointer.
4713   llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
4714   const Expr *AlignedThis = nullptr;
4715   for (const Expr *E : Aligneds) {
4716     E = E->IgnoreParenImpCasts();
4717     if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
4718       if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
4719         const VarDecl *CanonPVD = PVD->getCanonicalDecl();
4720         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
4721             FD->getParamDecl(PVD->getFunctionScopeIndex())
4722                     ->getCanonicalDecl() == CanonPVD) {
4723           // OpenMP  [2.8.1, simd construct, Restrictions]
4724           // A list-item cannot appear in more than one aligned clause.
4725           if (AlignedArgs.count(CanonPVD) > 0) {
4726             Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
4727                 << 1 << E->getSourceRange();
4728             Diag(AlignedArgs[CanonPVD]->getExprLoc(),
4729                  diag::note_omp_explicit_dsa)
4730                 << getOpenMPClauseName(OMPC_aligned);
4731             continue;
4732           }
4733           AlignedArgs[CanonPVD] = E;
4734           QualType QTy = PVD->getType()
4735                              .getNonReferenceType()
4736                              .getUnqualifiedType()
4737                              .getCanonicalType();
4738           const Type *Ty = QTy.getTypePtrOrNull();
4739           if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
4740             Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
4741                 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
4742             Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
4743           }
4744           continue;
4745         }
4746       }
4747     if (isa<CXXThisExpr>(E)) {
4748       if (AlignedThis) {
4749         Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
4750             << 2 << E->getSourceRange();
4751         Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
4752             << getOpenMPClauseName(OMPC_aligned);
4753       }
4754       AlignedThis = E;
4755       continue;
4756     }
4757     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
4758         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
4759   }
4760   // The optional parameter of the aligned clause, alignment, must be a constant
4761   // positive integer expression. If no optional parameter is specified,
4762   // implementation-defined default alignments for SIMD instructions on the
4763   // target platforms are assumed.
4764   SmallVector<const Expr *, 4> NewAligns;
4765   for (Expr *E : Alignments) {
4766     ExprResult Align;
4767     if (E)
4768       Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
4769     NewAligns.push_back(Align.get());
4770   }
4771   // OpenMP [2.8.2, declare simd construct, Description]
4772   // The linear clause declares one or more list items to be private to a SIMD
4773   // lane and to have a linear relationship with respect to the iteration space
4774   // of a loop.
4775   // The special this pointer can be used as if was one of the arguments to the
4776   // function in any of the linear, aligned, or uniform clauses.
4777   // When a linear-step expression is specified in a linear clause it must be
4778   // either a constant integer expression or an integer-typed parameter that is
4779   // specified in a uniform clause on the directive.
4780   llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
4781   const bool IsUniformedThis = UniformedLinearThis != nullptr;
4782   auto MI = LinModifiers.begin();
4783   for (const Expr *E : Linears) {
4784     auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
4785     ++MI;
4786     E = E->IgnoreParenImpCasts();
4787     if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
4788       if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
4789         const VarDecl *CanonPVD = PVD->getCanonicalDecl();
4790         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
4791             FD->getParamDecl(PVD->getFunctionScopeIndex())
4792                     ->getCanonicalDecl() == CanonPVD) {
4793           // OpenMP  [2.15.3.7, linear Clause, Restrictions]
4794           // A list-item cannot appear in more than one linear clause.
4795           if (LinearArgs.count(CanonPVD) > 0) {
4796             Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
4797                 << getOpenMPClauseName(OMPC_linear)
4798                 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
4799             Diag(LinearArgs[CanonPVD]->getExprLoc(),
4800                  diag::note_omp_explicit_dsa)
4801                 << getOpenMPClauseName(OMPC_linear);
4802             continue;
4803           }
4804           // Each argument can appear in at most one uniform or linear clause.
4805           if (UniformedArgs.count(CanonPVD) > 0) {
4806             Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
4807                 << getOpenMPClauseName(OMPC_linear)
4808                 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
4809             Diag(UniformedArgs[CanonPVD]->getExprLoc(),
4810                  diag::note_omp_explicit_dsa)
4811                 << getOpenMPClauseName(OMPC_uniform);
4812             continue;
4813           }
4814           LinearArgs[CanonPVD] = E;
4815           if (E->isValueDependent() || E->isTypeDependent() ||
4816               E->isInstantiationDependent() ||
4817               E->containsUnexpandedParameterPack())
4818             continue;
4819           (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
4820                                       PVD->getOriginalType());
4821           continue;
4822         }
4823       }
4824     if (isa<CXXThisExpr>(E)) {
4825       if (UniformedLinearThis) {
4826         Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
4827             << getOpenMPClauseName(OMPC_linear)
4828             << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
4829             << E->getSourceRange();
4830         Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
4831             << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
4832                                                    : OMPC_linear);
4833         continue;
4834       }
4835       UniformedLinearThis = E;
4836       if (E->isValueDependent() || E->isTypeDependent() ||
4837           E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
4838         continue;
4839       (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
4840                                   E->getType());
4841       continue;
4842     }
4843     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
4844         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
4845   }
4846   Expr *Step = nullptr;
4847   Expr *NewStep = nullptr;
4848   SmallVector<Expr *, 4> NewSteps;
4849   for (Expr *E : Steps) {
4850     // Skip the same step expression, it was checked already.
4851     if (Step == E || !E) {
4852       NewSteps.push_back(E ? NewStep : nullptr);
4853       continue;
4854     }
4855     Step = E;
4856     if (const auto *DRE = dyn_cast<DeclRefExpr>(Step))
4857       if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
4858         const VarDecl *CanonPVD = PVD->getCanonicalDecl();
4859         if (UniformedArgs.count(CanonPVD) == 0) {
4860           Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
4861               << Step->getSourceRange();
4862         } else if (E->isValueDependent() || E->isTypeDependent() ||
4863                    E->isInstantiationDependent() ||
4864                    E->containsUnexpandedParameterPack() ||
4865                    CanonPVD->getType()->hasIntegerRepresentation()) {
4866           NewSteps.push_back(Step);
4867         } else {
4868           Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
4869               << Step->getSourceRange();
4870         }
4871         continue;
4872       }
4873     NewStep = Step;
4874     if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
4875         !Step->isInstantiationDependent() &&
4876         !Step->containsUnexpandedParameterPack()) {
4877       NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
4878                     .get();
4879       if (NewStep)
4880         NewStep = VerifyIntegerConstantExpression(NewStep).get();
4881     }
4882     NewSteps.push_back(NewStep);
4883   }
4884   auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
4885       Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
4886       Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
4887       const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
4888       const_cast<Expr **>(Linears.data()), Linears.size(),
4889       const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
4890       NewSteps.data(), NewSteps.size(), SR);
4891   ADecl->addAttr(NewAttr);
4892   return DG;
4893 }
4894 
4895 Optional<std::pair<FunctionDecl *, Expr *>>
4896 Sema::checkOpenMPDeclareVariantFunction(Sema::DeclGroupPtrTy DG,
4897                                         Expr *VariantRef, SourceRange SR) {
4898   if (!DG || DG.get().isNull())
4899     return None;
4900 
4901   const int VariantId = 1;
4902   // Must be applied only to single decl.
4903   if (!DG.get().isSingleDecl()) {
4904     Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant)
4905         << VariantId << SR;
4906     return None;
4907   }
4908   Decl *ADecl = DG.get().getSingleDecl();
4909   if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
4910     ADecl = FTD->getTemplatedDecl();
4911 
4912   // Decl must be a function.
4913   auto *FD = dyn_cast<FunctionDecl>(ADecl);
4914   if (!FD) {
4915     Diag(ADecl->getLocation(), diag::err_omp_function_expected)
4916         << VariantId << SR;
4917     return None;
4918   }
4919 
4920   auto &&HasMultiVersionAttributes = [](const FunctionDecl *FD) {
4921     return FD->hasAttrs() &&
4922            (FD->hasAttr<CPUDispatchAttr>() || FD->hasAttr<CPUSpecificAttr>() ||
4923             FD->hasAttr<TargetAttr>());
4924   };
4925   // OpenMP is not compatible with CPU-specific attributes.
4926   if (HasMultiVersionAttributes(FD)) {
4927     Diag(FD->getLocation(), diag::err_omp_declare_variant_incompat_attributes)
4928         << SR;
4929     return None;
4930   }
4931 
4932   // Allow #pragma omp declare variant only if the function is not used.
4933   if (FD->isUsed(false)) {
4934     Diag(SR.getBegin(), diag::err_omp_declare_variant_after_used)
4935         << FD->getLocation();
4936     return None;
4937   }
4938 
4939   // The VariantRef must point to function.
4940   if (!VariantRef) {
4941     Diag(SR.getBegin(), diag::err_omp_function_expected) << VariantId;
4942     return None;
4943   }
4944 
4945   // Do not check templates, wait until instantiation.
4946   if (VariantRef->isTypeDependent() || VariantRef->isValueDependent() ||
4947       VariantRef->containsUnexpandedParameterPack() ||
4948       VariantRef->isInstantiationDependent() || FD->isDependentContext())
4949     return std::make_pair(FD, VariantRef);
4950 
4951   // Convert VariantRef expression to the type of the original function to
4952   // resolve possible conflicts.
4953   ExprResult VariantRefCast;
4954   if (LangOpts.CPlusPlus) {
4955     QualType FnPtrType;
4956     auto *Method = dyn_cast<CXXMethodDecl>(FD);
4957     if (Method && !Method->isStatic()) {
4958       const Type *ClassType =
4959           Context.getTypeDeclType(Method->getParent()).getTypePtr();
4960       FnPtrType = Context.getMemberPointerType(FD->getType(), ClassType);
4961       ExprResult ER;
4962       {
4963         // Build adrr_of unary op to correctly handle type checks for member
4964         // functions.
4965         Sema::TentativeAnalysisScope Trap(*this);
4966         ER = CreateBuiltinUnaryOp(VariantRef->getBeginLoc(), UO_AddrOf,
4967                                   VariantRef);
4968       }
4969       if (!ER.isUsable()) {
4970         Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected)
4971             << VariantId << VariantRef->getSourceRange();
4972         return None;
4973       }
4974       VariantRef = ER.get();
4975     } else {
4976       FnPtrType = Context.getPointerType(FD->getType());
4977     }
4978     ImplicitConversionSequence ICS =
4979         TryImplicitConversion(VariantRef, FnPtrType.getUnqualifiedType(),
4980                               /*SuppressUserConversions=*/false,
4981                               /*AllowExplicit=*/false,
4982                               /*InOverloadResolution=*/false,
4983                               /*CStyle=*/false,
4984                               /*AllowObjCWritebackConversion=*/false);
4985     if (ICS.isFailure()) {
4986       Diag(VariantRef->getExprLoc(),
4987            diag::err_omp_declare_variant_incompat_types)
4988           << VariantRef->getType() << FnPtrType << VariantRef->getSourceRange();
4989       return None;
4990     }
4991     VariantRefCast = PerformImplicitConversion(
4992         VariantRef, FnPtrType.getUnqualifiedType(), AA_Converting);
4993     if (!VariantRefCast.isUsable())
4994       return None;
4995     // Drop previously built artificial addr_of unary op for member functions.
4996     if (Method && !Method->isStatic()) {
4997       Expr *PossibleAddrOfVariantRef = VariantRefCast.get();
4998       if (auto *UO = dyn_cast<UnaryOperator>(
4999               PossibleAddrOfVariantRef->IgnoreImplicit()))
5000         VariantRefCast = UO->getSubExpr();
5001     }
5002   } else {
5003     VariantRefCast = VariantRef;
5004   }
5005 
5006   ExprResult ER = CheckPlaceholderExpr(VariantRefCast.get());
5007   if (!ER.isUsable() ||
5008       !ER.get()->IgnoreParenImpCasts()->getType()->isFunctionType()) {
5009     Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected)
5010         << VariantId << VariantRef->getSourceRange();
5011     return None;
5012   }
5013 
5014   // The VariantRef must point to function.
5015   auto *DRE = dyn_cast<DeclRefExpr>(ER.get()->IgnoreParenImpCasts());
5016   if (!DRE) {
5017     Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected)
5018         << VariantId << VariantRef->getSourceRange();
5019     return None;
5020   }
5021   auto *NewFD = dyn_cast_or_null<FunctionDecl>(DRE->getDecl());
5022   if (!NewFD) {
5023     Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected)
5024         << VariantId << VariantRef->getSourceRange();
5025     return None;
5026   }
5027 
5028   // Check if variant function is not marked with declare variant directive.
5029   if (NewFD->hasAttrs() && NewFD->hasAttr<OMPDeclareVariantAttr>()) {
5030     Diag(VariantRef->getExprLoc(),
5031          diag::warn_omp_declare_variant_marked_as_declare_variant)
5032         << VariantRef->getSourceRange();
5033     SourceRange SR =
5034         NewFD->specific_attr_begin<OMPDeclareVariantAttr>()->getRange();
5035     Diag(SR.getBegin(), diag::note_omp_marked_declare_variant_here) << SR;
5036     return None;
5037   }
5038 
5039   enum DoesntSupport {
5040     VirtFuncs = 1,
5041     Constructors = 3,
5042     Destructors = 4,
5043     DeletedFuncs = 5,
5044     DefaultedFuncs = 6,
5045     ConstexprFuncs = 7,
5046     ConstevalFuncs = 8,
5047   };
5048   if (const auto *CXXFD = dyn_cast<CXXMethodDecl>(FD)) {
5049     if (CXXFD->isVirtual()) {
5050       Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
5051           << VirtFuncs;
5052       return None;
5053     }
5054 
5055     if (isa<CXXConstructorDecl>(FD)) {
5056       Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
5057           << Constructors;
5058       return None;
5059     }
5060 
5061     if (isa<CXXDestructorDecl>(FD)) {
5062       Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
5063           << Destructors;
5064       return None;
5065     }
5066   }
5067 
5068   if (FD->isDeleted()) {
5069     Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
5070         << DeletedFuncs;
5071     return None;
5072   }
5073 
5074   if (FD->isDefaulted()) {
5075     Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
5076         << DefaultedFuncs;
5077     return None;
5078   }
5079 
5080   if (FD->isConstexpr()) {
5081     Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
5082         << (NewFD->isConsteval() ? ConstevalFuncs : ConstexprFuncs);
5083     return None;
5084   }
5085 
5086   // Check general compatibility.
5087   if (areMultiversionVariantFunctionsCompatible(
5088           FD, NewFD, PDiag(diag::err_omp_declare_variant_noproto),
5089           PartialDiagnosticAt(
5090               SR.getBegin(),
5091               PDiag(diag::note_omp_declare_variant_specified_here) << SR),
5092           PartialDiagnosticAt(
5093               VariantRef->getExprLoc(),
5094               PDiag(diag::err_omp_declare_variant_doesnt_support)),
5095           PartialDiagnosticAt(VariantRef->getExprLoc(),
5096                               PDiag(diag::err_omp_declare_variant_diff)
5097                                   << FD->getLocation()),
5098           /*TemplatesSupported=*/true, /*ConstexprSupported=*/false))
5099     return None;
5100   return std::make_pair(FD, cast<Expr>(DRE));
5101 }
5102 
5103 void Sema::ActOnOpenMPDeclareVariantDirective(FunctionDecl *FD,
5104                                               Expr *VariantRef,
5105                                               SourceRange SR) {
5106   auto *NewAttr =
5107       OMPDeclareVariantAttr::CreateImplicit(Context, VariantRef, SR);
5108   FD->addAttr(NewAttr);
5109 }
5110 
5111 void Sema::markOpenMPDeclareVariantFuncsReferenced(SourceLocation Loc,
5112                                                    FunctionDecl *Func,
5113                                                    bool MightBeOdrUse) {
5114   assert(LangOpts.OpenMP && "Expected OpenMP mode.");
5115 
5116   if (!Func->isDependentContext() && Func->hasAttrs()) {
5117     for (OMPDeclareVariantAttr *A :
5118          Func->specific_attrs<OMPDeclareVariantAttr>()) {
5119       // TODO: add checks for active OpenMP context where possible.
5120       Expr *VariantRef = A->getVariantFuncRef();
5121       auto *DRE = dyn_cast<DeclRefExpr>(VariantRef->IgnoreParenImpCasts());
5122       auto *F = cast<FunctionDecl>(DRE->getDecl());
5123       if (!F->isDefined() && F->isTemplateInstantiation())
5124         InstantiateFunctionDefinition(Loc, F->getFirstDecl());
5125       MarkFunctionReferenced(Loc, F, MightBeOdrUse);
5126     }
5127   }
5128 }
5129 
5130 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
5131                                               Stmt *AStmt,
5132                                               SourceLocation StartLoc,
5133                                               SourceLocation EndLoc) {
5134   if (!AStmt)
5135     return StmtError();
5136 
5137   auto *CS = cast<CapturedStmt>(AStmt);
5138   // 1.2.2 OpenMP Language Terminology
5139   // Structured block - An executable statement with a single entry at the
5140   // top and a single exit at the bottom.
5141   // The point of exit cannot be a branch out of the structured block.
5142   // longjmp() and throw() must not violate the entry/exit criteria.
5143   CS->getCapturedDecl()->setNothrow();
5144 
5145   setFunctionHasBranchProtectedScope();
5146 
5147   return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5148                                       DSAStack->isCancelRegion());
5149 }
5150 
5151 namespace {
5152 /// Iteration space of a single for loop.
5153 struct LoopIterationSpace final {
5154   /// True if the condition operator is the strict compare operator (<, > or
5155   /// !=).
5156   bool IsStrictCompare = false;
5157   /// Condition of the loop.
5158   Expr *PreCond = nullptr;
5159   /// This expression calculates the number of iterations in the loop.
5160   /// It is always possible to calculate it before starting the loop.
5161   Expr *NumIterations = nullptr;
5162   /// The loop counter variable.
5163   Expr *CounterVar = nullptr;
5164   /// Private loop counter variable.
5165   Expr *PrivateCounterVar = nullptr;
5166   /// This is initializer for the initial value of #CounterVar.
5167   Expr *CounterInit = nullptr;
5168   /// This is step for the #CounterVar used to generate its update:
5169   /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
5170   Expr *CounterStep = nullptr;
5171   /// Should step be subtracted?
5172   bool Subtract = false;
5173   /// Source range of the loop init.
5174   SourceRange InitSrcRange;
5175   /// Source range of the loop condition.
5176   SourceRange CondSrcRange;
5177   /// Source range of the loop increment.
5178   SourceRange IncSrcRange;
5179   /// Minimum value that can have the loop control variable. Used to support
5180   /// non-rectangular loops. Applied only for LCV with the non-iterator types,
5181   /// since only such variables can be used in non-loop invariant expressions.
5182   Expr *MinValue = nullptr;
5183   /// Maximum value that can have the loop control variable. Used to support
5184   /// non-rectangular loops. Applied only for LCV with the non-iterator type,
5185   /// since only such variables can be used in non-loop invariant expressions.
5186   Expr *MaxValue = nullptr;
5187   /// true, if the lower bound depends on the outer loop control var.
5188   bool IsNonRectangularLB = false;
5189   /// true, if the upper bound depends on the outer loop control var.
5190   bool IsNonRectangularUB = false;
5191   /// Index of the loop this loop depends on and forms non-rectangular loop
5192   /// nest.
5193   unsigned LoopDependentIdx = 0;
5194   /// Final condition for the non-rectangular loop nest support. It is used to
5195   /// check that the number of iterations for this particular counter must be
5196   /// finished.
5197   Expr *FinalCondition = nullptr;
5198 };
5199 
5200 /// Helper class for checking canonical form of the OpenMP loops and
5201 /// extracting iteration space of each loop in the loop nest, that will be used
5202 /// for IR generation.
5203 class OpenMPIterationSpaceChecker {
5204   /// Reference to Sema.
5205   Sema &SemaRef;
5206   /// Data-sharing stack.
5207   DSAStackTy &Stack;
5208   /// A location for diagnostics (when there is no some better location).
5209   SourceLocation DefaultLoc;
5210   /// A location for diagnostics (when increment is not compatible).
5211   SourceLocation ConditionLoc;
5212   /// A source location for referring to loop init later.
5213   SourceRange InitSrcRange;
5214   /// A source location for referring to condition later.
5215   SourceRange ConditionSrcRange;
5216   /// A source location for referring to increment later.
5217   SourceRange IncrementSrcRange;
5218   /// Loop variable.
5219   ValueDecl *LCDecl = nullptr;
5220   /// Reference to loop variable.
5221   Expr *LCRef = nullptr;
5222   /// Lower bound (initializer for the var).
5223   Expr *LB = nullptr;
5224   /// Upper bound.
5225   Expr *UB = nullptr;
5226   /// Loop step (increment).
5227   Expr *Step = nullptr;
5228   /// This flag is true when condition is one of:
5229   ///   Var <  UB
5230   ///   Var <= UB
5231   ///   UB  >  Var
5232   ///   UB  >= Var
5233   /// This will have no value when the condition is !=
5234   llvm::Optional<bool> TestIsLessOp;
5235   /// This flag is true when condition is strict ( < or > ).
5236   bool TestIsStrictOp = false;
5237   /// This flag is true when step is subtracted on each iteration.
5238   bool SubtractStep = false;
5239   /// The outer loop counter this loop depends on (if any).
5240   const ValueDecl *DepDecl = nullptr;
5241   /// Contains number of loop (starts from 1) on which loop counter init
5242   /// expression of this loop depends on.
5243   Optional<unsigned> InitDependOnLC;
5244   /// Contains number of loop (starts from 1) on which loop counter condition
5245   /// expression of this loop depends on.
5246   Optional<unsigned> CondDependOnLC;
5247   /// Checks if the provide statement depends on the loop counter.
5248   Optional<unsigned> doesDependOnLoopCounter(const Stmt *S, bool IsInitializer);
5249   /// Original condition required for checking of the exit condition for
5250   /// non-rectangular loop.
5251   Expr *Condition = nullptr;
5252 
5253 public:
5254   OpenMPIterationSpaceChecker(Sema &SemaRef, DSAStackTy &Stack,
5255                               SourceLocation DefaultLoc)
5256       : SemaRef(SemaRef), Stack(Stack), DefaultLoc(DefaultLoc),
5257         ConditionLoc(DefaultLoc) {}
5258   /// Check init-expr for canonical loop form and save loop counter
5259   /// variable - #Var and its initialization value - #LB.
5260   bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
5261   /// Check test-expr for canonical form, save upper-bound (#UB), flags
5262   /// for less/greater and for strict/non-strict comparison.
5263   bool checkAndSetCond(Expr *S);
5264   /// Check incr-expr for canonical loop form and return true if it
5265   /// does not conform, otherwise save loop step (#Step).
5266   bool checkAndSetInc(Expr *S);
5267   /// Return the loop counter variable.
5268   ValueDecl *getLoopDecl() const { return LCDecl; }
5269   /// Return the reference expression to loop counter variable.
5270   Expr *getLoopDeclRefExpr() const { return LCRef; }
5271   /// Source range of the loop init.
5272   SourceRange getInitSrcRange() const { return InitSrcRange; }
5273   /// Source range of the loop condition.
5274   SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
5275   /// Source range of the loop increment.
5276   SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
5277   /// True if the step should be subtracted.
5278   bool shouldSubtractStep() const { return SubtractStep; }
5279   /// True, if the compare operator is strict (<, > or !=).
5280   bool isStrictTestOp() const { return TestIsStrictOp; }
5281   /// Build the expression to calculate the number of iterations.
5282   Expr *buildNumIterations(
5283       Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType,
5284       llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
5285   /// Build the precondition expression for the loops.
5286   Expr *
5287   buildPreCond(Scope *S, Expr *Cond,
5288                llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
5289   /// Build reference expression to the counter be used for codegen.
5290   DeclRefExpr *
5291   buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
5292                   DSAStackTy &DSA) const;
5293   /// Build reference expression to the private counter be used for
5294   /// codegen.
5295   Expr *buildPrivateCounterVar() const;
5296   /// Build initialization of the counter be used for codegen.
5297   Expr *buildCounterInit() const;
5298   /// Build step of the counter be used for codegen.
5299   Expr *buildCounterStep() const;
5300   /// Build loop data with counter value for depend clauses in ordered
5301   /// directives.
5302   Expr *
5303   buildOrderedLoopData(Scope *S, Expr *Counter,
5304                        llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
5305                        SourceLocation Loc, Expr *Inc = nullptr,
5306                        OverloadedOperatorKind OOK = OO_Amp);
5307   /// Builds the minimum value for the loop counter.
5308   std::pair<Expr *, Expr *> buildMinMaxValues(
5309       Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
5310   /// Builds final condition for the non-rectangular loops.
5311   Expr *buildFinalCondition(Scope *S) const;
5312   /// Return true if any expression is dependent.
5313   bool dependent() const;
5314   /// Returns true if the initializer forms non-rectangular loop.
5315   bool doesInitDependOnLC() const { return InitDependOnLC.hasValue(); }
5316   /// Returns true if the condition forms non-rectangular loop.
5317   bool doesCondDependOnLC() const { return CondDependOnLC.hasValue(); }
5318   /// Returns index of the loop we depend on (starting from 1), or 0 otherwise.
5319   unsigned getLoopDependentIdx() const {
5320     return InitDependOnLC.getValueOr(CondDependOnLC.getValueOr(0));
5321   }
5322 
5323 private:
5324   /// Check the right-hand side of an assignment in the increment
5325   /// expression.
5326   bool checkAndSetIncRHS(Expr *RHS);
5327   /// Helper to set loop counter variable and its initializer.
5328   bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB,
5329                       bool EmitDiags);
5330   /// Helper to set upper bound.
5331   bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp,
5332              SourceRange SR, SourceLocation SL);
5333   /// Helper to set loop increment.
5334   bool setStep(Expr *NewStep, bool Subtract);
5335 };
5336 
5337 bool OpenMPIterationSpaceChecker::dependent() const {
5338   if (!LCDecl) {
5339     assert(!LB && !UB && !Step);
5340     return false;
5341   }
5342   return LCDecl->getType()->isDependentType() ||
5343          (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
5344          (Step && Step->isValueDependent());
5345 }
5346 
5347 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
5348                                                  Expr *NewLCRefExpr,
5349                                                  Expr *NewLB, bool EmitDiags) {
5350   // State consistency checking to ensure correct usage.
5351   assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
5352          UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
5353   if (!NewLCDecl || !NewLB)
5354     return true;
5355   LCDecl = getCanonicalDecl(NewLCDecl);
5356   LCRef = NewLCRefExpr;
5357   if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
5358     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
5359       if ((Ctor->isCopyOrMoveConstructor() ||
5360            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
5361           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
5362         NewLB = CE->getArg(0)->IgnoreParenImpCasts();
5363   LB = NewLB;
5364   if (EmitDiags)
5365     InitDependOnLC = doesDependOnLoopCounter(LB, /*IsInitializer=*/true);
5366   return false;
5367 }
5368 
5369 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB,
5370                                         llvm::Optional<bool> LessOp,
5371                                         bool StrictOp, SourceRange SR,
5372                                         SourceLocation SL) {
5373   // State consistency checking to ensure correct usage.
5374   assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
5375          Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
5376   if (!NewUB)
5377     return true;
5378   UB = NewUB;
5379   if (LessOp)
5380     TestIsLessOp = LessOp;
5381   TestIsStrictOp = StrictOp;
5382   ConditionSrcRange = SR;
5383   ConditionLoc = SL;
5384   CondDependOnLC = doesDependOnLoopCounter(UB, /*IsInitializer=*/false);
5385   return false;
5386 }
5387 
5388 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
5389   // State consistency checking to ensure correct usage.
5390   assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
5391   if (!NewStep)
5392     return true;
5393   if (!NewStep->isValueDependent()) {
5394     // Check that the step is integer expression.
5395     SourceLocation StepLoc = NewStep->getBeginLoc();
5396     ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion(
5397         StepLoc, getExprAsWritten(NewStep));
5398     if (Val.isInvalid())
5399       return true;
5400     NewStep = Val.get();
5401 
5402     // OpenMP [2.6, Canonical Loop Form, Restrictions]
5403     //  If test-expr is of form var relational-op b and relational-op is < or
5404     //  <= then incr-expr must cause var to increase on each iteration of the
5405     //  loop. If test-expr is of form var relational-op b and relational-op is
5406     //  > or >= then incr-expr must cause var to decrease on each iteration of
5407     //  the loop.
5408     //  If test-expr is of form b relational-op var and relational-op is < or
5409     //  <= then incr-expr must cause var to decrease on each iteration of the
5410     //  loop. If test-expr is of form b relational-op var and relational-op is
5411     //  > or >= then incr-expr must cause var to increase on each iteration of
5412     //  the loop.
5413     llvm::APSInt Result;
5414     bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
5415     bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
5416     bool IsConstNeg =
5417         IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
5418     bool IsConstPos =
5419         IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
5420     bool IsConstZero = IsConstant && !Result.getBoolValue();
5421 
5422     // != with increment is treated as <; != with decrement is treated as >
5423     if (!TestIsLessOp.hasValue())
5424       TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract);
5425     if (UB && (IsConstZero ||
5426                (TestIsLessOp.getValue() ?
5427                   (IsConstNeg || (IsUnsigned && Subtract)) :
5428                   (IsConstPos || (IsUnsigned && !Subtract))))) {
5429       SemaRef.Diag(NewStep->getExprLoc(),
5430                    diag::err_omp_loop_incr_not_compatible)
5431           << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange();
5432       SemaRef.Diag(ConditionLoc,
5433                    diag::note_omp_loop_cond_requres_compatible_incr)
5434           << TestIsLessOp.getValue() << ConditionSrcRange;
5435       return true;
5436     }
5437     if (TestIsLessOp.getValue() == Subtract) {
5438       NewStep =
5439           SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
5440               .get();
5441       Subtract = !Subtract;
5442     }
5443   }
5444 
5445   Step = NewStep;
5446   SubtractStep = Subtract;
5447   return false;
5448 }
5449 
5450 namespace {
5451 /// Checker for the non-rectangular loops. Checks if the initializer or
5452 /// condition expression references loop counter variable.
5453 class LoopCounterRefChecker final
5454     : public ConstStmtVisitor<LoopCounterRefChecker, bool> {
5455   Sema &SemaRef;
5456   DSAStackTy &Stack;
5457   const ValueDecl *CurLCDecl = nullptr;
5458   const ValueDecl *DepDecl = nullptr;
5459   const ValueDecl *PrevDepDecl = nullptr;
5460   bool IsInitializer = true;
5461   unsigned BaseLoopId = 0;
5462   bool checkDecl(const Expr *E, const ValueDecl *VD) {
5463     if (getCanonicalDecl(VD) == getCanonicalDecl(CurLCDecl)) {
5464       SemaRef.Diag(E->getExprLoc(), diag::err_omp_stmt_depends_on_loop_counter)
5465           << (IsInitializer ? 0 : 1);
5466       return false;
5467     }
5468     const auto &&Data = Stack.isLoopControlVariable(VD);
5469     // OpenMP, 2.9.1 Canonical Loop Form, Restrictions.
5470     // The type of the loop iterator on which we depend may not have a random
5471     // access iterator type.
5472     if (Data.first && VD->getType()->isRecordType()) {
5473       SmallString<128> Name;
5474       llvm::raw_svector_ostream OS(Name);
5475       VD->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(),
5476                                /*Qualified=*/true);
5477       SemaRef.Diag(E->getExprLoc(),
5478                    diag::err_omp_wrong_dependency_iterator_type)
5479           << OS.str();
5480       SemaRef.Diag(VD->getLocation(), diag::note_previous_decl) << VD;
5481       return false;
5482     }
5483     if (Data.first &&
5484         (DepDecl || (PrevDepDecl &&
5485                      getCanonicalDecl(VD) != getCanonicalDecl(PrevDepDecl)))) {
5486       if (!DepDecl && PrevDepDecl)
5487         DepDecl = PrevDepDecl;
5488       SmallString<128> Name;
5489       llvm::raw_svector_ostream OS(Name);
5490       DepDecl->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(),
5491                                     /*Qualified=*/true);
5492       SemaRef.Diag(E->getExprLoc(),
5493                    diag::err_omp_invariant_or_linear_dependency)
5494           << OS.str();
5495       return false;
5496     }
5497     if (Data.first) {
5498       DepDecl = VD;
5499       BaseLoopId = Data.first;
5500     }
5501     return Data.first;
5502   }
5503 
5504 public:
5505   bool VisitDeclRefExpr(const DeclRefExpr *E) {
5506     const ValueDecl *VD = E->getDecl();
5507     if (isa<VarDecl>(VD))
5508       return checkDecl(E, VD);
5509     return false;
5510   }
5511   bool VisitMemberExpr(const MemberExpr *E) {
5512     if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
5513       const ValueDecl *VD = E->getMemberDecl();
5514       if (isa<VarDecl>(VD) || isa<FieldDecl>(VD))
5515         return checkDecl(E, VD);
5516     }
5517     return false;
5518   }
5519   bool VisitStmt(const Stmt *S) {
5520     bool Res = false;
5521     for (const Stmt *Child : S->children())
5522       Res = (Child && Visit(Child)) || Res;
5523     return Res;
5524   }
5525   explicit LoopCounterRefChecker(Sema &SemaRef, DSAStackTy &Stack,
5526                                  const ValueDecl *CurLCDecl, bool IsInitializer,
5527                                  const ValueDecl *PrevDepDecl = nullptr)
5528       : SemaRef(SemaRef), Stack(Stack), CurLCDecl(CurLCDecl),
5529         PrevDepDecl(PrevDepDecl), IsInitializer(IsInitializer) {}
5530   unsigned getBaseLoopId() const {
5531     assert(CurLCDecl && "Expected loop dependency.");
5532     return BaseLoopId;
5533   }
5534   const ValueDecl *getDepDecl() const {
5535     assert(CurLCDecl && "Expected loop dependency.");
5536     return DepDecl;
5537   }
5538 };
5539 } // namespace
5540 
5541 Optional<unsigned>
5542 OpenMPIterationSpaceChecker::doesDependOnLoopCounter(const Stmt *S,
5543                                                      bool IsInitializer) {
5544   // Check for the non-rectangular loops.
5545   LoopCounterRefChecker LoopStmtChecker(SemaRef, Stack, LCDecl, IsInitializer,
5546                                         DepDecl);
5547   if (LoopStmtChecker.Visit(S)) {
5548     DepDecl = LoopStmtChecker.getDepDecl();
5549     return LoopStmtChecker.getBaseLoopId();
5550   }
5551   return llvm::None;
5552 }
5553 
5554 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
5555   // Check init-expr for canonical loop form and save loop counter
5556   // variable - #Var and its initialization value - #LB.
5557   // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
5558   //   var = lb
5559   //   integer-type var = lb
5560   //   random-access-iterator-type var = lb
5561   //   pointer-type var = lb
5562   //
5563   if (!S) {
5564     if (EmitDiags) {
5565       SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
5566     }
5567     return true;
5568   }
5569   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
5570     if (!ExprTemp->cleanupsHaveSideEffects())
5571       S = ExprTemp->getSubExpr();
5572 
5573   InitSrcRange = S->getSourceRange();
5574   if (Expr *E = dyn_cast<Expr>(S))
5575     S = E->IgnoreParens();
5576   if (auto *BO = dyn_cast<BinaryOperator>(S)) {
5577     if (BO->getOpcode() == BO_Assign) {
5578       Expr *LHS = BO->getLHS()->IgnoreParens();
5579       if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
5580         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
5581           if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
5582             return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
5583                                   EmitDiags);
5584         return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS(), EmitDiags);
5585       }
5586       if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
5587         if (ME->isArrow() &&
5588             isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
5589           return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
5590                                 EmitDiags);
5591       }
5592     }
5593   } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
5594     if (DS->isSingleDecl()) {
5595       if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
5596         if (Var->hasInit() && !Var->getType()->isReferenceType()) {
5597           // Accept non-canonical init form here but emit ext. warning.
5598           if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
5599             SemaRef.Diag(S->getBeginLoc(),
5600                          diag::ext_omp_loop_not_canonical_init)
5601                 << S->getSourceRange();
5602           return setLCDeclAndLB(
5603               Var,
5604               buildDeclRefExpr(SemaRef, Var,
5605                                Var->getType().getNonReferenceType(),
5606                                DS->getBeginLoc()),
5607               Var->getInit(), EmitDiags);
5608         }
5609       }
5610     }
5611   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
5612     if (CE->getOperator() == OO_Equal) {
5613       Expr *LHS = CE->getArg(0);
5614       if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
5615         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
5616           if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
5617             return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
5618                                   EmitDiags);
5619         return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1), EmitDiags);
5620       }
5621       if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
5622         if (ME->isArrow() &&
5623             isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
5624           return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
5625                                 EmitDiags);
5626       }
5627     }
5628   }
5629 
5630   if (dependent() || SemaRef.CurContext->isDependentContext())
5631     return false;
5632   if (EmitDiags) {
5633     SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init)
5634         << S->getSourceRange();
5635   }
5636   return true;
5637 }
5638 
5639 /// Ignore parenthesizes, implicit casts, copy constructor and return the
5640 /// variable (which may be the loop variable) if possible.
5641 static const ValueDecl *getInitLCDecl(const Expr *E) {
5642   if (!E)
5643     return nullptr;
5644   E = getExprAsWritten(E);
5645   if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
5646     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
5647       if ((Ctor->isCopyOrMoveConstructor() ||
5648            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
5649           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
5650         E = CE->getArg(0)->IgnoreParenImpCasts();
5651   if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
5652     if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
5653       return getCanonicalDecl(VD);
5654   }
5655   if (const auto *ME = dyn_cast_or_null<MemberExpr>(E))
5656     if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
5657       return getCanonicalDecl(ME->getMemberDecl());
5658   return nullptr;
5659 }
5660 
5661 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
5662   // Check test-expr for canonical form, save upper-bound UB, flags for
5663   // less/greater and for strict/non-strict comparison.
5664   // OpenMP [2.9] Canonical loop form. Test-expr may be one of the following:
5665   //   var relational-op b
5666   //   b relational-op var
5667   //
5668   bool IneqCondIsCanonical = SemaRef.getLangOpts().OpenMP >= 50;
5669   if (!S) {
5670     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond)
5671         << (IneqCondIsCanonical ? 1 : 0) << LCDecl;
5672     return true;
5673   }
5674   Condition = S;
5675   S = getExprAsWritten(S);
5676   SourceLocation CondLoc = S->getBeginLoc();
5677   if (auto *BO = dyn_cast<BinaryOperator>(S)) {
5678     if (BO->isRelationalOp()) {
5679       if (getInitLCDecl(BO->getLHS()) == LCDecl)
5680         return setUB(BO->getRHS(),
5681                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
5682                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
5683                      BO->getSourceRange(), BO->getOperatorLoc());
5684       if (getInitLCDecl(BO->getRHS()) == LCDecl)
5685         return setUB(BO->getLHS(),
5686                      (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
5687                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
5688                      BO->getSourceRange(), BO->getOperatorLoc());
5689     } else if (IneqCondIsCanonical && BO->getOpcode() == BO_NE)
5690       return setUB(
5691           getInitLCDecl(BO->getLHS()) == LCDecl ? BO->getRHS() : BO->getLHS(),
5692           /*LessOp=*/llvm::None,
5693           /*StrictOp=*/true, BO->getSourceRange(), BO->getOperatorLoc());
5694   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
5695     if (CE->getNumArgs() == 2) {
5696       auto Op = CE->getOperator();
5697       switch (Op) {
5698       case OO_Greater:
5699       case OO_GreaterEqual:
5700       case OO_Less:
5701       case OO_LessEqual:
5702         if (getInitLCDecl(CE->getArg(0)) == LCDecl)
5703           return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
5704                        Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
5705                        CE->getOperatorLoc());
5706         if (getInitLCDecl(CE->getArg(1)) == LCDecl)
5707           return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
5708                        Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
5709                        CE->getOperatorLoc());
5710         break;
5711       case OO_ExclaimEqual:
5712         if (IneqCondIsCanonical)
5713           return setUB(getInitLCDecl(CE->getArg(0)) == LCDecl ? CE->getArg(1)
5714                                                               : CE->getArg(0),
5715                        /*LessOp=*/llvm::None,
5716                        /*StrictOp=*/true, CE->getSourceRange(),
5717                        CE->getOperatorLoc());
5718         break;
5719       default:
5720         break;
5721       }
5722     }
5723   }
5724   if (dependent() || SemaRef.CurContext->isDependentContext())
5725     return false;
5726   SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
5727       << (IneqCondIsCanonical ? 1 : 0) << S->getSourceRange() << LCDecl;
5728   return true;
5729 }
5730 
5731 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
5732   // RHS of canonical loop form increment can be:
5733   //   var + incr
5734   //   incr + var
5735   //   var - incr
5736   //
5737   RHS = RHS->IgnoreParenImpCasts();
5738   if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
5739     if (BO->isAdditiveOp()) {
5740       bool IsAdd = BO->getOpcode() == BO_Add;
5741       if (getInitLCDecl(BO->getLHS()) == LCDecl)
5742         return setStep(BO->getRHS(), !IsAdd);
5743       if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl)
5744         return setStep(BO->getLHS(), /*Subtract=*/false);
5745     }
5746   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
5747     bool IsAdd = CE->getOperator() == OO_Plus;
5748     if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
5749       if (getInitLCDecl(CE->getArg(0)) == LCDecl)
5750         return setStep(CE->getArg(1), !IsAdd);
5751       if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl)
5752         return setStep(CE->getArg(0), /*Subtract=*/false);
5753     }
5754   }
5755   if (dependent() || SemaRef.CurContext->isDependentContext())
5756     return false;
5757   SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
5758       << RHS->getSourceRange() << LCDecl;
5759   return true;
5760 }
5761 
5762 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
5763   // Check incr-expr for canonical loop form and return true if it
5764   // does not conform.
5765   // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
5766   //   ++var
5767   //   var++
5768   //   --var
5769   //   var--
5770   //   var += incr
5771   //   var -= incr
5772   //   var = var + incr
5773   //   var = incr + var
5774   //   var = var - incr
5775   //
5776   if (!S) {
5777     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
5778     return true;
5779   }
5780   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
5781     if (!ExprTemp->cleanupsHaveSideEffects())
5782       S = ExprTemp->getSubExpr();
5783 
5784   IncrementSrcRange = S->getSourceRange();
5785   S = S->IgnoreParens();
5786   if (auto *UO = dyn_cast<UnaryOperator>(S)) {
5787     if (UO->isIncrementDecrementOp() &&
5788         getInitLCDecl(UO->getSubExpr()) == LCDecl)
5789       return setStep(SemaRef
5790                          .ActOnIntegerConstant(UO->getBeginLoc(),
5791                                                (UO->isDecrementOp() ? -1 : 1))
5792                          .get(),
5793                      /*Subtract=*/false);
5794   } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
5795     switch (BO->getOpcode()) {
5796     case BO_AddAssign:
5797     case BO_SubAssign:
5798       if (getInitLCDecl(BO->getLHS()) == LCDecl)
5799         return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
5800       break;
5801     case BO_Assign:
5802       if (getInitLCDecl(BO->getLHS()) == LCDecl)
5803         return checkAndSetIncRHS(BO->getRHS());
5804       break;
5805     default:
5806       break;
5807     }
5808   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
5809     switch (CE->getOperator()) {
5810     case OO_PlusPlus:
5811     case OO_MinusMinus:
5812       if (getInitLCDecl(CE->getArg(0)) == LCDecl)
5813         return setStep(SemaRef
5814                            .ActOnIntegerConstant(
5815                                CE->getBeginLoc(),
5816                                ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
5817                            .get(),
5818                        /*Subtract=*/false);
5819       break;
5820     case OO_PlusEqual:
5821     case OO_MinusEqual:
5822       if (getInitLCDecl(CE->getArg(0)) == LCDecl)
5823         return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
5824       break;
5825     case OO_Equal:
5826       if (getInitLCDecl(CE->getArg(0)) == LCDecl)
5827         return checkAndSetIncRHS(CE->getArg(1));
5828       break;
5829     default:
5830       break;
5831     }
5832   }
5833   if (dependent() || SemaRef.CurContext->isDependentContext())
5834     return false;
5835   SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
5836       << S->getSourceRange() << LCDecl;
5837   return true;
5838 }
5839 
5840 static ExprResult
5841 tryBuildCapture(Sema &SemaRef, Expr *Capture,
5842                 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
5843   if (SemaRef.CurContext->isDependentContext())
5844     return ExprResult(Capture);
5845   if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
5846     return SemaRef.PerformImplicitConversion(
5847         Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
5848         /*AllowExplicit=*/true);
5849   auto I = Captures.find(Capture);
5850   if (I != Captures.end())
5851     return buildCapture(SemaRef, Capture, I->second);
5852   DeclRefExpr *Ref = nullptr;
5853   ExprResult Res = buildCapture(SemaRef, Capture, Ref);
5854   Captures[Capture] = Ref;
5855   return Res;
5856 }
5857 
5858 /// Build the expression to calculate the number of iterations.
5859 Expr *OpenMPIterationSpaceChecker::buildNumIterations(
5860     Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType,
5861     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
5862   ExprResult Diff;
5863   QualType VarType = LCDecl->getType().getNonReferenceType();
5864   if (VarType->isIntegerType() || VarType->isPointerType() ||
5865       SemaRef.getLangOpts().CPlusPlus) {
5866     Expr *LBVal = LB;
5867     Expr *UBVal = UB;
5868     // LB = TestIsLessOp.getValue() ? min(LB(MinVal), LB(MaxVal)) :
5869     // max(LB(MinVal), LB(MaxVal))
5870     if (InitDependOnLC) {
5871       const LoopIterationSpace &IS =
5872           ResultIterSpaces[ResultIterSpaces.size() - 1 -
5873                            InitDependOnLC.getValueOr(
5874                                CondDependOnLC.getValueOr(0))];
5875       if (!IS.MinValue || !IS.MaxValue)
5876         return nullptr;
5877       // OuterVar = Min
5878       ExprResult MinValue =
5879           SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue);
5880       if (!MinValue.isUsable())
5881         return nullptr;
5882 
5883       ExprResult LBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
5884                                                IS.CounterVar, MinValue.get());
5885       if (!LBMinVal.isUsable())
5886         return nullptr;
5887       // OuterVar = Min, LBVal
5888       LBMinVal =
5889           SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMinVal.get(), LBVal);
5890       if (!LBMinVal.isUsable())
5891         return nullptr;
5892       // (OuterVar = Min, LBVal)
5893       LBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMinVal.get());
5894       if (!LBMinVal.isUsable())
5895         return nullptr;
5896 
5897       // OuterVar = Max
5898       ExprResult MaxValue =
5899           SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue);
5900       if (!MaxValue.isUsable())
5901         return nullptr;
5902 
5903       ExprResult LBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
5904                                                IS.CounterVar, MaxValue.get());
5905       if (!LBMaxVal.isUsable())
5906         return nullptr;
5907       // OuterVar = Max, LBVal
5908       LBMaxVal =
5909           SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMaxVal.get(), LBVal);
5910       if (!LBMaxVal.isUsable())
5911         return nullptr;
5912       // (OuterVar = Max, LBVal)
5913       LBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMaxVal.get());
5914       if (!LBMaxVal.isUsable())
5915         return nullptr;
5916 
5917       Expr *LBMin = tryBuildCapture(SemaRef, LBMinVal.get(), Captures).get();
5918       Expr *LBMax = tryBuildCapture(SemaRef, LBMaxVal.get(), Captures).get();
5919       if (!LBMin || !LBMax)
5920         return nullptr;
5921       // LB(MinVal) < LB(MaxVal)
5922       ExprResult MinLessMaxRes =
5923           SemaRef.BuildBinOp(S, DefaultLoc, BO_LT, LBMin, LBMax);
5924       if (!MinLessMaxRes.isUsable())
5925         return nullptr;
5926       Expr *MinLessMax =
5927           tryBuildCapture(SemaRef, MinLessMaxRes.get(), Captures).get();
5928       if (!MinLessMax)
5929         return nullptr;
5930       if (TestIsLessOp.getValue()) {
5931         // LB(MinVal) < LB(MaxVal) ? LB(MinVal) : LB(MaxVal) - min(LB(MinVal),
5932         // LB(MaxVal))
5933         ExprResult MinLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc,
5934                                                       MinLessMax, LBMin, LBMax);
5935         if (!MinLB.isUsable())
5936           return nullptr;
5937         LBVal = MinLB.get();
5938       } else {
5939         // LB(MinVal) < LB(MaxVal) ? LB(MaxVal) : LB(MinVal) - max(LB(MinVal),
5940         // LB(MaxVal))
5941         ExprResult MaxLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc,
5942                                                       MinLessMax, LBMax, LBMin);
5943         if (!MaxLB.isUsable())
5944           return nullptr;
5945         LBVal = MaxLB.get();
5946       }
5947     }
5948     // UB = TestIsLessOp.getValue() ? max(UB(MinVal), UB(MaxVal)) :
5949     // min(UB(MinVal), UB(MaxVal))
5950     if (CondDependOnLC) {
5951       const LoopIterationSpace &IS =
5952           ResultIterSpaces[ResultIterSpaces.size() - 1 -
5953                            InitDependOnLC.getValueOr(
5954                                CondDependOnLC.getValueOr(0))];
5955       if (!IS.MinValue || !IS.MaxValue)
5956         return nullptr;
5957       // OuterVar = Min
5958       ExprResult MinValue =
5959           SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue);
5960       if (!MinValue.isUsable())
5961         return nullptr;
5962 
5963       ExprResult UBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
5964                                                IS.CounterVar, MinValue.get());
5965       if (!UBMinVal.isUsable())
5966         return nullptr;
5967       // OuterVar = Min, UBVal
5968       UBMinVal =
5969           SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMinVal.get(), UBVal);
5970       if (!UBMinVal.isUsable())
5971         return nullptr;
5972       // (OuterVar = Min, UBVal)
5973       UBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMinVal.get());
5974       if (!UBMinVal.isUsable())
5975         return nullptr;
5976 
5977       // OuterVar = Max
5978       ExprResult MaxValue =
5979           SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue);
5980       if (!MaxValue.isUsable())
5981         return nullptr;
5982 
5983       ExprResult UBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
5984                                                IS.CounterVar, MaxValue.get());
5985       if (!UBMaxVal.isUsable())
5986         return nullptr;
5987       // OuterVar = Max, UBVal
5988       UBMaxVal =
5989           SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMaxVal.get(), UBVal);
5990       if (!UBMaxVal.isUsable())
5991         return nullptr;
5992       // (OuterVar = Max, UBVal)
5993       UBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMaxVal.get());
5994       if (!UBMaxVal.isUsable())
5995         return nullptr;
5996 
5997       Expr *UBMin = tryBuildCapture(SemaRef, UBMinVal.get(), Captures).get();
5998       Expr *UBMax = tryBuildCapture(SemaRef, UBMaxVal.get(), Captures).get();
5999       if (!UBMin || !UBMax)
6000         return nullptr;
6001       // UB(MinVal) > UB(MaxVal)
6002       ExprResult MinGreaterMaxRes =
6003           SemaRef.BuildBinOp(S, DefaultLoc, BO_GT, UBMin, UBMax);
6004       if (!MinGreaterMaxRes.isUsable())
6005         return nullptr;
6006       Expr *MinGreaterMax =
6007           tryBuildCapture(SemaRef, MinGreaterMaxRes.get(), Captures).get();
6008       if (!MinGreaterMax)
6009         return nullptr;
6010       if (TestIsLessOp.getValue()) {
6011         // UB(MinVal) > UB(MaxVal) ? UB(MinVal) : UB(MaxVal) - max(UB(MinVal),
6012         // UB(MaxVal))
6013         ExprResult MaxUB = SemaRef.ActOnConditionalOp(
6014             DefaultLoc, DefaultLoc, MinGreaterMax, UBMin, UBMax);
6015         if (!MaxUB.isUsable())
6016           return nullptr;
6017         UBVal = MaxUB.get();
6018       } else {
6019         // UB(MinVal) > UB(MaxVal) ? UB(MaxVal) : UB(MinVal) - min(UB(MinVal),
6020         // UB(MaxVal))
6021         ExprResult MinUB = SemaRef.ActOnConditionalOp(
6022             DefaultLoc, DefaultLoc, MinGreaterMax, UBMax, UBMin);
6023         if (!MinUB.isUsable())
6024           return nullptr;
6025         UBVal = MinUB.get();
6026       }
6027     }
6028     // Upper - Lower
6029     Expr *UBExpr = TestIsLessOp.getValue() ? UBVal : LBVal;
6030     Expr *LBExpr = TestIsLessOp.getValue() ? LBVal : UBVal;
6031     Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
6032     Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
6033     if (!Upper || !Lower)
6034       return nullptr;
6035 
6036     Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
6037 
6038     if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
6039       // BuildBinOp already emitted error, this one is to point user to upper
6040       // and lower bound, and to tell what is passed to 'operator-'.
6041       SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
6042           << Upper->getSourceRange() << Lower->getSourceRange();
6043       return nullptr;
6044     }
6045   }
6046 
6047   if (!Diff.isUsable())
6048     return nullptr;
6049 
6050   // Upper - Lower [- 1]
6051   if (TestIsStrictOp)
6052     Diff = SemaRef.BuildBinOp(
6053         S, DefaultLoc, BO_Sub, Diff.get(),
6054         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
6055   if (!Diff.isUsable())
6056     return nullptr;
6057 
6058   // Upper - Lower [- 1] + Step
6059   ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
6060   if (!NewStep.isUsable())
6061     return nullptr;
6062   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
6063   if (!Diff.isUsable())
6064     return nullptr;
6065 
6066   // Parentheses (for dumping/debugging purposes only).
6067   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
6068   if (!Diff.isUsable())
6069     return nullptr;
6070 
6071   // (Upper - Lower [- 1] + Step) / Step
6072   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
6073   if (!Diff.isUsable())
6074     return nullptr;
6075 
6076   // OpenMP runtime requires 32-bit or 64-bit loop variables.
6077   QualType Type = Diff.get()->getType();
6078   ASTContext &C = SemaRef.Context;
6079   bool UseVarType = VarType->hasIntegerRepresentation() &&
6080                     C.getTypeSize(Type) > C.getTypeSize(VarType);
6081   if (!Type->isIntegerType() || UseVarType) {
6082     unsigned NewSize =
6083         UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
6084     bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
6085                                : Type->hasSignedIntegerRepresentation();
6086     Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
6087     if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
6088       Diff = SemaRef.PerformImplicitConversion(
6089           Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
6090       if (!Diff.isUsable())
6091         return nullptr;
6092     }
6093   }
6094   if (LimitedType) {
6095     unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
6096     if (NewSize != C.getTypeSize(Type)) {
6097       if (NewSize < C.getTypeSize(Type)) {
6098         assert(NewSize == 64 && "incorrect loop var size");
6099         SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
6100             << InitSrcRange << ConditionSrcRange;
6101       }
6102       QualType NewType = C.getIntTypeForBitwidth(
6103           NewSize, Type->hasSignedIntegerRepresentation() ||
6104                        C.getTypeSize(Type) < NewSize);
6105       if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
6106         Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
6107                                                  Sema::AA_Converting, true);
6108         if (!Diff.isUsable())
6109           return nullptr;
6110       }
6111     }
6112   }
6113 
6114   return Diff.get();
6115 }
6116 
6117 std::pair<Expr *, Expr *> OpenMPIterationSpaceChecker::buildMinMaxValues(
6118     Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
6119   // Do not build for iterators, they cannot be used in non-rectangular loop
6120   // nests.
6121   if (LCDecl->getType()->isRecordType())
6122     return std::make_pair(nullptr, nullptr);
6123   // If we subtract, the min is in the condition, otherwise the min is in the
6124   // init value.
6125   Expr *MinExpr = nullptr;
6126   Expr *MaxExpr = nullptr;
6127   Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB;
6128   Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB;
6129   bool LBNonRect = TestIsLessOp.getValue() ? InitDependOnLC.hasValue()
6130                                            : CondDependOnLC.hasValue();
6131   bool UBNonRect = TestIsLessOp.getValue() ? CondDependOnLC.hasValue()
6132                                            : InitDependOnLC.hasValue();
6133   Expr *Lower =
6134       LBNonRect ? LBExpr : tryBuildCapture(SemaRef, LBExpr, Captures).get();
6135   Expr *Upper =
6136       UBNonRect ? UBExpr : tryBuildCapture(SemaRef, UBExpr, Captures).get();
6137   if (!Upper || !Lower)
6138     return std::make_pair(nullptr, nullptr);
6139 
6140   if (TestIsLessOp.getValue())
6141     MinExpr = Lower;
6142   else
6143     MaxExpr = Upper;
6144 
6145   // Build minimum/maximum value based on number of iterations.
6146   ExprResult Diff;
6147   QualType VarType = LCDecl->getType().getNonReferenceType();
6148 
6149   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
6150   if (!Diff.isUsable())
6151     return std::make_pair(nullptr, nullptr);
6152 
6153   // Upper - Lower [- 1]
6154   if (TestIsStrictOp)
6155     Diff = SemaRef.BuildBinOp(
6156         S, DefaultLoc, BO_Sub, Diff.get(),
6157         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
6158   if (!Diff.isUsable())
6159     return std::make_pair(nullptr, nullptr);
6160 
6161   // Upper - Lower [- 1] + Step
6162   ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
6163   if (!NewStep.isUsable())
6164     return std::make_pair(nullptr, nullptr);
6165 
6166   // Parentheses (for dumping/debugging purposes only).
6167   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
6168   if (!Diff.isUsable())
6169     return std::make_pair(nullptr, nullptr);
6170 
6171   // (Upper - Lower [- 1]) / Step
6172   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
6173   if (!Diff.isUsable())
6174     return std::make_pair(nullptr, nullptr);
6175 
6176   // ((Upper - Lower [- 1]) / Step) * Step
6177   // Parentheses (for dumping/debugging purposes only).
6178   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
6179   if (!Diff.isUsable())
6180     return std::make_pair(nullptr, nullptr);
6181 
6182   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Mul, Diff.get(), NewStep.get());
6183   if (!Diff.isUsable())
6184     return std::make_pair(nullptr, nullptr);
6185 
6186   // Convert to the original type or ptrdiff_t, if original type is pointer.
6187   if (!VarType->isAnyPointerType() &&
6188       !SemaRef.Context.hasSameType(Diff.get()->getType(), VarType)) {
6189     Diff = SemaRef.PerformImplicitConversion(
6190         Diff.get(), VarType, Sema::AA_Converting, /*AllowExplicit=*/true);
6191   } else if (VarType->isAnyPointerType() &&
6192              !SemaRef.Context.hasSameType(
6193                  Diff.get()->getType(),
6194                  SemaRef.Context.getUnsignedPointerDiffType())) {
6195     Diff = SemaRef.PerformImplicitConversion(
6196         Diff.get(), SemaRef.Context.getUnsignedPointerDiffType(),
6197         Sema::AA_Converting, /*AllowExplicit=*/true);
6198   }
6199   if (!Diff.isUsable())
6200     return std::make_pair(nullptr, nullptr);
6201 
6202   // Parentheses (for dumping/debugging purposes only).
6203   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
6204   if (!Diff.isUsable())
6205     return std::make_pair(nullptr, nullptr);
6206 
6207   if (TestIsLessOp.getValue()) {
6208     // MinExpr = Lower;
6209     // MaxExpr = Lower + (((Upper - Lower [- 1]) / Step) * Step)
6210     Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Lower, Diff.get());
6211     if (!Diff.isUsable())
6212       return std::make_pair(nullptr, nullptr);
6213     Diff = SemaRef.ActOnFinishFullExpr(Diff.get(), /*DiscardedValue*/ false);
6214     if (!Diff.isUsable())
6215       return std::make_pair(nullptr, nullptr);
6216     MaxExpr = Diff.get();
6217   } else {
6218     // MaxExpr = Upper;
6219     // MinExpr = Upper - (((Upper - Lower [- 1]) / Step) * Step)
6220     Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Diff.get());
6221     if (!Diff.isUsable())
6222       return std::make_pair(nullptr, nullptr);
6223     Diff = SemaRef.ActOnFinishFullExpr(Diff.get(), /*DiscardedValue*/ false);
6224     if (!Diff.isUsable())
6225       return std::make_pair(nullptr, nullptr);
6226     MinExpr = Diff.get();
6227   }
6228 
6229   return std::make_pair(MinExpr, MaxExpr);
6230 }
6231 
6232 Expr *OpenMPIterationSpaceChecker::buildFinalCondition(Scope *S) const {
6233   if (InitDependOnLC || CondDependOnLC)
6234     return Condition;
6235   return nullptr;
6236 }
6237 
6238 Expr *OpenMPIterationSpaceChecker::buildPreCond(
6239     Scope *S, Expr *Cond,
6240     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
6241   // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
6242   Sema::TentativeAnalysisScope Trap(SemaRef);
6243 
6244   ExprResult NewLB =
6245       InitDependOnLC ? LB : tryBuildCapture(SemaRef, LB, Captures);
6246   ExprResult NewUB =
6247       CondDependOnLC ? UB : tryBuildCapture(SemaRef, UB, Captures);
6248   if (!NewLB.isUsable() || !NewUB.isUsable())
6249     return nullptr;
6250 
6251   ExprResult CondExpr =
6252       SemaRef.BuildBinOp(S, DefaultLoc,
6253                          TestIsLessOp.getValue() ?
6254                            (TestIsStrictOp ? BO_LT : BO_LE) :
6255                            (TestIsStrictOp ? BO_GT : BO_GE),
6256                          NewLB.get(), NewUB.get());
6257   if (CondExpr.isUsable()) {
6258     if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
6259                                                 SemaRef.Context.BoolTy))
6260       CondExpr = SemaRef.PerformImplicitConversion(
6261           CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
6262           /*AllowExplicit=*/true);
6263   }
6264 
6265   // Otherwise use original loop condition and evaluate it in runtime.
6266   return CondExpr.isUsable() ? CondExpr.get() : Cond;
6267 }
6268 
6269 /// Build reference expression to the counter be used for codegen.
6270 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
6271     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
6272     DSAStackTy &DSA) const {
6273   auto *VD = dyn_cast<VarDecl>(LCDecl);
6274   if (!VD) {
6275     VD = SemaRef.isOpenMPCapturedDecl(LCDecl);
6276     DeclRefExpr *Ref = buildDeclRefExpr(
6277         SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
6278     const DSAStackTy::DSAVarData Data =
6279         DSA.getTopDSA(LCDecl, /*FromParent=*/false);
6280     // If the loop control decl is explicitly marked as private, do not mark it
6281     // as captured again.
6282     if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
6283       Captures.insert(std::make_pair(LCRef, Ref));
6284     return Ref;
6285   }
6286   return cast<DeclRefExpr>(LCRef);
6287 }
6288 
6289 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
6290   if (LCDecl && !LCDecl->isInvalidDecl()) {
6291     QualType Type = LCDecl->getType().getNonReferenceType();
6292     VarDecl *PrivateVar = buildVarDecl(
6293         SemaRef, DefaultLoc, Type, LCDecl->getName(),
6294         LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
6295         isa<VarDecl>(LCDecl)
6296             ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc)
6297             : nullptr);
6298     if (PrivateVar->isInvalidDecl())
6299       return nullptr;
6300     return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
6301   }
6302   return nullptr;
6303 }
6304 
6305 /// Build initialization of the counter to be used for codegen.
6306 Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
6307 
6308 /// Build step of the counter be used for codegen.
6309 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
6310 
6311 Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
6312     Scope *S, Expr *Counter,
6313     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
6314     Expr *Inc, OverloadedOperatorKind OOK) {
6315   Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get();
6316   if (!Cnt)
6317     return nullptr;
6318   if (Inc) {
6319     assert((OOK == OO_Plus || OOK == OO_Minus) &&
6320            "Expected only + or - operations for depend clauses.");
6321     BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
6322     Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get();
6323     if (!Cnt)
6324       return nullptr;
6325   }
6326   ExprResult Diff;
6327   QualType VarType = LCDecl->getType().getNonReferenceType();
6328   if (VarType->isIntegerType() || VarType->isPointerType() ||
6329       SemaRef.getLangOpts().CPlusPlus) {
6330     // Upper - Lower
6331     Expr *Upper = TestIsLessOp.getValue()
6332                       ? Cnt
6333                       : tryBuildCapture(SemaRef, UB, Captures).get();
6334     Expr *Lower = TestIsLessOp.getValue()
6335                       ? tryBuildCapture(SemaRef, LB, Captures).get()
6336                       : Cnt;
6337     if (!Upper || !Lower)
6338       return nullptr;
6339 
6340     Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
6341 
6342     if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
6343       // BuildBinOp already emitted error, this one is to point user to upper
6344       // and lower bound, and to tell what is passed to 'operator-'.
6345       SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
6346           << Upper->getSourceRange() << Lower->getSourceRange();
6347       return nullptr;
6348     }
6349   }
6350 
6351   if (!Diff.isUsable())
6352     return nullptr;
6353 
6354   // Parentheses (for dumping/debugging purposes only).
6355   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
6356   if (!Diff.isUsable())
6357     return nullptr;
6358 
6359   ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
6360   if (!NewStep.isUsable())
6361     return nullptr;
6362   // (Upper - Lower) / Step
6363   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
6364   if (!Diff.isUsable())
6365     return nullptr;
6366 
6367   return Diff.get();
6368 }
6369 } // namespace
6370 
6371 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
6372   assert(getLangOpts().OpenMP && "OpenMP is not active.");
6373   assert(Init && "Expected loop in canonical form.");
6374   unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
6375   if (AssociatedLoops > 0 &&
6376       isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
6377     DSAStack->loopStart();
6378     OpenMPIterationSpaceChecker ISC(*this, *DSAStack, ForLoc);
6379     if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) {
6380       if (ValueDecl *D = ISC.getLoopDecl()) {
6381         auto *VD = dyn_cast<VarDecl>(D);
6382         DeclRefExpr *PrivateRef = nullptr;
6383         if (!VD) {
6384           if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
6385             VD = Private;
6386           } else {
6387             PrivateRef = buildCapture(*this, D, ISC.getLoopDeclRefExpr(),
6388                                       /*WithInit=*/false);
6389             VD = cast<VarDecl>(PrivateRef->getDecl());
6390           }
6391         }
6392         DSAStack->addLoopControlVariable(D, VD);
6393         const Decl *LD = DSAStack->getPossiblyLoopCunter();
6394         if (LD != D->getCanonicalDecl()) {
6395           DSAStack->resetPossibleLoopCounter();
6396           if (auto *Var = dyn_cast_or_null<VarDecl>(LD))
6397             MarkDeclarationsReferencedInExpr(
6398                 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var),
6399                                  Var->getType().getNonLValueExprType(Context),
6400                                  ForLoc, /*RefersToCapture=*/true));
6401         }
6402         OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
6403         // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables
6404         // Referenced in a Construct, C/C++]. The loop iteration variable in the
6405         // associated for-loop of a simd construct with just one associated
6406         // for-loop may be listed in a linear clause with a constant-linear-step
6407         // that is the increment of the associated for-loop. The loop iteration
6408         // variable(s) in the associated for-loop(s) of a for or parallel for
6409         // construct may be listed in a private or lastprivate clause.
6410         DSAStackTy::DSAVarData DVar =
6411             DSAStack->getTopDSA(D, /*FromParent=*/false);
6412         // If LoopVarRefExpr is nullptr it means the corresponding loop variable
6413         // is declared in the loop and it is predetermined as a private.
6414         Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
6415         OpenMPClauseKind PredeterminedCKind =
6416             isOpenMPSimdDirective(DKind)
6417                 ? (DSAStack->hasMutipleLoops() ? OMPC_lastprivate : OMPC_linear)
6418                 : OMPC_private;
6419         if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
6420               DVar.CKind != PredeterminedCKind && DVar.RefExpr &&
6421               (LangOpts.OpenMP <= 45 || (DVar.CKind != OMPC_lastprivate &&
6422                                          DVar.CKind != OMPC_private))) ||
6423              ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
6424                isOpenMPDistributeDirective(DKind)) &&
6425               !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
6426               DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
6427             (DVar.CKind != OMPC_private || DVar.RefExpr)) {
6428           Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa)
6429               << getOpenMPClauseName(DVar.CKind)
6430               << getOpenMPDirectiveName(DKind)
6431               << getOpenMPClauseName(PredeterminedCKind);
6432           if (DVar.RefExpr == nullptr)
6433             DVar.CKind = PredeterminedCKind;
6434           reportOriginalDsa(*this, DSAStack, D, DVar,
6435                             /*IsLoopIterVar=*/true);
6436         } else if (LoopDeclRefExpr) {
6437           // Make the loop iteration variable private (for worksharing
6438           // constructs), linear (for simd directives with the only one
6439           // associated loop) or lastprivate (for simd directives with several
6440           // collapsed or ordered loops).
6441           if (DVar.CKind == OMPC_unknown)
6442             DSAStack->addDSA(D, LoopDeclRefExpr, PredeterminedCKind,
6443                              PrivateRef);
6444         }
6445       }
6446     }
6447     DSAStack->setAssociatedLoops(AssociatedLoops - 1);
6448   }
6449 }
6450 
6451 /// Called on a for stmt to check and extract its iteration space
6452 /// for further processing (such as collapsing).
6453 static bool checkOpenMPIterationSpace(
6454     OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
6455     unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
6456     unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
6457     Expr *OrderedLoopCountExpr,
6458     Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
6459     llvm::MutableArrayRef<LoopIterationSpace> ResultIterSpaces,
6460     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
6461   // OpenMP [2.6, Canonical Loop Form]
6462   //   for (init-expr; test-expr; incr-expr) structured-block
6463   auto *For = dyn_cast_or_null<ForStmt>(S);
6464   if (!For) {
6465     SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for)
6466         << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
6467         << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount
6468         << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
6469     if (TotalNestedLoopCount > 1) {
6470       if (CollapseLoopCountExpr && OrderedLoopCountExpr)
6471         SemaRef.Diag(DSA.getConstructLoc(),
6472                      diag::note_omp_collapse_ordered_expr)
6473             << 2 << CollapseLoopCountExpr->getSourceRange()
6474             << OrderedLoopCountExpr->getSourceRange();
6475       else if (CollapseLoopCountExpr)
6476         SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
6477                      diag::note_omp_collapse_ordered_expr)
6478             << 0 << CollapseLoopCountExpr->getSourceRange();
6479       else
6480         SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
6481                      diag::note_omp_collapse_ordered_expr)
6482             << 1 << OrderedLoopCountExpr->getSourceRange();
6483     }
6484     return true;
6485   }
6486   assert(For->getBody());
6487 
6488   OpenMPIterationSpaceChecker ISC(SemaRef, DSA, For->getForLoc());
6489 
6490   // Check init.
6491   Stmt *Init = For->getInit();
6492   if (ISC.checkAndSetInit(Init))
6493     return true;
6494 
6495   bool HasErrors = false;
6496 
6497   // Check loop variable's type.
6498   if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
6499     // OpenMP [2.6, Canonical Loop Form]
6500     // Var is one of the following:
6501     //   A variable of signed or unsigned integer type.
6502     //   For C++, a variable of a random access iterator type.
6503     //   For C, a variable of a pointer type.
6504     QualType VarType = LCDecl->getType().getNonReferenceType();
6505     if (!VarType->isDependentType() && !VarType->isIntegerType() &&
6506         !VarType->isPointerType() &&
6507         !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
6508       SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type)
6509           << SemaRef.getLangOpts().CPlusPlus;
6510       HasErrors = true;
6511     }
6512 
6513     // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
6514     // a Construct
6515     // The loop iteration variable(s) in the associated for-loop(s) of a for or
6516     // parallel for construct is (are) private.
6517     // The loop iteration variable in the associated for-loop of a simd
6518     // construct with just one associated for-loop is linear with a
6519     // constant-linear-step that is the increment of the associated for-loop.
6520     // Exclude loop var from the list of variables with implicitly defined data
6521     // sharing attributes.
6522     VarsWithImplicitDSA.erase(LCDecl);
6523 
6524     assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
6525 
6526     // Check test-expr.
6527     HasErrors |= ISC.checkAndSetCond(For->getCond());
6528 
6529     // Check incr-expr.
6530     HasErrors |= ISC.checkAndSetInc(For->getInc());
6531   }
6532 
6533   if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
6534     return HasErrors;
6535 
6536   // Build the loop's iteration space representation.
6537   ResultIterSpaces[CurrentNestedLoopCount].PreCond =
6538       ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures);
6539   ResultIterSpaces[CurrentNestedLoopCount].NumIterations =
6540       ISC.buildNumIterations(DSA.getCurScope(), ResultIterSpaces,
6541                              (isOpenMPWorksharingDirective(DKind) ||
6542                               isOpenMPTaskLoopDirective(DKind) ||
6543                               isOpenMPDistributeDirective(DKind)),
6544                              Captures);
6545   ResultIterSpaces[CurrentNestedLoopCount].CounterVar =
6546       ISC.buildCounterVar(Captures, DSA);
6547   ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar =
6548       ISC.buildPrivateCounterVar();
6549   ResultIterSpaces[CurrentNestedLoopCount].CounterInit = ISC.buildCounterInit();
6550   ResultIterSpaces[CurrentNestedLoopCount].CounterStep = ISC.buildCounterStep();
6551   ResultIterSpaces[CurrentNestedLoopCount].InitSrcRange = ISC.getInitSrcRange();
6552   ResultIterSpaces[CurrentNestedLoopCount].CondSrcRange =
6553       ISC.getConditionSrcRange();
6554   ResultIterSpaces[CurrentNestedLoopCount].IncSrcRange =
6555       ISC.getIncrementSrcRange();
6556   ResultIterSpaces[CurrentNestedLoopCount].Subtract = ISC.shouldSubtractStep();
6557   ResultIterSpaces[CurrentNestedLoopCount].IsStrictCompare =
6558       ISC.isStrictTestOp();
6559   std::tie(ResultIterSpaces[CurrentNestedLoopCount].MinValue,
6560            ResultIterSpaces[CurrentNestedLoopCount].MaxValue) =
6561       ISC.buildMinMaxValues(DSA.getCurScope(), Captures);
6562   ResultIterSpaces[CurrentNestedLoopCount].FinalCondition =
6563       ISC.buildFinalCondition(DSA.getCurScope());
6564   ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularLB =
6565       ISC.doesInitDependOnLC();
6566   ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularUB =
6567       ISC.doesCondDependOnLC();
6568   ResultIterSpaces[CurrentNestedLoopCount].LoopDependentIdx =
6569       ISC.getLoopDependentIdx();
6570 
6571   HasErrors |=
6572       (ResultIterSpaces[CurrentNestedLoopCount].PreCond == nullptr ||
6573        ResultIterSpaces[CurrentNestedLoopCount].NumIterations == nullptr ||
6574        ResultIterSpaces[CurrentNestedLoopCount].CounterVar == nullptr ||
6575        ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar == nullptr ||
6576        ResultIterSpaces[CurrentNestedLoopCount].CounterInit == nullptr ||
6577        ResultIterSpaces[CurrentNestedLoopCount].CounterStep == nullptr);
6578   if (!HasErrors && DSA.isOrderedRegion()) {
6579     if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
6580       if (CurrentNestedLoopCount <
6581           DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
6582         DSA.getOrderedRegionParam().second->setLoopNumIterations(
6583             CurrentNestedLoopCount,
6584             ResultIterSpaces[CurrentNestedLoopCount].NumIterations);
6585         DSA.getOrderedRegionParam().second->setLoopCounter(
6586             CurrentNestedLoopCount,
6587             ResultIterSpaces[CurrentNestedLoopCount].CounterVar);
6588       }
6589     }
6590     for (auto &Pair : DSA.getDoacrossDependClauses()) {
6591       if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) {
6592         // Erroneous case - clause has some problems.
6593         continue;
6594       }
6595       if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink &&
6596           Pair.second.size() <= CurrentNestedLoopCount) {
6597         // Erroneous case - clause has some problems.
6598         Pair.first->setLoopData(CurrentNestedLoopCount, nullptr);
6599         continue;
6600       }
6601       Expr *CntValue;
6602       if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
6603         CntValue = ISC.buildOrderedLoopData(
6604             DSA.getCurScope(),
6605             ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures,
6606             Pair.first->getDependencyLoc());
6607       else
6608         CntValue = ISC.buildOrderedLoopData(
6609             DSA.getCurScope(),
6610             ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures,
6611             Pair.first->getDependencyLoc(),
6612             Pair.second[CurrentNestedLoopCount].first,
6613             Pair.second[CurrentNestedLoopCount].second);
6614       Pair.first->setLoopData(CurrentNestedLoopCount, CntValue);
6615     }
6616   }
6617 
6618   return HasErrors;
6619 }
6620 
6621 /// Build 'VarRef = Start.
6622 static ExprResult
6623 buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
6624                  ExprResult Start, bool IsNonRectangularLB,
6625                  llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
6626   // Build 'VarRef = Start.
6627   ExprResult NewStart = IsNonRectangularLB
6628                             ? Start.get()
6629                             : tryBuildCapture(SemaRef, Start.get(), Captures);
6630   if (!NewStart.isUsable())
6631     return ExprError();
6632   if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
6633                                    VarRef.get()->getType())) {
6634     NewStart = SemaRef.PerformImplicitConversion(
6635         NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
6636         /*AllowExplicit=*/true);
6637     if (!NewStart.isUsable())
6638       return ExprError();
6639   }
6640 
6641   ExprResult Init =
6642       SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
6643   return Init;
6644 }
6645 
6646 /// Build 'VarRef = Start + Iter * Step'.
6647 static ExprResult buildCounterUpdate(
6648     Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
6649     ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
6650     bool IsNonRectangularLB,
6651     llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
6652   // Add parentheses (for debugging purposes only).
6653   Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
6654   if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
6655       !Step.isUsable())
6656     return ExprError();
6657 
6658   ExprResult NewStep = Step;
6659   if (Captures)
6660     NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
6661   if (NewStep.isInvalid())
6662     return ExprError();
6663   ExprResult Update =
6664       SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
6665   if (!Update.isUsable())
6666     return ExprError();
6667 
6668   // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
6669   // 'VarRef = Start (+|-) Iter * Step'.
6670   if (!Start.isUsable())
6671     return ExprError();
6672   ExprResult NewStart = SemaRef.ActOnParenExpr(Loc, Loc, Start.get());
6673   if (!NewStart.isUsable())
6674     return ExprError();
6675   if (Captures && !IsNonRectangularLB)
6676     NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
6677   if (NewStart.isInvalid())
6678     return ExprError();
6679 
6680   // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
6681   ExprResult SavedUpdate = Update;
6682   ExprResult UpdateVal;
6683   if (VarRef.get()->getType()->isOverloadableType() ||
6684       NewStart.get()->getType()->isOverloadableType() ||
6685       Update.get()->getType()->isOverloadableType()) {
6686     Sema::TentativeAnalysisScope Trap(SemaRef);
6687 
6688     Update =
6689         SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
6690     if (Update.isUsable()) {
6691       UpdateVal =
6692           SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
6693                              VarRef.get(), SavedUpdate.get());
6694       if (UpdateVal.isUsable()) {
6695         Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
6696                                             UpdateVal.get());
6697       }
6698     }
6699   }
6700 
6701   // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
6702   if (!Update.isUsable() || !UpdateVal.isUsable()) {
6703     Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
6704                                 NewStart.get(), SavedUpdate.get());
6705     if (!Update.isUsable())
6706       return ExprError();
6707 
6708     if (!SemaRef.Context.hasSameType(Update.get()->getType(),
6709                                      VarRef.get()->getType())) {
6710       Update = SemaRef.PerformImplicitConversion(
6711           Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
6712       if (!Update.isUsable())
6713         return ExprError();
6714     }
6715 
6716     Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
6717   }
6718   return Update;
6719 }
6720 
6721 /// Convert integer expression \a E to make it have at least \a Bits
6722 /// bits.
6723 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
6724   if (E == nullptr)
6725     return ExprError();
6726   ASTContext &C = SemaRef.Context;
6727   QualType OldType = E->getType();
6728   unsigned HasBits = C.getTypeSize(OldType);
6729   if (HasBits >= Bits)
6730     return ExprResult(E);
6731   // OK to convert to signed, because new type has more bits than old.
6732   QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
6733   return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
6734                                            true);
6735 }
6736 
6737 /// Check if the given expression \a E is a constant integer that fits
6738 /// into \a Bits bits.
6739 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
6740   if (E == nullptr)
6741     return false;
6742   llvm::APSInt Result;
6743   if (E->isIntegerConstantExpr(Result, SemaRef.Context))
6744     return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
6745   return false;
6746 }
6747 
6748 /// Build preinits statement for the given declarations.
6749 static Stmt *buildPreInits(ASTContext &Context,
6750                            MutableArrayRef<Decl *> PreInits) {
6751   if (!PreInits.empty()) {
6752     return new (Context) DeclStmt(
6753         DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
6754         SourceLocation(), SourceLocation());
6755   }
6756   return nullptr;
6757 }
6758 
6759 /// Build preinits statement for the given declarations.
6760 static Stmt *
6761 buildPreInits(ASTContext &Context,
6762               const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
6763   if (!Captures.empty()) {
6764     SmallVector<Decl *, 16> PreInits;
6765     for (const auto &Pair : Captures)
6766       PreInits.push_back(Pair.second->getDecl());
6767     return buildPreInits(Context, PreInits);
6768   }
6769   return nullptr;
6770 }
6771 
6772 /// Build postupdate expression for the given list of postupdates expressions.
6773 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
6774   Expr *PostUpdate = nullptr;
6775   if (!PostUpdates.empty()) {
6776     for (Expr *E : PostUpdates) {
6777       Expr *ConvE = S.BuildCStyleCastExpr(
6778                          E->getExprLoc(),
6779                          S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
6780                          E->getExprLoc(), E)
6781                         .get();
6782       PostUpdate = PostUpdate
6783                        ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
6784                                               PostUpdate, ConvE)
6785                              .get()
6786                        : ConvE;
6787     }
6788   }
6789   return PostUpdate;
6790 }
6791 
6792 /// Called on a for stmt to check itself and nested loops (if any).
6793 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
6794 /// number of collapsed loops otherwise.
6795 static unsigned
6796 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
6797                 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
6798                 DSAStackTy &DSA,
6799                 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
6800                 OMPLoopDirective::HelperExprs &Built) {
6801   unsigned NestedLoopCount = 1;
6802   if (CollapseLoopCountExpr) {
6803     // Found 'collapse' clause - calculate collapse number.
6804     Expr::EvalResult Result;
6805     if (!CollapseLoopCountExpr->isValueDependent() &&
6806         CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
6807       NestedLoopCount = Result.Val.getInt().getLimitedValue();
6808     } else {
6809       Built.clear(/*Size=*/1);
6810       return 1;
6811     }
6812   }
6813   unsigned OrderedLoopCount = 1;
6814   if (OrderedLoopCountExpr) {
6815     // Found 'ordered' clause - calculate collapse number.
6816     Expr::EvalResult EVResult;
6817     if (!OrderedLoopCountExpr->isValueDependent() &&
6818         OrderedLoopCountExpr->EvaluateAsInt(EVResult,
6819                                             SemaRef.getASTContext())) {
6820       llvm::APSInt Result = EVResult.Val.getInt();
6821       if (Result.getLimitedValue() < NestedLoopCount) {
6822         SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
6823                      diag::err_omp_wrong_ordered_loop_count)
6824             << OrderedLoopCountExpr->getSourceRange();
6825         SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
6826                      diag::note_collapse_loop_count)
6827             << CollapseLoopCountExpr->getSourceRange();
6828       }
6829       OrderedLoopCount = Result.getLimitedValue();
6830     } else {
6831       Built.clear(/*Size=*/1);
6832       return 1;
6833     }
6834   }
6835   // This is helper routine for loop directives (e.g., 'for', 'simd',
6836   // 'for simd', etc.).
6837   llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
6838   SmallVector<LoopIterationSpace, 4> IterSpaces(
6839       std::max(OrderedLoopCount, NestedLoopCount));
6840   Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
6841   for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
6842     if (checkOpenMPIterationSpace(
6843             DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
6844             std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
6845             OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces, Captures))
6846       return 0;
6847     // Move on to the next nested for loop, or to the loop body.
6848     // OpenMP [2.8.1, simd construct, Restrictions]
6849     // All loops associated with the construct must be perfectly nested; that
6850     // is, there must be no intervening code nor any OpenMP directive between
6851     // any two loops.
6852     CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
6853   }
6854   for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) {
6855     if (checkOpenMPIterationSpace(
6856             DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
6857             std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
6858             OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces, Captures))
6859       return 0;
6860     if (Cnt > 0 && IterSpaces[Cnt].CounterVar) {
6861       // Handle initialization of captured loop iterator variables.
6862       auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar);
6863       if (isa<OMPCapturedExprDecl>(DRE->getDecl())) {
6864         Captures[DRE] = DRE;
6865       }
6866     }
6867     // Move on to the next nested for loop, or to the loop body.
6868     // OpenMP [2.8.1, simd construct, Restrictions]
6869     // All loops associated with the construct must be perfectly nested; that
6870     // is, there must be no intervening code nor any OpenMP directive between
6871     // any two loops.
6872     CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
6873   }
6874 
6875   Built.clear(/* size */ NestedLoopCount);
6876 
6877   if (SemaRef.CurContext->isDependentContext())
6878     return NestedLoopCount;
6879 
6880   // An example of what is generated for the following code:
6881   //
6882   //   #pragma omp simd collapse(2) ordered(2)
6883   //   for (i = 0; i < NI; ++i)
6884   //     for (k = 0; k < NK; ++k)
6885   //       for (j = J0; j < NJ; j+=2) {
6886   //         <loop body>
6887   //       }
6888   //
6889   // We generate the code below.
6890   // Note: the loop body may be outlined in CodeGen.
6891   // Note: some counters may be C++ classes, operator- is used to find number of
6892   // iterations and operator+= to calculate counter value.
6893   // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
6894   // or i64 is currently supported).
6895   //
6896   //   #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
6897   //   for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
6898   //     .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
6899   //     .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
6900   //     // similar updates for vars in clauses (e.g. 'linear')
6901   //     <loop body (using local i and j)>
6902   //   }
6903   //   i = NI; // assign final values of counters
6904   //   j = NJ;
6905   //
6906 
6907   // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
6908   // the iteration counts of the collapsed for loops.
6909   // Precondition tests if there is at least one iteration (all conditions are
6910   // true).
6911   auto PreCond = ExprResult(IterSpaces[0].PreCond);
6912   Expr *N0 = IterSpaces[0].NumIterations;
6913   ExprResult LastIteration32 =
6914       widenIterationCount(/*Bits=*/32,
6915                           SemaRef
6916                               .PerformImplicitConversion(
6917                                   N0->IgnoreImpCasts(), N0->getType(),
6918                                   Sema::AA_Converting, /*AllowExplicit=*/true)
6919                               .get(),
6920                           SemaRef);
6921   ExprResult LastIteration64 = widenIterationCount(
6922       /*Bits=*/64,
6923       SemaRef
6924           .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(),
6925                                      Sema::AA_Converting,
6926                                      /*AllowExplicit=*/true)
6927           .get(),
6928       SemaRef);
6929 
6930   if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
6931     return NestedLoopCount;
6932 
6933   ASTContext &C = SemaRef.Context;
6934   bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
6935 
6936   Scope *CurScope = DSA.getCurScope();
6937   for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
6938     if (PreCond.isUsable()) {
6939       PreCond =
6940           SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
6941                              PreCond.get(), IterSpaces[Cnt].PreCond);
6942     }
6943     Expr *N = IterSpaces[Cnt].NumIterations;
6944     SourceLocation Loc = N->getExprLoc();
6945     AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
6946     if (LastIteration32.isUsable())
6947       LastIteration32 = SemaRef.BuildBinOp(
6948           CurScope, Loc, BO_Mul, LastIteration32.get(),
6949           SemaRef
6950               .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
6951                                          Sema::AA_Converting,
6952                                          /*AllowExplicit=*/true)
6953               .get());
6954     if (LastIteration64.isUsable())
6955       LastIteration64 = SemaRef.BuildBinOp(
6956           CurScope, Loc, BO_Mul, LastIteration64.get(),
6957           SemaRef
6958               .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
6959                                          Sema::AA_Converting,
6960                                          /*AllowExplicit=*/true)
6961               .get());
6962   }
6963 
6964   // Choose either the 32-bit or 64-bit version.
6965   ExprResult LastIteration = LastIteration64;
6966   if (SemaRef.getLangOpts().OpenMPOptimisticCollapse ||
6967       (LastIteration32.isUsable() &&
6968        C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
6969        (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
6970         fitsInto(
6971             /*Bits=*/32,
6972             LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
6973             LastIteration64.get(), SemaRef))))
6974     LastIteration = LastIteration32;
6975   QualType VType = LastIteration.get()->getType();
6976   QualType RealVType = VType;
6977   QualType StrideVType = VType;
6978   if (isOpenMPTaskLoopDirective(DKind)) {
6979     VType =
6980         SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
6981     StrideVType =
6982         SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
6983   }
6984 
6985   if (!LastIteration.isUsable())
6986     return 0;
6987 
6988   // Save the number of iterations.
6989   ExprResult NumIterations = LastIteration;
6990   {
6991     LastIteration = SemaRef.BuildBinOp(
6992         CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
6993         LastIteration.get(),
6994         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
6995     if (!LastIteration.isUsable())
6996       return 0;
6997   }
6998 
6999   // Calculate the last iteration number beforehand instead of doing this on
7000   // each iteration. Do not do this if the number of iterations may be kfold-ed.
7001   llvm::APSInt Result;
7002   bool IsConstant =
7003       LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
7004   ExprResult CalcLastIteration;
7005   if (!IsConstant) {
7006     ExprResult SaveRef =
7007         tryBuildCapture(SemaRef, LastIteration.get(), Captures);
7008     LastIteration = SaveRef;
7009 
7010     // Prepare SaveRef + 1.
7011     NumIterations = SemaRef.BuildBinOp(
7012         CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
7013         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
7014     if (!NumIterations.isUsable())
7015       return 0;
7016   }
7017 
7018   SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
7019 
7020   // Build variables passed into runtime, necessary for worksharing directives.
7021   ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
7022   if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
7023       isOpenMPDistributeDirective(DKind)) {
7024     // Lower bound variable, initialized with zero.
7025     VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
7026     LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
7027     SemaRef.AddInitializerToDecl(LBDecl,
7028                                  SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
7029                                  /*DirectInit*/ false);
7030 
7031     // Upper bound variable, initialized with last iteration number.
7032     VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
7033     UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
7034     SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
7035                                  /*DirectInit*/ false);
7036 
7037     // A 32-bit variable-flag where runtime returns 1 for the last iteration.
7038     // This will be used to implement clause 'lastprivate'.
7039     QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
7040     VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
7041     IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
7042     SemaRef.AddInitializerToDecl(ILDecl,
7043                                  SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
7044                                  /*DirectInit*/ false);
7045 
7046     // Stride variable returned by runtime (we initialize it to 1 by default).
7047     VarDecl *STDecl =
7048         buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
7049     ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
7050     SemaRef.AddInitializerToDecl(STDecl,
7051                                  SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
7052                                  /*DirectInit*/ false);
7053 
7054     // Build expression: UB = min(UB, LastIteration)
7055     // It is necessary for CodeGen of directives with static scheduling.
7056     ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
7057                                                 UB.get(), LastIteration.get());
7058     ExprResult CondOp = SemaRef.ActOnConditionalOp(
7059         LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(),
7060         LastIteration.get(), UB.get());
7061     EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
7062                              CondOp.get());
7063     EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false);
7064 
7065     // If we have a combined directive that combines 'distribute', 'for' or
7066     // 'simd' we need to be able to access the bounds of the schedule of the
7067     // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
7068     // by scheduling 'distribute' have to be passed to the schedule of 'for'.
7069     if (isOpenMPLoopBoundSharingDirective(DKind)) {
7070       // Lower bound variable, initialized with zero.
7071       VarDecl *CombLBDecl =
7072           buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
7073       CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
7074       SemaRef.AddInitializerToDecl(
7075           CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
7076           /*DirectInit*/ false);
7077 
7078       // Upper bound variable, initialized with last iteration number.
7079       VarDecl *CombUBDecl =
7080           buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
7081       CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
7082       SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
7083                                    /*DirectInit*/ false);
7084 
7085       ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
7086           CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
7087       ExprResult CombCondOp =
7088           SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
7089                                      LastIteration.get(), CombUB.get());
7090       CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
7091                                    CombCondOp.get());
7092       CombEUB =
7093           SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false);
7094 
7095       const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
7096       // We expect to have at least 2 more parameters than the 'parallel'
7097       // directive does - the lower and upper bounds of the previous schedule.
7098       assert(CD->getNumParams() >= 4 &&
7099              "Unexpected number of parameters in loop combined directive");
7100 
7101       // Set the proper type for the bounds given what we learned from the
7102       // enclosed loops.
7103       ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
7104       ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
7105 
7106       // Previous lower and upper bounds are obtained from the region
7107       // parameters.
7108       PrevLB =
7109           buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
7110       PrevUB =
7111           buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
7112     }
7113   }
7114 
7115   // Build the iteration variable and its initialization before loop.
7116   ExprResult IV;
7117   ExprResult Init, CombInit;
7118   {
7119     VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
7120     IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
7121     Expr *RHS =
7122         (isOpenMPWorksharingDirective(DKind) ||
7123          isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
7124             ? LB.get()
7125             : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
7126     Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
7127     Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false);
7128 
7129     if (isOpenMPLoopBoundSharingDirective(DKind)) {
7130       Expr *CombRHS =
7131           (isOpenMPWorksharingDirective(DKind) ||
7132            isOpenMPTaskLoopDirective(DKind) ||
7133            isOpenMPDistributeDirective(DKind))
7134               ? CombLB.get()
7135               : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
7136       CombInit =
7137           SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
7138       CombInit =
7139           SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false);
7140     }
7141   }
7142 
7143   bool UseStrictCompare =
7144       RealVType->hasUnsignedIntegerRepresentation() &&
7145       llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) {
7146         return LIS.IsStrictCompare;
7147       });
7148   // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for
7149   // unsigned IV)) for worksharing loops.
7150   SourceLocation CondLoc = AStmt->getBeginLoc();
7151   Expr *BoundUB = UB.get();
7152   if (UseStrictCompare) {
7153     BoundUB =
7154         SemaRef
7155             .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB,
7156                         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
7157             .get();
7158     BoundUB =
7159         SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get();
7160   }
7161   ExprResult Cond =
7162       (isOpenMPWorksharingDirective(DKind) ||
7163        isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
7164           ? SemaRef.BuildBinOp(CurScope, CondLoc,
7165                                UseStrictCompare ? BO_LT : BO_LE, IV.get(),
7166                                BoundUB)
7167           : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
7168                                NumIterations.get());
7169   ExprResult CombDistCond;
7170   if (isOpenMPLoopBoundSharingDirective(DKind)) {
7171     CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
7172                                       NumIterations.get());
7173   }
7174 
7175   ExprResult CombCond;
7176   if (isOpenMPLoopBoundSharingDirective(DKind)) {
7177     Expr *BoundCombUB = CombUB.get();
7178     if (UseStrictCompare) {
7179       BoundCombUB =
7180           SemaRef
7181               .BuildBinOp(
7182                   CurScope, CondLoc, BO_Add, BoundCombUB,
7183                   SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
7184               .get();
7185       BoundCombUB =
7186           SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false)
7187               .get();
7188     }
7189     CombCond =
7190         SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE,
7191                            IV.get(), BoundCombUB);
7192   }
7193   // Loop increment (IV = IV + 1)
7194   SourceLocation IncLoc = AStmt->getBeginLoc();
7195   ExprResult Inc =
7196       SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
7197                          SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
7198   if (!Inc.isUsable())
7199     return 0;
7200   Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
7201   Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false);
7202   if (!Inc.isUsable())
7203     return 0;
7204 
7205   // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
7206   // Used for directives with static scheduling.
7207   // In combined construct, add combined version that use CombLB and CombUB
7208   // base variables for the update
7209   ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
7210   if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
7211       isOpenMPDistributeDirective(DKind)) {
7212     // LB + ST
7213     NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
7214     if (!NextLB.isUsable())
7215       return 0;
7216     // LB = LB + ST
7217     NextLB =
7218         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
7219     NextLB =
7220         SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false);
7221     if (!NextLB.isUsable())
7222       return 0;
7223     // UB + ST
7224     NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
7225     if (!NextUB.isUsable())
7226       return 0;
7227     // UB = UB + ST
7228     NextUB =
7229         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
7230     NextUB =
7231         SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false);
7232     if (!NextUB.isUsable())
7233       return 0;
7234     if (isOpenMPLoopBoundSharingDirective(DKind)) {
7235       CombNextLB =
7236           SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
7237       if (!NextLB.isUsable())
7238         return 0;
7239       // LB = LB + ST
7240       CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
7241                                       CombNextLB.get());
7242       CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(),
7243                                                /*DiscardedValue*/ false);
7244       if (!CombNextLB.isUsable())
7245         return 0;
7246       // UB + ST
7247       CombNextUB =
7248           SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
7249       if (!CombNextUB.isUsable())
7250         return 0;
7251       // UB = UB + ST
7252       CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
7253                                       CombNextUB.get());
7254       CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(),
7255                                                /*DiscardedValue*/ false);
7256       if (!CombNextUB.isUsable())
7257         return 0;
7258     }
7259   }
7260 
7261   // Create increment expression for distribute loop when combined in a same
7262   // directive with for as IV = IV + ST; ensure upper bound expression based
7263   // on PrevUB instead of NumIterations - used to implement 'for' when found
7264   // in combination with 'distribute', like in 'distribute parallel for'
7265   SourceLocation DistIncLoc = AStmt->getBeginLoc();
7266   ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond;
7267   if (isOpenMPLoopBoundSharingDirective(DKind)) {
7268     DistCond = SemaRef.BuildBinOp(
7269         CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB);
7270     assert(DistCond.isUsable() && "distribute cond expr was not built");
7271 
7272     DistInc =
7273         SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
7274     assert(DistInc.isUsable() && "distribute inc expr was not built");
7275     DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
7276                                  DistInc.get());
7277     DistInc =
7278         SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false);
7279     assert(DistInc.isUsable() && "distribute inc expr was not built");
7280 
7281     // Build expression: UB = min(UB, prevUB) for #for in composite or combined
7282     // construct
7283     SourceLocation DistEUBLoc = AStmt->getBeginLoc();
7284     ExprResult IsUBGreater =
7285         SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
7286     ExprResult CondOp = SemaRef.ActOnConditionalOp(
7287         DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
7288     PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
7289                                  CondOp.get());
7290     PrevEUB =
7291         SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false);
7292 
7293     // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in
7294     // parallel for is in combination with a distribute directive with
7295     // schedule(static, 1)
7296     Expr *BoundPrevUB = PrevUB.get();
7297     if (UseStrictCompare) {
7298       BoundPrevUB =
7299           SemaRef
7300               .BuildBinOp(
7301                   CurScope, CondLoc, BO_Add, BoundPrevUB,
7302                   SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
7303               .get();
7304       BoundPrevUB =
7305           SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false)
7306               .get();
7307     }
7308     ParForInDistCond =
7309         SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE,
7310                            IV.get(), BoundPrevUB);
7311   }
7312 
7313   // Build updates and final values of the loop counters.
7314   bool HasErrors = false;
7315   Built.Counters.resize(NestedLoopCount);
7316   Built.Inits.resize(NestedLoopCount);
7317   Built.Updates.resize(NestedLoopCount);
7318   Built.Finals.resize(NestedLoopCount);
7319   Built.DependentCounters.resize(NestedLoopCount);
7320   Built.DependentInits.resize(NestedLoopCount);
7321   Built.FinalsConditions.resize(NestedLoopCount);
7322   {
7323     // We implement the following algorithm for obtaining the
7324     // original loop iteration variable values based on the
7325     // value of the collapsed loop iteration variable IV.
7326     //
7327     // Let n+1 be the number of collapsed loops in the nest.
7328     // Iteration variables (I0, I1, .... In)
7329     // Iteration counts (N0, N1, ... Nn)
7330     //
7331     // Acc = IV;
7332     //
7333     // To compute Ik for loop k, 0 <= k <= n, generate:
7334     //    Prod = N(k+1) * N(k+2) * ... * Nn;
7335     //    Ik = Acc / Prod;
7336     //    Acc -= Ik * Prod;
7337     //
7338     ExprResult Acc = IV;
7339     for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
7340       LoopIterationSpace &IS = IterSpaces[Cnt];
7341       SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
7342       ExprResult Iter;
7343 
7344       // Compute prod
7345       ExprResult Prod =
7346           SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
7347       for (unsigned int K = Cnt+1; K < NestedLoopCount; ++K)
7348         Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(),
7349                                   IterSpaces[K].NumIterations);
7350 
7351       // Iter = Acc / Prod
7352       // If there is at least one more inner loop to avoid
7353       // multiplication by 1.
7354       if (Cnt + 1 < NestedLoopCount)
7355         Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div,
7356                                   Acc.get(), Prod.get());
7357       else
7358         Iter = Acc;
7359       if (!Iter.isUsable()) {
7360         HasErrors = true;
7361         break;
7362       }
7363 
7364       // Update Acc:
7365       // Acc -= Iter * Prod
7366       // Check if there is at least one more inner loop to avoid
7367       // multiplication by 1.
7368       if (Cnt + 1 < NestedLoopCount)
7369         Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul,
7370                                   Iter.get(), Prod.get());
7371       else
7372         Prod = Iter;
7373       Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub,
7374                                Acc.get(), Prod.get());
7375 
7376       // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
7377       auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
7378       DeclRefExpr *CounterVar = buildDeclRefExpr(
7379           SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
7380           /*RefersToCapture=*/true);
7381       ExprResult Init =
7382           buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
7383                            IS.CounterInit, IS.IsNonRectangularLB, Captures);
7384       if (!Init.isUsable()) {
7385         HasErrors = true;
7386         break;
7387       }
7388       ExprResult Update = buildCounterUpdate(
7389           SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
7390           IS.CounterStep, IS.Subtract, IS.IsNonRectangularLB, &Captures);
7391       if (!Update.isUsable()) {
7392         HasErrors = true;
7393         break;
7394       }
7395 
7396       // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
7397       ExprResult Final =
7398           buildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
7399                              IS.CounterInit, IS.NumIterations, IS.CounterStep,
7400                              IS.Subtract, IS.IsNonRectangularLB, &Captures);
7401       if (!Final.isUsable()) {
7402         HasErrors = true;
7403         break;
7404       }
7405 
7406       if (!Update.isUsable() || !Final.isUsable()) {
7407         HasErrors = true;
7408         break;
7409       }
7410       // Save results
7411       Built.Counters[Cnt] = IS.CounterVar;
7412       Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
7413       Built.Inits[Cnt] = Init.get();
7414       Built.Updates[Cnt] = Update.get();
7415       Built.Finals[Cnt] = Final.get();
7416       Built.DependentCounters[Cnt] = nullptr;
7417       Built.DependentInits[Cnt] = nullptr;
7418       Built.FinalsConditions[Cnt] = nullptr;
7419       if (IS.IsNonRectangularLB) {
7420         Built.DependentCounters[Cnt] =
7421             Built.Counters[NestedLoopCount - 1 - IS.LoopDependentIdx];
7422         Built.DependentInits[Cnt] =
7423             Built.Inits[NestedLoopCount - 1 - IS.LoopDependentIdx];
7424         Built.FinalsConditions[Cnt] = IS.FinalCondition;
7425       }
7426     }
7427   }
7428 
7429   if (HasErrors)
7430     return 0;
7431 
7432   // Save results
7433   Built.IterationVarRef = IV.get();
7434   Built.LastIteration = LastIteration.get();
7435   Built.NumIterations = NumIterations.get();
7436   Built.CalcLastIteration = SemaRef
7437                                 .ActOnFinishFullExpr(CalcLastIteration.get(),
7438                                                      /*DiscardedValue=*/false)
7439                                 .get();
7440   Built.PreCond = PreCond.get();
7441   Built.PreInits = buildPreInits(C, Captures);
7442   Built.Cond = Cond.get();
7443   Built.Init = Init.get();
7444   Built.Inc = Inc.get();
7445   Built.LB = LB.get();
7446   Built.UB = UB.get();
7447   Built.IL = IL.get();
7448   Built.ST = ST.get();
7449   Built.EUB = EUB.get();
7450   Built.NLB = NextLB.get();
7451   Built.NUB = NextUB.get();
7452   Built.PrevLB = PrevLB.get();
7453   Built.PrevUB = PrevUB.get();
7454   Built.DistInc = DistInc.get();
7455   Built.PrevEUB = PrevEUB.get();
7456   Built.DistCombinedFields.LB = CombLB.get();
7457   Built.DistCombinedFields.UB = CombUB.get();
7458   Built.DistCombinedFields.EUB = CombEUB.get();
7459   Built.DistCombinedFields.Init = CombInit.get();
7460   Built.DistCombinedFields.Cond = CombCond.get();
7461   Built.DistCombinedFields.NLB = CombNextLB.get();
7462   Built.DistCombinedFields.NUB = CombNextUB.get();
7463   Built.DistCombinedFields.DistCond = CombDistCond.get();
7464   Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get();
7465 
7466   return NestedLoopCount;
7467 }
7468 
7469 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
7470   auto CollapseClauses =
7471       OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
7472   if (CollapseClauses.begin() != CollapseClauses.end())
7473     return (*CollapseClauses.begin())->getNumForLoops();
7474   return nullptr;
7475 }
7476 
7477 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
7478   auto OrderedClauses =
7479       OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
7480   if (OrderedClauses.begin() != OrderedClauses.end())
7481     return (*OrderedClauses.begin())->getNumForLoops();
7482   return nullptr;
7483 }
7484 
7485 static bool checkSimdlenSafelenSpecified(Sema &S,
7486                                          const ArrayRef<OMPClause *> Clauses) {
7487   const OMPSafelenClause *Safelen = nullptr;
7488   const OMPSimdlenClause *Simdlen = nullptr;
7489 
7490   for (const OMPClause *Clause : Clauses) {
7491     if (Clause->getClauseKind() == OMPC_safelen)
7492       Safelen = cast<OMPSafelenClause>(Clause);
7493     else if (Clause->getClauseKind() == OMPC_simdlen)
7494       Simdlen = cast<OMPSimdlenClause>(Clause);
7495     if (Safelen && Simdlen)
7496       break;
7497   }
7498 
7499   if (Simdlen && Safelen) {
7500     const Expr *SimdlenLength = Simdlen->getSimdlen();
7501     const Expr *SafelenLength = Safelen->getSafelen();
7502     if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
7503         SimdlenLength->isInstantiationDependent() ||
7504         SimdlenLength->containsUnexpandedParameterPack())
7505       return false;
7506     if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
7507         SafelenLength->isInstantiationDependent() ||
7508         SafelenLength->containsUnexpandedParameterPack())
7509       return false;
7510     Expr::EvalResult SimdlenResult, SafelenResult;
7511     SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context);
7512     SafelenLength->EvaluateAsInt(SafelenResult, S.Context);
7513     llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt();
7514     llvm::APSInt SafelenRes = SafelenResult.Val.getInt();
7515     // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
7516     // If both simdlen and safelen clauses are specified, the value of the
7517     // simdlen parameter must be less than or equal to the value of the safelen
7518     // parameter.
7519     if (SimdlenRes > SafelenRes) {
7520       S.Diag(SimdlenLength->getExprLoc(),
7521              diag::err_omp_wrong_simdlen_safelen_values)
7522           << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
7523       return true;
7524     }
7525   }
7526   return false;
7527 }
7528 
7529 StmtResult
7530 Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
7531                                SourceLocation StartLoc, SourceLocation EndLoc,
7532                                VarsWithInheritedDSAType &VarsWithImplicitDSA) {
7533   if (!AStmt)
7534     return StmtError();
7535 
7536   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7537   OMPLoopDirective::HelperExprs B;
7538   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7539   // define the nested loops number.
7540   unsigned NestedLoopCount = checkOpenMPLoop(
7541       OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
7542       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
7543   if (NestedLoopCount == 0)
7544     return StmtError();
7545 
7546   assert((CurContext->isDependentContext() || B.builtAll()) &&
7547          "omp simd loop exprs were not built");
7548 
7549   if (!CurContext->isDependentContext()) {
7550     // Finalize the clauses that need pre-built expressions for CodeGen.
7551     for (OMPClause *C : Clauses) {
7552       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7553         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7554                                      B.NumIterations, *this, CurScope,
7555                                      DSAStack))
7556           return StmtError();
7557     }
7558   }
7559 
7560   if (checkSimdlenSafelenSpecified(*this, Clauses))
7561     return StmtError();
7562 
7563   setFunctionHasBranchProtectedScope();
7564   return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
7565                                   Clauses, AStmt, B);
7566 }
7567 
7568 StmtResult
7569 Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
7570                               SourceLocation StartLoc, SourceLocation EndLoc,
7571                               VarsWithInheritedDSAType &VarsWithImplicitDSA) {
7572   if (!AStmt)
7573     return StmtError();
7574 
7575   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7576   OMPLoopDirective::HelperExprs B;
7577   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7578   // define the nested loops number.
7579   unsigned NestedLoopCount = checkOpenMPLoop(
7580       OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
7581       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
7582   if (NestedLoopCount == 0)
7583     return StmtError();
7584 
7585   assert((CurContext->isDependentContext() || B.builtAll()) &&
7586          "omp for loop exprs were not built");
7587 
7588   if (!CurContext->isDependentContext()) {
7589     // Finalize the clauses that need pre-built expressions for CodeGen.
7590     for (OMPClause *C : Clauses) {
7591       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7592         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7593                                      B.NumIterations, *this, CurScope,
7594                                      DSAStack))
7595           return StmtError();
7596     }
7597   }
7598 
7599   setFunctionHasBranchProtectedScope();
7600   return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
7601                                  Clauses, AStmt, B, DSAStack->isCancelRegion());
7602 }
7603 
7604 StmtResult Sema::ActOnOpenMPForSimdDirective(
7605     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7606     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
7607   if (!AStmt)
7608     return StmtError();
7609 
7610   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7611   OMPLoopDirective::HelperExprs B;
7612   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7613   // define the nested loops number.
7614   unsigned NestedLoopCount =
7615       checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
7616                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7617                       VarsWithImplicitDSA, B);
7618   if (NestedLoopCount == 0)
7619     return StmtError();
7620 
7621   assert((CurContext->isDependentContext() || B.builtAll()) &&
7622          "omp for simd loop exprs were not built");
7623 
7624   if (!CurContext->isDependentContext()) {
7625     // Finalize the clauses that need pre-built expressions for CodeGen.
7626     for (OMPClause *C : Clauses) {
7627       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7628         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7629                                      B.NumIterations, *this, CurScope,
7630                                      DSAStack))
7631           return StmtError();
7632     }
7633   }
7634 
7635   if (checkSimdlenSafelenSpecified(*this, Clauses))
7636     return StmtError();
7637 
7638   setFunctionHasBranchProtectedScope();
7639   return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
7640                                      Clauses, AStmt, B);
7641 }
7642 
7643 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
7644                                               Stmt *AStmt,
7645                                               SourceLocation StartLoc,
7646                                               SourceLocation EndLoc) {
7647   if (!AStmt)
7648     return StmtError();
7649 
7650   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7651   auto BaseStmt = AStmt;
7652   while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
7653     BaseStmt = CS->getCapturedStmt();
7654   if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
7655     auto S = C->children();
7656     if (S.begin() == S.end())
7657       return StmtError();
7658     // All associated statements must be '#pragma omp section' except for
7659     // the first one.
7660     for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
7661       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
7662         if (SectionStmt)
7663           Diag(SectionStmt->getBeginLoc(),
7664                diag::err_omp_sections_substmt_not_section);
7665         return StmtError();
7666       }
7667       cast<OMPSectionDirective>(SectionStmt)
7668           ->setHasCancel(DSAStack->isCancelRegion());
7669     }
7670   } else {
7671     Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt);
7672     return StmtError();
7673   }
7674 
7675   setFunctionHasBranchProtectedScope();
7676 
7677   return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
7678                                       DSAStack->isCancelRegion());
7679 }
7680 
7681 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
7682                                              SourceLocation StartLoc,
7683                                              SourceLocation EndLoc) {
7684   if (!AStmt)
7685     return StmtError();
7686 
7687   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7688 
7689   setFunctionHasBranchProtectedScope();
7690   DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
7691 
7692   return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
7693                                      DSAStack->isCancelRegion());
7694 }
7695 
7696 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
7697                                             Stmt *AStmt,
7698                                             SourceLocation StartLoc,
7699                                             SourceLocation EndLoc) {
7700   if (!AStmt)
7701     return StmtError();
7702 
7703   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7704 
7705   setFunctionHasBranchProtectedScope();
7706 
7707   // OpenMP [2.7.3, single Construct, Restrictions]
7708   // The copyprivate clause must not be used with the nowait clause.
7709   const OMPClause *Nowait = nullptr;
7710   const OMPClause *Copyprivate = nullptr;
7711   for (const OMPClause *Clause : Clauses) {
7712     if (Clause->getClauseKind() == OMPC_nowait)
7713       Nowait = Clause;
7714     else if (Clause->getClauseKind() == OMPC_copyprivate)
7715       Copyprivate = Clause;
7716     if (Copyprivate && Nowait) {
7717       Diag(Copyprivate->getBeginLoc(),
7718            diag::err_omp_single_copyprivate_with_nowait);
7719       Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here);
7720       return StmtError();
7721     }
7722   }
7723 
7724   return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
7725 }
7726 
7727 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
7728                                             SourceLocation StartLoc,
7729                                             SourceLocation EndLoc) {
7730   if (!AStmt)
7731     return StmtError();
7732 
7733   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7734 
7735   setFunctionHasBranchProtectedScope();
7736 
7737   return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
7738 }
7739 
7740 StmtResult Sema::ActOnOpenMPCriticalDirective(
7741     const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
7742     Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
7743   if (!AStmt)
7744     return StmtError();
7745 
7746   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7747 
7748   bool ErrorFound = false;
7749   llvm::APSInt Hint;
7750   SourceLocation HintLoc;
7751   bool DependentHint = false;
7752   for (const OMPClause *C : Clauses) {
7753     if (C->getClauseKind() == OMPC_hint) {
7754       if (!DirName.getName()) {
7755         Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name);
7756         ErrorFound = true;
7757       }
7758       Expr *E = cast<OMPHintClause>(C)->getHint();
7759       if (E->isTypeDependent() || E->isValueDependent() ||
7760           E->isInstantiationDependent()) {
7761         DependentHint = true;
7762       } else {
7763         Hint = E->EvaluateKnownConstInt(Context);
7764         HintLoc = C->getBeginLoc();
7765       }
7766     }
7767   }
7768   if (ErrorFound)
7769     return StmtError();
7770   const auto Pair = DSAStack->getCriticalWithHint(DirName);
7771   if (Pair.first && DirName.getName() && !DependentHint) {
7772     if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
7773       Diag(StartLoc, diag::err_omp_critical_with_hint);
7774       if (HintLoc.isValid())
7775         Diag(HintLoc, diag::note_omp_critical_hint_here)
7776             << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
7777       else
7778         Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
7779       if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
7780         Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here)
7781             << 1
7782             << C->getHint()->EvaluateKnownConstInt(Context).toString(
7783                    /*Radix=*/10, /*Signed=*/false);
7784       } else {
7785         Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1;
7786       }
7787     }
7788   }
7789 
7790   setFunctionHasBranchProtectedScope();
7791 
7792   auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
7793                                            Clauses, AStmt);
7794   if (!Pair.first && DirName.getName() && !DependentHint)
7795     DSAStack->addCriticalWithHint(Dir, Hint);
7796   return Dir;
7797 }
7798 
7799 StmtResult Sema::ActOnOpenMPParallelForDirective(
7800     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7801     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
7802   if (!AStmt)
7803     return StmtError();
7804 
7805   auto *CS = cast<CapturedStmt>(AStmt);
7806   // 1.2.2 OpenMP Language Terminology
7807   // Structured block - An executable statement with a single entry at the
7808   // top and a single exit at the bottom.
7809   // The point of exit cannot be a branch out of the structured block.
7810   // longjmp() and throw() must not violate the entry/exit criteria.
7811   CS->getCapturedDecl()->setNothrow();
7812 
7813   OMPLoopDirective::HelperExprs B;
7814   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7815   // define the nested loops number.
7816   unsigned NestedLoopCount =
7817       checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
7818                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7819                       VarsWithImplicitDSA, B);
7820   if (NestedLoopCount == 0)
7821     return StmtError();
7822 
7823   assert((CurContext->isDependentContext() || B.builtAll()) &&
7824          "omp parallel for loop exprs were not built");
7825 
7826   if (!CurContext->isDependentContext()) {
7827     // Finalize the clauses that need pre-built expressions for CodeGen.
7828     for (OMPClause *C : Clauses) {
7829       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7830         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7831                                      B.NumIterations, *this, CurScope,
7832                                      DSAStack))
7833           return StmtError();
7834     }
7835   }
7836 
7837   setFunctionHasBranchProtectedScope();
7838   return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
7839                                          NestedLoopCount, Clauses, AStmt, B,
7840                                          DSAStack->isCancelRegion());
7841 }
7842 
7843 StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
7844     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7845     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
7846   if (!AStmt)
7847     return StmtError();
7848 
7849   auto *CS = cast<CapturedStmt>(AStmt);
7850   // 1.2.2 OpenMP Language Terminology
7851   // Structured block - An executable statement with a single entry at the
7852   // top and a single exit at the bottom.
7853   // The point of exit cannot be a branch out of the structured block.
7854   // longjmp() and throw() must not violate the entry/exit criteria.
7855   CS->getCapturedDecl()->setNothrow();
7856 
7857   OMPLoopDirective::HelperExprs B;
7858   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7859   // define the nested loops number.
7860   unsigned NestedLoopCount =
7861       checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
7862                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7863                       VarsWithImplicitDSA, B);
7864   if (NestedLoopCount == 0)
7865     return StmtError();
7866 
7867   if (!CurContext->isDependentContext()) {
7868     // Finalize the clauses that need pre-built expressions for CodeGen.
7869     for (OMPClause *C : Clauses) {
7870       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7871         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7872                                      B.NumIterations, *this, CurScope,
7873                                      DSAStack))
7874           return StmtError();
7875     }
7876   }
7877 
7878   if (checkSimdlenSafelenSpecified(*this, Clauses))
7879     return StmtError();
7880 
7881   setFunctionHasBranchProtectedScope();
7882   return OMPParallelForSimdDirective::Create(
7883       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7884 }
7885 
7886 StmtResult
7887 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
7888                                            Stmt *AStmt, SourceLocation StartLoc,
7889                                            SourceLocation EndLoc) {
7890   if (!AStmt)
7891     return StmtError();
7892 
7893   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7894   auto BaseStmt = AStmt;
7895   while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
7896     BaseStmt = CS->getCapturedStmt();
7897   if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
7898     auto S = C->children();
7899     if (S.begin() == S.end())
7900       return StmtError();
7901     // All associated statements must be '#pragma omp section' except for
7902     // the first one.
7903     for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
7904       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
7905         if (SectionStmt)
7906           Diag(SectionStmt->getBeginLoc(),
7907                diag::err_omp_parallel_sections_substmt_not_section);
7908         return StmtError();
7909       }
7910       cast<OMPSectionDirective>(SectionStmt)
7911           ->setHasCancel(DSAStack->isCancelRegion());
7912     }
7913   } else {
7914     Diag(AStmt->getBeginLoc(),
7915          diag::err_omp_parallel_sections_not_compound_stmt);
7916     return StmtError();
7917   }
7918 
7919   setFunctionHasBranchProtectedScope();
7920 
7921   return OMPParallelSectionsDirective::Create(
7922       Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
7923 }
7924 
7925 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
7926                                           Stmt *AStmt, SourceLocation StartLoc,
7927                                           SourceLocation EndLoc) {
7928   if (!AStmt)
7929     return StmtError();
7930 
7931   auto *CS = cast<CapturedStmt>(AStmt);
7932   // 1.2.2 OpenMP Language Terminology
7933   // Structured block - An executable statement with a single entry at the
7934   // top and a single exit at the bottom.
7935   // The point of exit cannot be a branch out of the structured block.
7936   // longjmp() and throw() must not violate the entry/exit criteria.
7937   CS->getCapturedDecl()->setNothrow();
7938 
7939   setFunctionHasBranchProtectedScope();
7940 
7941   return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
7942                                   DSAStack->isCancelRegion());
7943 }
7944 
7945 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
7946                                                SourceLocation EndLoc) {
7947   return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
7948 }
7949 
7950 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
7951                                              SourceLocation EndLoc) {
7952   return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
7953 }
7954 
7955 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
7956                                               SourceLocation EndLoc) {
7957   return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
7958 }
7959 
7960 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
7961                                                Stmt *AStmt,
7962                                                SourceLocation StartLoc,
7963                                                SourceLocation EndLoc) {
7964   if (!AStmt)
7965     return StmtError();
7966 
7967   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7968 
7969   setFunctionHasBranchProtectedScope();
7970 
7971   return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
7972                                        AStmt,
7973                                        DSAStack->getTaskgroupReductionRef());
7974 }
7975 
7976 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
7977                                            SourceLocation StartLoc,
7978                                            SourceLocation EndLoc) {
7979   assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
7980   return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
7981 }
7982 
7983 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
7984                                              Stmt *AStmt,
7985                                              SourceLocation StartLoc,
7986                                              SourceLocation EndLoc) {
7987   const OMPClause *DependFound = nullptr;
7988   const OMPClause *DependSourceClause = nullptr;
7989   const OMPClause *DependSinkClause = nullptr;
7990   bool ErrorFound = false;
7991   const OMPThreadsClause *TC = nullptr;
7992   const OMPSIMDClause *SC = nullptr;
7993   for (const OMPClause *C : Clauses) {
7994     if (auto *DC = dyn_cast<OMPDependClause>(C)) {
7995       DependFound = C;
7996       if (DC->getDependencyKind() == OMPC_DEPEND_source) {
7997         if (DependSourceClause) {
7998           Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
7999               << getOpenMPDirectiveName(OMPD_ordered)
8000               << getOpenMPClauseName(OMPC_depend) << 2;
8001           ErrorFound = true;
8002         } else {
8003           DependSourceClause = C;
8004         }
8005         if (DependSinkClause) {
8006           Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
8007               << 0;
8008           ErrorFound = true;
8009         }
8010       } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
8011         if (DependSourceClause) {
8012           Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
8013               << 1;
8014           ErrorFound = true;
8015         }
8016         DependSinkClause = C;
8017       }
8018     } else if (C->getClauseKind() == OMPC_threads) {
8019       TC = cast<OMPThreadsClause>(C);
8020     } else if (C->getClauseKind() == OMPC_simd) {
8021       SC = cast<OMPSIMDClause>(C);
8022     }
8023   }
8024   if (!ErrorFound && !SC &&
8025       isOpenMPSimdDirective(DSAStack->getParentDirective())) {
8026     // OpenMP [2.8.1,simd Construct, Restrictions]
8027     // An ordered construct with the simd clause is the only OpenMP construct
8028     // that can appear in the simd region.
8029     Diag(StartLoc, diag::err_omp_prohibited_region_simd);
8030     ErrorFound = true;
8031   } else if (DependFound && (TC || SC)) {
8032     Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd)
8033         << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
8034     ErrorFound = true;
8035   } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) {
8036     Diag(DependFound->getBeginLoc(),
8037          diag::err_omp_ordered_directive_without_param);
8038     ErrorFound = true;
8039   } else if (TC || Clauses.empty()) {
8040     if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
8041       SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
8042       Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
8043           << (TC != nullptr);
8044       Diag(Param->getBeginLoc(), diag::note_omp_ordered_param);
8045       ErrorFound = true;
8046     }
8047   }
8048   if ((!AStmt && !DependFound) || ErrorFound)
8049     return StmtError();
8050 
8051   if (AStmt) {
8052     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8053 
8054     setFunctionHasBranchProtectedScope();
8055   }
8056 
8057   return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
8058 }
8059 
8060 namespace {
8061 /// Helper class for checking expression in 'omp atomic [update]'
8062 /// construct.
8063 class OpenMPAtomicUpdateChecker {
8064   /// Error results for atomic update expressions.
8065   enum ExprAnalysisErrorCode {
8066     /// A statement is not an expression statement.
8067     NotAnExpression,
8068     /// Expression is not builtin binary or unary operation.
8069     NotABinaryOrUnaryExpression,
8070     /// Unary operation is not post-/pre- increment/decrement operation.
8071     NotAnUnaryIncDecExpression,
8072     /// An expression is not of scalar type.
8073     NotAScalarType,
8074     /// A binary operation is not an assignment operation.
8075     NotAnAssignmentOp,
8076     /// RHS part of the binary operation is not a binary expression.
8077     NotABinaryExpression,
8078     /// RHS part is not additive/multiplicative/shift/biwise binary
8079     /// expression.
8080     NotABinaryOperator,
8081     /// RHS binary operation does not have reference to the updated LHS
8082     /// part.
8083     NotAnUpdateExpression,
8084     /// No errors is found.
8085     NoError
8086   };
8087   /// Reference to Sema.
8088   Sema &SemaRef;
8089   /// A location for note diagnostics (when error is found).
8090   SourceLocation NoteLoc;
8091   /// 'x' lvalue part of the source atomic expression.
8092   Expr *X;
8093   /// 'expr' rvalue part of the source atomic expression.
8094   Expr *E;
8095   /// Helper expression of the form
8096   /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
8097   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
8098   Expr *UpdateExpr;
8099   /// Is 'x' a LHS in a RHS part of full update expression. It is
8100   /// important for non-associative operations.
8101   bool IsXLHSInRHSPart;
8102   BinaryOperatorKind Op;
8103   SourceLocation OpLoc;
8104   /// true if the source expression is a postfix unary operation, false
8105   /// if it is a prefix unary operation.
8106   bool IsPostfixUpdate;
8107 
8108 public:
8109   OpenMPAtomicUpdateChecker(Sema &SemaRef)
8110       : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
8111         IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
8112   /// Check specified statement that it is suitable for 'atomic update'
8113   /// constructs and extract 'x', 'expr' and Operation from the original
8114   /// expression. If DiagId and NoteId == 0, then only check is performed
8115   /// without error notification.
8116   /// \param DiagId Diagnostic which should be emitted if error is found.
8117   /// \param NoteId Diagnostic note for the main error message.
8118   /// \return true if statement is not an update expression, false otherwise.
8119   bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
8120   /// Return the 'x' lvalue part of the source atomic expression.
8121   Expr *getX() const { return X; }
8122   /// Return the 'expr' rvalue part of the source atomic expression.
8123   Expr *getExpr() const { return E; }
8124   /// Return the update expression used in calculation of the updated
8125   /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
8126   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
8127   Expr *getUpdateExpr() const { return UpdateExpr; }
8128   /// Return true if 'x' is LHS in RHS part of full update expression,
8129   /// false otherwise.
8130   bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
8131 
8132   /// true if the source expression is a postfix unary operation, false
8133   /// if it is a prefix unary operation.
8134   bool isPostfixUpdate() const { return IsPostfixUpdate; }
8135 
8136 private:
8137   bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
8138                             unsigned NoteId = 0);
8139 };
8140 } // namespace
8141 
8142 bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
8143     BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
8144   ExprAnalysisErrorCode ErrorFound = NoError;
8145   SourceLocation ErrorLoc, NoteLoc;
8146   SourceRange ErrorRange, NoteRange;
8147   // Allowed constructs are:
8148   //  x = x binop expr;
8149   //  x = expr binop x;
8150   if (AtomicBinOp->getOpcode() == BO_Assign) {
8151     X = AtomicBinOp->getLHS();
8152     if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
8153             AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
8154       if (AtomicInnerBinOp->isMultiplicativeOp() ||
8155           AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
8156           AtomicInnerBinOp->isBitwiseOp()) {
8157         Op = AtomicInnerBinOp->getOpcode();
8158         OpLoc = AtomicInnerBinOp->getOperatorLoc();
8159         Expr *LHS = AtomicInnerBinOp->getLHS();
8160         Expr *RHS = AtomicInnerBinOp->getRHS();
8161         llvm::FoldingSetNodeID XId, LHSId, RHSId;
8162         X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
8163                                           /*Canonical=*/true);
8164         LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
8165                                             /*Canonical=*/true);
8166         RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
8167                                             /*Canonical=*/true);
8168         if (XId == LHSId) {
8169           E = RHS;
8170           IsXLHSInRHSPart = true;
8171         } else if (XId == RHSId) {
8172           E = LHS;
8173           IsXLHSInRHSPart = false;
8174         } else {
8175           ErrorLoc = AtomicInnerBinOp->getExprLoc();
8176           ErrorRange = AtomicInnerBinOp->getSourceRange();
8177           NoteLoc = X->getExprLoc();
8178           NoteRange = X->getSourceRange();
8179           ErrorFound = NotAnUpdateExpression;
8180         }
8181       } else {
8182         ErrorLoc = AtomicInnerBinOp->getExprLoc();
8183         ErrorRange = AtomicInnerBinOp->getSourceRange();
8184         NoteLoc = AtomicInnerBinOp->getOperatorLoc();
8185         NoteRange = SourceRange(NoteLoc, NoteLoc);
8186         ErrorFound = NotABinaryOperator;
8187       }
8188     } else {
8189       NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
8190       NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
8191       ErrorFound = NotABinaryExpression;
8192     }
8193   } else {
8194     ErrorLoc = AtomicBinOp->getExprLoc();
8195     ErrorRange = AtomicBinOp->getSourceRange();
8196     NoteLoc = AtomicBinOp->getOperatorLoc();
8197     NoteRange = SourceRange(NoteLoc, NoteLoc);
8198     ErrorFound = NotAnAssignmentOp;
8199   }
8200   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
8201     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
8202     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
8203     return true;
8204   }
8205   if (SemaRef.CurContext->isDependentContext())
8206     E = X = UpdateExpr = nullptr;
8207   return ErrorFound != NoError;
8208 }
8209 
8210 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
8211                                                unsigned NoteId) {
8212   ExprAnalysisErrorCode ErrorFound = NoError;
8213   SourceLocation ErrorLoc, NoteLoc;
8214   SourceRange ErrorRange, NoteRange;
8215   // Allowed constructs are:
8216   //  x++;
8217   //  x--;
8218   //  ++x;
8219   //  --x;
8220   //  x binop= expr;
8221   //  x = x binop expr;
8222   //  x = expr binop x;
8223   if (auto *AtomicBody = dyn_cast<Expr>(S)) {
8224     AtomicBody = AtomicBody->IgnoreParenImpCasts();
8225     if (AtomicBody->getType()->isScalarType() ||
8226         AtomicBody->isInstantiationDependent()) {
8227       if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
8228               AtomicBody->IgnoreParenImpCasts())) {
8229         // Check for Compound Assignment Operation
8230         Op = BinaryOperator::getOpForCompoundAssignment(
8231             AtomicCompAssignOp->getOpcode());
8232         OpLoc = AtomicCompAssignOp->getOperatorLoc();
8233         E = AtomicCompAssignOp->getRHS();
8234         X = AtomicCompAssignOp->getLHS()->IgnoreParens();
8235         IsXLHSInRHSPart = true;
8236       } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
8237                      AtomicBody->IgnoreParenImpCasts())) {
8238         // Check for Binary Operation
8239         if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
8240           return true;
8241       } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
8242                      AtomicBody->IgnoreParenImpCasts())) {
8243         // Check for Unary Operation
8244         if (AtomicUnaryOp->isIncrementDecrementOp()) {
8245           IsPostfixUpdate = AtomicUnaryOp->isPostfix();
8246           Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
8247           OpLoc = AtomicUnaryOp->getOperatorLoc();
8248           X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
8249           E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
8250           IsXLHSInRHSPart = true;
8251         } else {
8252           ErrorFound = NotAnUnaryIncDecExpression;
8253           ErrorLoc = AtomicUnaryOp->getExprLoc();
8254           ErrorRange = AtomicUnaryOp->getSourceRange();
8255           NoteLoc = AtomicUnaryOp->getOperatorLoc();
8256           NoteRange = SourceRange(NoteLoc, NoteLoc);
8257         }
8258       } else if (!AtomicBody->isInstantiationDependent()) {
8259         ErrorFound = NotABinaryOrUnaryExpression;
8260         NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
8261         NoteRange = ErrorRange = AtomicBody->getSourceRange();
8262       }
8263     } else {
8264       ErrorFound = NotAScalarType;
8265       NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
8266       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
8267     }
8268   } else {
8269     ErrorFound = NotAnExpression;
8270     NoteLoc = ErrorLoc = S->getBeginLoc();
8271     NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
8272   }
8273   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
8274     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
8275     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
8276     return true;
8277   }
8278   if (SemaRef.CurContext->isDependentContext())
8279     E = X = UpdateExpr = nullptr;
8280   if (ErrorFound == NoError && E && X) {
8281     // Build an update expression of form 'OpaqueValueExpr(x) binop
8282     // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
8283     // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
8284     auto *OVEX = new (SemaRef.getASTContext())
8285         OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
8286     auto *OVEExpr = new (SemaRef.getASTContext())
8287         OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
8288     ExprResult Update =
8289         SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
8290                                    IsXLHSInRHSPart ? OVEExpr : OVEX);
8291     if (Update.isInvalid())
8292       return true;
8293     Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
8294                                                Sema::AA_Casting);
8295     if (Update.isInvalid())
8296       return true;
8297     UpdateExpr = Update.get();
8298   }
8299   return ErrorFound != NoError;
8300 }
8301 
8302 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
8303                                             Stmt *AStmt,
8304                                             SourceLocation StartLoc,
8305                                             SourceLocation EndLoc) {
8306   if (!AStmt)
8307     return StmtError();
8308 
8309   auto *CS = cast<CapturedStmt>(AStmt);
8310   // 1.2.2 OpenMP Language Terminology
8311   // Structured block - An executable statement with a single entry at the
8312   // top and a single exit at the bottom.
8313   // The point of exit cannot be a branch out of the structured block.
8314   // longjmp() and throw() must not violate the entry/exit criteria.
8315   OpenMPClauseKind AtomicKind = OMPC_unknown;
8316   SourceLocation AtomicKindLoc;
8317   for (const OMPClause *C : Clauses) {
8318     if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
8319         C->getClauseKind() == OMPC_update ||
8320         C->getClauseKind() == OMPC_capture) {
8321       if (AtomicKind != OMPC_unknown) {
8322         Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses)
8323             << SourceRange(C->getBeginLoc(), C->getEndLoc());
8324         Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
8325             << getOpenMPClauseName(AtomicKind);
8326       } else {
8327         AtomicKind = C->getClauseKind();
8328         AtomicKindLoc = C->getBeginLoc();
8329       }
8330     }
8331   }
8332 
8333   Stmt *Body = CS->getCapturedStmt();
8334   if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
8335     Body = EWC->getSubExpr();
8336 
8337   Expr *X = nullptr;
8338   Expr *V = nullptr;
8339   Expr *E = nullptr;
8340   Expr *UE = nullptr;
8341   bool IsXLHSInRHSPart = false;
8342   bool IsPostfixUpdate = false;
8343   // OpenMP [2.12.6, atomic Construct]
8344   // In the next expressions:
8345   // * x and v (as applicable) are both l-value expressions with scalar type.
8346   // * During the execution of an atomic region, multiple syntactic
8347   // occurrences of x must designate the same storage location.
8348   // * Neither of v and expr (as applicable) may access the storage location
8349   // designated by x.
8350   // * Neither of x and expr (as applicable) may access the storage location
8351   // designated by v.
8352   // * expr is an expression with scalar type.
8353   // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
8354   // * binop, binop=, ++, and -- are not overloaded operators.
8355   // * The expression x binop expr must be numerically equivalent to x binop
8356   // (expr). This requirement is satisfied if the operators in expr have
8357   // precedence greater than binop, or by using parentheses around expr or
8358   // subexpressions of expr.
8359   // * The expression expr binop x must be numerically equivalent to (expr)
8360   // binop x. This requirement is satisfied if the operators in expr have
8361   // precedence equal to or greater than binop, or by using parentheses around
8362   // expr or subexpressions of expr.
8363   // * For forms that allow multiple occurrences of x, the number of times
8364   // that x is evaluated is unspecified.
8365   if (AtomicKind == OMPC_read) {
8366     enum {
8367       NotAnExpression,
8368       NotAnAssignmentOp,
8369       NotAScalarType,
8370       NotAnLValue,
8371       NoError
8372     } ErrorFound = NoError;
8373     SourceLocation ErrorLoc, NoteLoc;
8374     SourceRange ErrorRange, NoteRange;
8375     // If clause is read:
8376     //  v = x;
8377     if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
8378       const auto *AtomicBinOp =
8379           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
8380       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
8381         X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
8382         V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
8383         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
8384             (V->isInstantiationDependent() || V->getType()->isScalarType())) {
8385           if (!X->isLValue() || !V->isLValue()) {
8386             const Expr *NotLValueExpr = X->isLValue() ? V : X;
8387             ErrorFound = NotAnLValue;
8388             ErrorLoc = AtomicBinOp->getExprLoc();
8389             ErrorRange = AtomicBinOp->getSourceRange();
8390             NoteLoc = NotLValueExpr->getExprLoc();
8391             NoteRange = NotLValueExpr->getSourceRange();
8392           }
8393         } else if (!X->isInstantiationDependent() ||
8394                    !V->isInstantiationDependent()) {
8395           const Expr *NotScalarExpr =
8396               (X->isInstantiationDependent() || X->getType()->isScalarType())
8397                   ? V
8398                   : X;
8399           ErrorFound = NotAScalarType;
8400           ErrorLoc = AtomicBinOp->getExprLoc();
8401           ErrorRange = AtomicBinOp->getSourceRange();
8402           NoteLoc = NotScalarExpr->getExprLoc();
8403           NoteRange = NotScalarExpr->getSourceRange();
8404         }
8405       } else if (!AtomicBody->isInstantiationDependent()) {
8406         ErrorFound = NotAnAssignmentOp;
8407         ErrorLoc = AtomicBody->getExprLoc();
8408         ErrorRange = AtomicBody->getSourceRange();
8409         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
8410                               : AtomicBody->getExprLoc();
8411         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
8412                                 : AtomicBody->getSourceRange();
8413       }
8414     } else {
8415       ErrorFound = NotAnExpression;
8416       NoteLoc = ErrorLoc = Body->getBeginLoc();
8417       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
8418     }
8419     if (ErrorFound != NoError) {
8420       Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
8421           << ErrorRange;
8422       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
8423                                                       << NoteRange;
8424       return StmtError();
8425     }
8426     if (CurContext->isDependentContext())
8427       V = X = nullptr;
8428   } else if (AtomicKind == OMPC_write) {
8429     enum {
8430       NotAnExpression,
8431       NotAnAssignmentOp,
8432       NotAScalarType,
8433       NotAnLValue,
8434       NoError
8435     } ErrorFound = NoError;
8436     SourceLocation ErrorLoc, NoteLoc;
8437     SourceRange ErrorRange, NoteRange;
8438     // If clause is write:
8439     //  x = expr;
8440     if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
8441       const auto *AtomicBinOp =
8442           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
8443       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
8444         X = AtomicBinOp->getLHS();
8445         E = AtomicBinOp->getRHS();
8446         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
8447             (E->isInstantiationDependent() || E->getType()->isScalarType())) {
8448           if (!X->isLValue()) {
8449             ErrorFound = NotAnLValue;
8450             ErrorLoc = AtomicBinOp->getExprLoc();
8451             ErrorRange = AtomicBinOp->getSourceRange();
8452             NoteLoc = X->getExprLoc();
8453             NoteRange = X->getSourceRange();
8454           }
8455         } else if (!X->isInstantiationDependent() ||
8456                    !E->isInstantiationDependent()) {
8457           const Expr *NotScalarExpr =
8458               (X->isInstantiationDependent() || X->getType()->isScalarType())
8459                   ? E
8460                   : X;
8461           ErrorFound = NotAScalarType;
8462           ErrorLoc = AtomicBinOp->getExprLoc();
8463           ErrorRange = AtomicBinOp->getSourceRange();
8464           NoteLoc = NotScalarExpr->getExprLoc();
8465           NoteRange = NotScalarExpr->getSourceRange();
8466         }
8467       } else if (!AtomicBody->isInstantiationDependent()) {
8468         ErrorFound = NotAnAssignmentOp;
8469         ErrorLoc = AtomicBody->getExprLoc();
8470         ErrorRange = AtomicBody->getSourceRange();
8471         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
8472                               : AtomicBody->getExprLoc();
8473         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
8474                                 : AtomicBody->getSourceRange();
8475       }
8476     } else {
8477       ErrorFound = NotAnExpression;
8478       NoteLoc = ErrorLoc = Body->getBeginLoc();
8479       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
8480     }
8481     if (ErrorFound != NoError) {
8482       Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
8483           << ErrorRange;
8484       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
8485                                                       << NoteRange;
8486       return StmtError();
8487     }
8488     if (CurContext->isDependentContext())
8489       E = X = nullptr;
8490   } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
8491     // If clause is update:
8492     //  x++;
8493     //  x--;
8494     //  ++x;
8495     //  --x;
8496     //  x binop= expr;
8497     //  x = x binop expr;
8498     //  x = expr binop x;
8499     OpenMPAtomicUpdateChecker Checker(*this);
8500     if (Checker.checkStatement(
8501             Body, (AtomicKind == OMPC_update)
8502                       ? diag::err_omp_atomic_update_not_expression_statement
8503                       : diag::err_omp_atomic_not_expression_statement,
8504             diag::note_omp_atomic_update))
8505       return StmtError();
8506     if (!CurContext->isDependentContext()) {
8507       E = Checker.getExpr();
8508       X = Checker.getX();
8509       UE = Checker.getUpdateExpr();
8510       IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
8511     }
8512   } else if (AtomicKind == OMPC_capture) {
8513     enum {
8514       NotAnAssignmentOp,
8515       NotACompoundStatement,
8516       NotTwoSubstatements,
8517       NotASpecificExpression,
8518       NoError
8519     } ErrorFound = NoError;
8520     SourceLocation ErrorLoc, NoteLoc;
8521     SourceRange ErrorRange, NoteRange;
8522     if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
8523       // If clause is a capture:
8524       //  v = x++;
8525       //  v = x--;
8526       //  v = ++x;
8527       //  v = --x;
8528       //  v = x binop= expr;
8529       //  v = x = x binop expr;
8530       //  v = x = expr binop x;
8531       const auto *AtomicBinOp =
8532           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
8533       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
8534         V = AtomicBinOp->getLHS();
8535         Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
8536         OpenMPAtomicUpdateChecker Checker(*this);
8537         if (Checker.checkStatement(
8538                 Body, diag::err_omp_atomic_capture_not_expression_statement,
8539                 diag::note_omp_atomic_update))
8540           return StmtError();
8541         E = Checker.getExpr();
8542         X = Checker.getX();
8543         UE = Checker.getUpdateExpr();
8544         IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
8545         IsPostfixUpdate = Checker.isPostfixUpdate();
8546       } else if (!AtomicBody->isInstantiationDependent()) {
8547         ErrorLoc = AtomicBody->getExprLoc();
8548         ErrorRange = AtomicBody->getSourceRange();
8549         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
8550                               : AtomicBody->getExprLoc();
8551         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
8552                                 : AtomicBody->getSourceRange();
8553         ErrorFound = NotAnAssignmentOp;
8554       }
8555       if (ErrorFound != NoError) {
8556         Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
8557             << ErrorRange;
8558         Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
8559         return StmtError();
8560       }
8561       if (CurContext->isDependentContext())
8562         UE = V = E = X = nullptr;
8563     } else {
8564       // If clause is a capture:
8565       //  { v = x; x = expr; }
8566       //  { v = x; x++; }
8567       //  { v = x; x--; }
8568       //  { v = x; ++x; }
8569       //  { v = x; --x; }
8570       //  { v = x; x binop= expr; }
8571       //  { v = x; x = x binop expr; }
8572       //  { v = x; x = expr binop x; }
8573       //  { x++; v = x; }
8574       //  { x--; v = x; }
8575       //  { ++x; v = x; }
8576       //  { --x; v = x; }
8577       //  { x binop= expr; v = x; }
8578       //  { x = x binop expr; v = x; }
8579       //  { x = expr binop x; v = x; }
8580       if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
8581         // Check that this is { expr1; expr2; }
8582         if (CS->size() == 2) {
8583           Stmt *First = CS->body_front();
8584           Stmt *Second = CS->body_back();
8585           if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
8586             First = EWC->getSubExpr()->IgnoreParenImpCasts();
8587           if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
8588             Second = EWC->getSubExpr()->IgnoreParenImpCasts();
8589           // Need to find what subexpression is 'v' and what is 'x'.
8590           OpenMPAtomicUpdateChecker Checker(*this);
8591           bool IsUpdateExprFound = !Checker.checkStatement(Second);
8592           BinaryOperator *BinOp = nullptr;
8593           if (IsUpdateExprFound) {
8594             BinOp = dyn_cast<BinaryOperator>(First);
8595             IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
8596           }
8597           if (IsUpdateExprFound && !CurContext->isDependentContext()) {
8598             //  { v = x; x++; }
8599             //  { v = x; x--; }
8600             //  { v = x; ++x; }
8601             //  { v = x; --x; }
8602             //  { v = x; x binop= expr; }
8603             //  { v = x; x = x binop expr; }
8604             //  { v = x; x = expr binop x; }
8605             // Check that the first expression has form v = x.
8606             Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
8607             llvm::FoldingSetNodeID XId, PossibleXId;
8608             Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
8609             PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
8610             IsUpdateExprFound = XId == PossibleXId;
8611             if (IsUpdateExprFound) {
8612               V = BinOp->getLHS();
8613               X = Checker.getX();
8614               E = Checker.getExpr();
8615               UE = Checker.getUpdateExpr();
8616               IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
8617               IsPostfixUpdate = true;
8618             }
8619           }
8620           if (!IsUpdateExprFound) {
8621             IsUpdateExprFound = !Checker.checkStatement(First);
8622             BinOp = nullptr;
8623             if (IsUpdateExprFound) {
8624               BinOp = dyn_cast<BinaryOperator>(Second);
8625               IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
8626             }
8627             if (IsUpdateExprFound && !CurContext->isDependentContext()) {
8628               //  { x++; v = x; }
8629               //  { x--; v = x; }
8630               //  { ++x; v = x; }
8631               //  { --x; v = x; }
8632               //  { x binop= expr; v = x; }
8633               //  { x = x binop expr; v = x; }
8634               //  { x = expr binop x; v = x; }
8635               // Check that the second expression has form v = x.
8636               Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
8637               llvm::FoldingSetNodeID XId, PossibleXId;
8638               Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
8639               PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
8640               IsUpdateExprFound = XId == PossibleXId;
8641               if (IsUpdateExprFound) {
8642                 V = BinOp->getLHS();
8643                 X = Checker.getX();
8644                 E = Checker.getExpr();
8645                 UE = Checker.getUpdateExpr();
8646                 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
8647                 IsPostfixUpdate = false;
8648               }
8649             }
8650           }
8651           if (!IsUpdateExprFound) {
8652             //  { v = x; x = expr; }
8653             auto *FirstExpr = dyn_cast<Expr>(First);
8654             auto *SecondExpr = dyn_cast<Expr>(Second);
8655             if (!FirstExpr || !SecondExpr ||
8656                 !(FirstExpr->isInstantiationDependent() ||
8657                   SecondExpr->isInstantiationDependent())) {
8658               auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
8659               if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
8660                 ErrorFound = NotAnAssignmentOp;
8661                 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
8662                                                 : First->getBeginLoc();
8663                 NoteRange = ErrorRange = FirstBinOp
8664                                              ? FirstBinOp->getSourceRange()
8665                                              : SourceRange(ErrorLoc, ErrorLoc);
8666               } else {
8667                 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
8668                 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
8669                   ErrorFound = NotAnAssignmentOp;
8670                   NoteLoc = ErrorLoc = SecondBinOp
8671                                            ? SecondBinOp->getOperatorLoc()
8672                                            : Second->getBeginLoc();
8673                   NoteRange = ErrorRange =
8674                       SecondBinOp ? SecondBinOp->getSourceRange()
8675                                   : SourceRange(ErrorLoc, ErrorLoc);
8676                 } else {
8677                   Expr *PossibleXRHSInFirst =
8678                       FirstBinOp->getRHS()->IgnoreParenImpCasts();
8679                   Expr *PossibleXLHSInSecond =
8680                       SecondBinOp->getLHS()->IgnoreParenImpCasts();
8681                   llvm::FoldingSetNodeID X1Id, X2Id;
8682                   PossibleXRHSInFirst->Profile(X1Id, Context,
8683                                                /*Canonical=*/true);
8684                   PossibleXLHSInSecond->Profile(X2Id, Context,
8685                                                 /*Canonical=*/true);
8686                   IsUpdateExprFound = X1Id == X2Id;
8687                   if (IsUpdateExprFound) {
8688                     V = FirstBinOp->getLHS();
8689                     X = SecondBinOp->getLHS();
8690                     E = SecondBinOp->getRHS();
8691                     UE = nullptr;
8692                     IsXLHSInRHSPart = false;
8693                     IsPostfixUpdate = true;
8694                   } else {
8695                     ErrorFound = NotASpecificExpression;
8696                     ErrorLoc = FirstBinOp->getExprLoc();
8697                     ErrorRange = FirstBinOp->getSourceRange();
8698                     NoteLoc = SecondBinOp->getLHS()->getExprLoc();
8699                     NoteRange = SecondBinOp->getRHS()->getSourceRange();
8700                   }
8701                 }
8702               }
8703             }
8704           }
8705         } else {
8706           NoteLoc = ErrorLoc = Body->getBeginLoc();
8707           NoteRange = ErrorRange =
8708               SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
8709           ErrorFound = NotTwoSubstatements;
8710         }
8711       } else {
8712         NoteLoc = ErrorLoc = Body->getBeginLoc();
8713         NoteRange = ErrorRange =
8714             SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
8715         ErrorFound = NotACompoundStatement;
8716       }
8717       if (ErrorFound != NoError) {
8718         Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
8719             << ErrorRange;
8720         Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
8721         return StmtError();
8722       }
8723       if (CurContext->isDependentContext())
8724         UE = V = E = X = nullptr;
8725     }
8726   }
8727 
8728   setFunctionHasBranchProtectedScope();
8729 
8730   return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
8731                                     X, V, E, UE, IsXLHSInRHSPart,
8732                                     IsPostfixUpdate);
8733 }
8734 
8735 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
8736                                             Stmt *AStmt,
8737                                             SourceLocation StartLoc,
8738                                             SourceLocation EndLoc) {
8739   if (!AStmt)
8740     return StmtError();
8741 
8742   auto *CS = cast<CapturedStmt>(AStmt);
8743   // 1.2.2 OpenMP Language Terminology
8744   // Structured block - An executable statement with a single entry at the
8745   // top and a single exit at the bottom.
8746   // The point of exit cannot be a branch out of the structured block.
8747   // longjmp() and throw() must not violate the entry/exit criteria.
8748   CS->getCapturedDecl()->setNothrow();
8749   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target);
8750        ThisCaptureLevel > 1; --ThisCaptureLevel) {
8751     CS = cast<CapturedStmt>(CS->getCapturedStmt());
8752     // 1.2.2 OpenMP Language Terminology
8753     // Structured block - An executable statement with a single entry at the
8754     // top and a single exit at the bottom.
8755     // The point of exit cannot be a branch out of the structured block.
8756     // longjmp() and throw() must not violate the entry/exit criteria.
8757     CS->getCapturedDecl()->setNothrow();
8758   }
8759 
8760   // OpenMP [2.16, Nesting of Regions]
8761   // If specified, a teams construct must be contained within a target
8762   // construct. That target construct must contain no statements or directives
8763   // outside of the teams construct.
8764   if (DSAStack->hasInnerTeamsRegion()) {
8765     const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
8766     bool OMPTeamsFound = true;
8767     if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
8768       auto I = CS->body_begin();
8769       while (I != CS->body_end()) {
8770         const auto *OED = dyn_cast<OMPExecutableDirective>(*I);
8771         if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) ||
8772             OMPTeamsFound) {
8773 
8774           OMPTeamsFound = false;
8775           break;
8776         }
8777         ++I;
8778       }
8779       assert(I != CS->body_end() && "Not found statement");
8780       S = *I;
8781     } else {
8782       const auto *OED = dyn_cast<OMPExecutableDirective>(S);
8783       OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
8784     }
8785     if (!OMPTeamsFound) {
8786       Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
8787       Diag(DSAStack->getInnerTeamsRegionLoc(),
8788            diag::note_omp_nested_teams_construct_here);
8789       Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here)
8790           << isa<OMPExecutableDirective>(S);
8791       return StmtError();
8792     }
8793   }
8794 
8795   setFunctionHasBranchProtectedScope();
8796 
8797   return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
8798 }
8799 
8800 StmtResult
8801 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
8802                                          Stmt *AStmt, SourceLocation StartLoc,
8803                                          SourceLocation EndLoc) {
8804   if (!AStmt)
8805     return StmtError();
8806 
8807   auto *CS = cast<CapturedStmt>(AStmt);
8808   // 1.2.2 OpenMP Language Terminology
8809   // Structured block - An executable statement with a single entry at the
8810   // top and a single exit at the bottom.
8811   // The point of exit cannot be a branch out of the structured block.
8812   // longjmp() and throw() must not violate the entry/exit criteria.
8813   CS->getCapturedDecl()->setNothrow();
8814   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel);
8815        ThisCaptureLevel > 1; --ThisCaptureLevel) {
8816     CS = cast<CapturedStmt>(CS->getCapturedStmt());
8817     // 1.2.2 OpenMP Language Terminology
8818     // Structured block - An executable statement with a single entry at the
8819     // top and a single exit at the bottom.
8820     // The point of exit cannot be a branch out of the structured block.
8821     // longjmp() and throw() must not violate the entry/exit criteria.
8822     CS->getCapturedDecl()->setNothrow();
8823   }
8824 
8825   setFunctionHasBranchProtectedScope();
8826 
8827   return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
8828                                             AStmt);
8829 }
8830 
8831 StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
8832     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
8833     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
8834   if (!AStmt)
8835     return StmtError();
8836 
8837   auto *CS = cast<CapturedStmt>(AStmt);
8838   // 1.2.2 OpenMP Language Terminology
8839   // Structured block - An executable statement with a single entry at the
8840   // top and a single exit at the bottom.
8841   // The point of exit cannot be a branch out of the structured block.
8842   // longjmp() and throw() must not violate the entry/exit criteria.
8843   CS->getCapturedDecl()->setNothrow();
8844   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
8845        ThisCaptureLevel > 1; --ThisCaptureLevel) {
8846     CS = cast<CapturedStmt>(CS->getCapturedStmt());
8847     // 1.2.2 OpenMP Language Terminology
8848     // Structured block - An executable statement with a single entry at the
8849     // top and a single exit at the bottom.
8850     // The point of exit cannot be a branch out of the structured block.
8851     // longjmp() and throw() must not violate the entry/exit criteria.
8852     CS->getCapturedDecl()->setNothrow();
8853   }
8854 
8855   OMPLoopDirective::HelperExprs B;
8856   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
8857   // define the nested loops number.
8858   unsigned NestedLoopCount =
8859       checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
8860                       getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
8861                       VarsWithImplicitDSA, B);
8862   if (NestedLoopCount == 0)
8863     return StmtError();
8864 
8865   assert((CurContext->isDependentContext() || B.builtAll()) &&
8866          "omp target parallel for loop exprs were not built");
8867 
8868   if (!CurContext->isDependentContext()) {
8869     // Finalize the clauses that need pre-built expressions for CodeGen.
8870     for (OMPClause *C : Clauses) {
8871       if (auto *LC = dyn_cast<OMPLinearClause>(C))
8872         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8873                                      B.NumIterations, *this, CurScope,
8874                                      DSAStack))
8875           return StmtError();
8876     }
8877   }
8878 
8879   setFunctionHasBranchProtectedScope();
8880   return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
8881                                                NestedLoopCount, Clauses, AStmt,
8882                                                B, DSAStack->isCancelRegion());
8883 }
8884 
8885 /// Check for existence of a map clause in the list of clauses.
8886 static bool hasClauses(ArrayRef<OMPClause *> Clauses,
8887                        const OpenMPClauseKind K) {
8888   return llvm::any_of(
8889       Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
8890 }
8891 
8892 template <typename... Params>
8893 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
8894                        const Params... ClauseTypes) {
8895   return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
8896 }
8897 
8898 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
8899                                                 Stmt *AStmt,
8900                                                 SourceLocation StartLoc,
8901                                                 SourceLocation EndLoc) {
8902   if (!AStmt)
8903     return StmtError();
8904 
8905   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8906 
8907   // OpenMP [2.10.1, Restrictions, p. 97]
8908   // At least one map clause must appear on the directive.
8909   if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
8910     Diag(StartLoc, diag::err_omp_no_clause_for_directive)
8911         << "'map' or 'use_device_ptr'"
8912         << getOpenMPDirectiveName(OMPD_target_data);
8913     return StmtError();
8914   }
8915 
8916   setFunctionHasBranchProtectedScope();
8917 
8918   return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
8919                                         AStmt);
8920 }
8921 
8922 StmtResult
8923 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
8924                                           SourceLocation StartLoc,
8925                                           SourceLocation EndLoc, Stmt *AStmt) {
8926   if (!AStmt)
8927     return StmtError();
8928 
8929   auto *CS = cast<CapturedStmt>(AStmt);
8930   // 1.2.2 OpenMP Language Terminology
8931   // Structured block - An executable statement with a single entry at the
8932   // top and a single exit at the bottom.
8933   // The point of exit cannot be a branch out of the structured block.
8934   // longjmp() and throw() must not violate the entry/exit criteria.
8935   CS->getCapturedDecl()->setNothrow();
8936   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
8937        ThisCaptureLevel > 1; --ThisCaptureLevel) {
8938     CS = cast<CapturedStmt>(CS->getCapturedStmt());
8939     // 1.2.2 OpenMP Language Terminology
8940     // Structured block - An executable statement with a single entry at the
8941     // top and a single exit at the bottom.
8942     // The point of exit cannot be a branch out of the structured block.
8943     // longjmp() and throw() must not violate the entry/exit criteria.
8944     CS->getCapturedDecl()->setNothrow();
8945   }
8946 
8947   // OpenMP [2.10.2, Restrictions, p. 99]
8948   // At least one map clause must appear on the directive.
8949   if (!hasClauses(Clauses, OMPC_map)) {
8950     Diag(StartLoc, diag::err_omp_no_clause_for_directive)
8951         << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
8952     return StmtError();
8953   }
8954 
8955   return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
8956                                              AStmt);
8957 }
8958 
8959 StmtResult
8960 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
8961                                          SourceLocation StartLoc,
8962                                          SourceLocation EndLoc, Stmt *AStmt) {
8963   if (!AStmt)
8964     return StmtError();
8965 
8966   auto *CS = cast<CapturedStmt>(AStmt);
8967   // 1.2.2 OpenMP Language Terminology
8968   // Structured block - An executable statement with a single entry at the
8969   // top and a single exit at the bottom.
8970   // The point of exit cannot be a branch out of the structured block.
8971   // longjmp() and throw() must not violate the entry/exit criteria.
8972   CS->getCapturedDecl()->setNothrow();
8973   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
8974        ThisCaptureLevel > 1; --ThisCaptureLevel) {
8975     CS = cast<CapturedStmt>(CS->getCapturedStmt());
8976     // 1.2.2 OpenMP Language Terminology
8977     // Structured block - An executable statement with a single entry at the
8978     // top and a single exit at the bottom.
8979     // The point of exit cannot be a branch out of the structured block.
8980     // longjmp() and throw() must not violate the entry/exit criteria.
8981     CS->getCapturedDecl()->setNothrow();
8982   }
8983 
8984   // OpenMP [2.10.3, Restrictions, p. 102]
8985   // At least one map clause must appear on the directive.
8986   if (!hasClauses(Clauses, OMPC_map)) {
8987     Diag(StartLoc, diag::err_omp_no_clause_for_directive)
8988         << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
8989     return StmtError();
8990   }
8991 
8992   return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
8993                                             AStmt);
8994 }
8995 
8996 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
8997                                                   SourceLocation StartLoc,
8998                                                   SourceLocation EndLoc,
8999                                                   Stmt *AStmt) {
9000   if (!AStmt)
9001     return StmtError();
9002 
9003   auto *CS = cast<CapturedStmt>(AStmt);
9004   // 1.2.2 OpenMP Language Terminology
9005   // Structured block - An executable statement with a single entry at the
9006   // top and a single exit at the bottom.
9007   // The point of exit cannot be a branch out of the structured block.
9008   // longjmp() and throw() must not violate the entry/exit criteria.
9009   CS->getCapturedDecl()->setNothrow();
9010   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
9011        ThisCaptureLevel > 1; --ThisCaptureLevel) {
9012     CS = cast<CapturedStmt>(CS->getCapturedStmt());
9013     // 1.2.2 OpenMP Language Terminology
9014     // Structured block - An executable statement with a single entry at the
9015     // top and a single exit at the bottom.
9016     // The point of exit cannot be a branch out of the structured block.
9017     // longjmp() and throw() must not violate the entry/exit criteria.
9018     CS->getCapturedDecl()->setNothrow();
9019   }
9020 
9021   if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
9022     Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
9023     return StmtError();
9024   }
9025   return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
9026                                           AStmt);
9027 }
9028 
9029 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
9030                                            Stmt *AStmt, SourceLocation StartLoc,
9031                                            SourceLocation EndLoc) {
9032   if (!AStmt)
9033     return StmtError();
9034 
9035   auto *CS = cast<CapturedStmt>(AStmt);
9036   // 1.2.2 OpenMP Language Terminology
9037   // Structured block - An executable statement with a single entry at the
9038   // top and a single exit at the bottom.
9039   // The point of exit cannot be a branch out of the structured block.
9040   // longjmp() and throw() must not violate the entry/exit criteria.
9041   CS->getCapturedDecl()->setNothrow();
9042 
9043   setFunctionHasBranchProtectedScope();
9044 
9045   DSAStack->setParentTeamsRegionLoc(StartLoc);
9046 
9047   return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
9048 }
9049 
9050 StmtResult
9051 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
9052                                             SourceLocation EndLoc,
9053                                             OpenMPDirectiveKind CancelRegion) {
9054   if (DSAStack->isParentNowaitRegion()) {
9055     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
9056     return StmtError();
9057   }
9058   if (DSAStack->isParentOrderedRegion()) {
9059     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
9060     return StmtError();
9061   }
9062   return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
9063                                                CancelRegion);
9064 }
9065 
9066 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
9067                                             SourceLocation StartLoc,
9068                                             SourceLocation EndLoc,
9069                                             OpenMPDirectiveKind CancelRegion) {
9070   if (DSAStack->isParentNowaitRegion()) {
9071     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
9072     return StmtError();
9073   }
9074   if (DSAStack->isParentOrderedRegion()) {
9075     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
9076     return StmtError();
9077   }
9078   DSAStack->setParentCancelRegion(/*Cancel=*/true);
9079   return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
9080                                     CancelRegion);
9081 }
9082 
9083 static bool checkGrainsizeNumTasksClauses(Sema &S,
9084                                           ArrayRef<OMPClause *> Clauses) {
9085   const OMPClause *PrevClause = nullptr;
9086   bool ErrorFound = false;
9087   for (const OMPClause *C : Clauses) {
9088     if (C->getClauseKind() == OMPC_grainsize ||
9089         C->getClauseKind() == OMPC_num_tasks) {
9090       if (!PrevClause)
9091         PrevClause = C;
9092       else if (PrevClause->getClauseKind() != C->getClauseKind()) {
9093         S.Diag(C->getBeginLoc(),
9094                diag::err_omp_grainsize_num_tasks_mutually_exclusive)
9095             << getOpenMPClauseName(C->getClauseKind())
9096             << getOpenMPClauseName(PrevClause->getClauseKind());
9097         S.Diag(PrevClause->getBeginLoc(),
9098                diag::note_omp_previous_grainsize_num_tasks)
9099             << getOpenMPClauseName(PrevClause->getClauseKind());
9100         ErrorFound = true;
9101       }
9102     }
9103   }
9104   return ErrorFound;
9105 }
9106 
9107 static bool checkReductionClauseWithNogroup(Sema &S,
9108                                             ArrayRef<OMPClause *> Clauses) {
9109   const OMPClause *ReductionClause = nullptr;
9110   const OMPClause *NogroupClause = nullptr;
9111   for (const OMPClause *C : Clauses) {
9112     if (C->getClauseKind() == OMPC_reduction) {
9113       ReductionClause = C;
9114       if (NogroupClause)
9115         break;
9116       continue;
9117     }
9118     if (C->getClauseKind() == OMPC_nogroup) {
9119       NogroupClause = C;
9120       if (ReductionClause)
9121         break;
9122       continue;
9123     }
9124   }
9125   if (ReductionClause && NogroupClause) {
9126     S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup)
9127         << SourceRange(NogroupClause->getBeginLoc(),
9128                        NogroupClause->getEndLoc());
9129     return true;
9130   }
9131   return false;
9132 }
9133 
9134 StmtResult Sema::ActOnOpenMPTaskLoopDirective(
9135     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9136     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9137   if (!AStmt)
9138     return StmtError();
9139 
9140   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9141   OMPLoopDirective::HelperExprs B;
9142   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
9143   // define the nested loops number.
9144   unsigned NestedLoopCount =
9145       checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
9146                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
9147                       VarsWithImplicitDSA, B);
9148   if (NestedLoopCount == 0)
9149     return StmtError();
9150 
9151   assert((CurContext->isDependentContext() || B.builtAll()) &&
9152          "omp for loop exprs were not built");
9153 
9154   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
9155   // The grainsize clause and num_tasks clause are mutually exclusive and may
9156   // not appear on the same taskloop directive.
9157   if (checkGrainsizeNumTasksClauses(*this, Clauses))
9158     return StmtError();
9159   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
9160   // If a reduction clause is present on the taskloop directive, the nogroup
9161   // clause must not be specified.
9162   if (checkReductionClauseWithNogroup(*this, Clauses))
9163     return StmtError();
9164 
9165   setFunctionHasBranchProtectedScope();
9166   return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
9167                                       NestedLoopCount, Clauses, AStmt, B);
9168 }
9169 
9170 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
9171     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9172     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9173   if (!AStmt)
9174     return StmtError();
9175 
9176   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9177   OMPLoopDirective::HelperExprs B;
9178   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
9179   // define the nested loops number.
9180   unsigned NestedLoopCount =
9181       checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
9182                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
9183                       VarsWithImplicitDSA, B);
9184   if (NestedLoopCount == 0)
9185     return StmtError();
9186 
9187   assert((CurContext->isDependentContext() || B.builtAll()) &&
9188          "omp for loop exprs were not built");
9189 
9190   if (!CurContext->isDependentContext()) {
9191     // Finalize the clauses that need pre-built expressions for CodeGen.
9192     for (OMPClause *C : Clauses) {
9193       if (auto *LC = dyn_cast<OMPLinearClause>(C))
9194         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9195                                      B.NumIterations, *this, CurScope,
9196                                      DSAStack))
9197           return StmtError();
9198     }
9199   }
9200 
9201   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
9202   // The grainsize clause and num_tasks clause are mutually exclusive and may
9203   // not appear on the same taskloop directive.
9204   if (checkGrainsizeNumTasksClauses(*this, Clauses))
9205     return StmtError();
9206   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
9207   // If a reduction clause is present on the taskloop directive, the nogroup
9208   // clause must not be specified.
9209   if (checkReductionClauseWithNogroup(*this, Clauses))
9210     return StmtError();
9211   if (checkSimdlenSafelenSpecified(*this, Clauses))
9212     return StmtError();
9213 
9214   setFunctionHasBranchProtectedScope();
9215   return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
9216                                           NestedLoopCount, Clauses, AStmt, B);
9217 }
9218 
9219 StmtResult Sema::ActOnOpenMPDistributeDirective(
9220     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9221     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9222   if (!AStmt)
9223     return StmtError();
9224 
9225   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9226   OMPLoopDirective::HelperExprs B;
9227   // In presence of clause 'collapse' with number of loops, it will
9228   // define the nested loops number.
9229   unsigned NestedLoopCount =
9230       checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
9231                       nullptr /*ordered not a clause on distribute*/, AStmt,
9232                       *this, *DSAStack, VarsWithImplicitDSA, B);
9233   if (NestedLoopCount == 0)
9234     return StmtError();
9235 
9236   assert((CurContext->isDependentContext() || B.builtAll()) &&
9237          "omp for loop exprs were not built");
9238 
9239   setFunctionHasBranchProtectedScope();
9240   return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
9241                                         NestedLoopCount, Clauses, AStmt, B);
9242 }
9243 
9244 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
9245     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9246     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9247   if (!AStmt)
9248     return StmtError();
9249 
9250   auto *CS = cast<CapturedStmt>(AStmt);
9251   // 1.2.2 OpenMP Language Terminology
9252   // Structured block - An executable statement with a single entry at the
9253   // top and a single exit at the bottom.
9254   // The point of exit cannot be a branch out of the structured block.
9255   // longjmp() and throw() must not violate the entry/exit criteria.
9256   CS->getCapturedDecl()->setNothrow();
9257   for (int ThisCaptureLevel =
9258            getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
9259        ThisCaptureLevel > 1; --ThisCaptureLevel) {
9260     CS = cast<CapturedStmt>(CS->getCapturedStmt());
9261     // 1.2.2 OpenMP Language Terminology
9262     // Structured block - An executable statement with a single entry at the
9263     // top and a single exit at the bottom.
9264     // The point of exit cannot be a branch out of the structured block.
9265     // longjmp() and throw() must not violate the entry/exit criteria.
9266     CS->getCapturedDecl()->setNothrow();
9267   }
9268 
9269   OMPLoopDirective::HelperExprs B;
9270   // In presence of clause 'collapse' with number of loops, it will
9271   // define the nested loops number.
9272   unsigned NestedLoopCount = checkOpenMPLoop(
9273       OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
9274       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
9275       VarsWithImplicitDSA, B);
9276   if (NestedLoopCount == 0)
9277     return StmtError();
9278 
9279   assert((CurContext->isDependentContext() || B.builtAll()) &&
9280          "omp for loop exprs were not built");
9281 
9282   setFunctionHasBranchProtectedScope();
9283   return OMPDistributeParallelForDirective::Create(
9284       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
9285       DSAStack->isCancelRegion());
9286 }
9287 
9288 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
9289     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9290     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9291   if (!AStmt)
9292     return StmtError();
9293 
9294   auto *CS = cast<CapturedStmt>(AStmt);
9295   // 1.2.2 OpenMP Language Terminology
9296   // Structured block - An executable statement with a single entry at the
9297   // top and a single exit at the bottom.
9298   // The point of exit cannot be a branch out of the structured block.
9299   // longjmp() and throw() must not violate the entry/exit criteria.
9300   CS->getCapturedDecl()->setNothrow();
9301   for (int ThisCaptureLevel =
9302            getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
9303        ThisCaptureLevel > 1; --ThisCaptureLevel) {
9304     CS = cast<CapturedStmt>(CS->getCapturedStmt());
9305     // 1.2.2 OpenMP Language Terminology
9306     // Structured block - An executable statement with a single entry at the
9307     // top and a single exit at the bottom.
9308     // The point of exit cannot be a branch out of the structured block.
9309     // longjmp() and throw() must not violate the entry/exit criteria.
9310     CS->getCapturedDecl()->setNothrow();
9311   }
9312 
9313   OMPLoopDirective::HelperExprs B;
9314   // In presence of clause 'collapse' with number of loops, it will
9315   // define the nested loops number.
9316   unsigned NestedLoopCount = checkOpenMPLoop(
9317       OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
9318       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
9319       VarsWithImplicitDSA, B);
9320   if (NestedLoopCount == 0)
9321     return StmtError();
9322 
9323   assert((CurContext->isDependentContext() || B.builtAll()) &&
9324          "omp for loop exprs were not built");
9325 
9326   if (!CurContext->isDependentContext()) {
9327     // Finalize the clauses that need pre-built expressions for CodeGen.
9328     for (OMPClause *C : Clauses) {
9329       if (auto *LC = dyn_cast<OMPLinearClause>(C))
9330         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9331                                      B.NumIterations, *this, CurScope,
9332                                      DSAStack))
9333           return StmtError();
9334     }
9335   }
9336 
9337   if (checkSimdlenSafelenSpecified(*this, Clauses))
9338     return StmtError();
9339 
9340   setFunctionHasBranchProtectedScope();
9341   return OMPDistributeParallelForSimdDirective::Create(
9342       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
9343 }
9344 
9345 StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
9346     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9347     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9348   if (!AStmt)
9349     return StmtError();
9350 
9351   auto *CS = cast<CapturedStmt>(AStmt);
9352   // 1.2.2 OpenMP Language Terminology
9353   // Structured block - An executable statement with a single entry at the
9354   // top and a single exit at the bottom.
9355   // The point of exit cannot be a branch out of the structured block.
9356   // longjmp() and throw() must not violate the entry/exit criteria.
9357   CS->getCapturedDecl()->setNothrow();
9358   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
9359        ThisCaptureLevel > 1; --ThisCaptureLevel) {
9360     CS = cast<CapturedStmt>(CS->getCapturedStmt());
9361     // 1.2.2 OpenMP Language Terminology
9362     // Structured block - An executable statement with a single entry at the
9363     // top and a single exit at the bottom.
9364     // The point of exit cannot be a branch out of the structured block.
9365     // longjmp() and throw() must not violate the entry/exit criteria.
9366     CS->getCapturedDecl()->setNothrow();
9367   }
9368 
9369   OMPLoopDirective::HelperExprs B;
9370   // In presence of clause 'collapse' with number of loops, it will
9371   // define the nested loops number.
9372   unsigned NestedLoopCount =
9373       checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
9374                       nullptr /*ordered not a clause on distribute*/, CS, *this,
9375                       *DSAStack, VarsWithImplicitDSA, B);
9376   if (NestedLoopCount == 0)
9377     return StmtError();
9378 
9379   assert((CurContext->isDependentContext() || B.builtAll()) &&
9380          "omp for loop exprs were not built");
9381 
9382   if (!CurContext->isDependentContext()) {
9383     // Finalize the clauses that need pre-built expressions for CodeGen.
9384     for (OMPClause *C : Clauses) {
9385       if (auto *LC = dyn_cast<OMPLinearClause>(C))
9386         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9387                                      B.NumIterations, *this, CurScope,
9388                                      DSAStack))
9389           return StmtError();
9390     }
9391   }
9392 
9393   if (checkSimdlenSafelenSpecified(*this, Clauses))
9394     return StmtError();
9395 
9396   setFunctionHasBranchProtectedScope();
9397   return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
9398                                             NestedLoopCount, Clauses, AStmt, B);
9399 }
9400 
9401 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
9402     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9403     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9404   if (!AStmt)
9405     return StmtError();
9406 
9407   auto *CS = cast<CapturedStmt>(AStmt);
9408   // 1.2.2 OpenMP Language Terminology
9409   // Structured block - An executable statement with a single entry at the
9410   // top and a single exit at the bottom.
9411   // The point of exit cannot be a branch out of the structured block.
9412   // longjmp() and throw() must not violate the entry/exit criteria.
9413   CS->getCapturedDecl()->setNothrow();
9414   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
9415        ThisCaptureLevel > 1; --ThisCaptureLevel) {
9416     CS = cast<CapturedStmt>(CS->getCapturedStmt());
9417     // 1.2.2 OpenMP Language Terminology
9418     // Structured block - An executable statement with a single entry at the
9419     // top and a single exit at the bottom.
9420     // The point of exit cannot be a branch out of the structured block.
9421     // longjmp() and throw() must not violate the entry/exit criteria.
9422     CS->getCapturedDecl()->setNothrow();
9423   }
9424 
9425   OMPLoopDirective::HelperExprs B;
9426   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
9427   // define the nested loops number.
9428   unsigned NestedLoopCount = checkOpenMPLoop(
9429       OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
9430       getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
9431       VarsWithImplicitDSA, B);
9432   if (NestedLoopCount == 0)
9433     return StmtError();
9434 
9435   assert((CurContext->isDependentContext() || B.builtAll()) &&
9436          "omp target parallel for simd loop exprs were not built");
9437 
9438   if (!CurContext->isDependentContext()) {
9439     // Finalize the clauses that need pre-built expressions for CodeGen.
9440     for (OMPClause *C : Clauses) {
9441       if (auto *LC = dyn_cast<OMPLinearClause>(C))
9442         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9443                                      B.NumIterations, *this, CurScope,
9444                                      DSAStack))
9445           return StmtError();
9446     }
9447   }
9448   if (checkSimdlenSafelenSpecified(*this, Clauses))
9449     return StmtError();
9450 
9451   setFunctionHasBranchProtectedScope();
9452   return OMPTargetParallelForSimdDirective::Create(
9453       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
9454 }
9455 
9456 StmtResult Sema::ActOnOpenMPTargetSimdDirective(
9457     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9458     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9459   if (!AStmt)
9460     return StmtError();
9461 
9462   auto *CS = cast<CapturedStmt>(AStmt);
9463   // 1.2.2 OpenMP Language Terminology
9464   // Structured block - An executable statement with a single entry at the
9465   // top and a single exit at the bottom.
9466   // The point of exit cannot be a branch out of the structured block.
9467   // longjmp() and throw() must not violate the entry/exit criteria.
9468   CS->getCapturedDecl()->setNothrow();
9469   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
9470        ThisCaptureLevel > 1; --ThisCaptureLevel) {
9471     CS = cast<CapturedStmt>(CS->getCapturedStmt());
9472     // 1.2.2 OpenMP Language Terminology
9473     // Structured block - An executable statement with a single entry at the
9474     // top and a single exit at the bottom.
9475     // The point of exit cannot be a branch out of the structured block.
9476     // longjmp() and throw() must not violate the entry/exit criteria.
9477     CS->getCapturedDecl()->setNothrow();
9478   }
9479 
9480   OMPLoopDirective::HelperExprs B;
9481   // In presence of clause 'collapse' with number of loops, it will define the
9482   // nested loops number.
9483   unsigned NestedLoopCount =
9484       checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
9485                       getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
9486                       VarsWithImplicitDSA, B);
9487   if (NestedLoopCount == 0)
9488     return StmtError();
9489 
9490   assert((CurContext->isDependentContext() || B.builtAll()) &&
9491          "omp target simd loop exprs were not built");
9492 
9493   if (!CurContext->isDependentContext()) {
9494     // Finalize the clauses that need pre-built expressions for CodeGen.
9495     for (OMPClause *C : Clauses) {
9496       if (auto *LC = dyn_cast<OMPLinearClause>(C))
9497         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9498                                      B.NumIterations, *this, CurScope,
9499                                      DSAStack))
9500           return StmtError();
9501     }
9502   }
9503 
9504   if (checkSimdlenSafelenSpecified(*this, Clauses))
9505     return StmtError();
9506 
9507   setFunctionHasBranchProtectedScope();
9508   return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
9509                                         NestedLoopCount, Clauses, AStmt, B);
9510 }
9511 
9512 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
9513     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9514     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9515   if (!AStmt)
9516     return StmtError();
9517 
9518   auto *CS = cast<CapturedStmt>(AStmt);
9519   // 1.2.2 OpenMP Language Terminology
9520   // Structured block - An executable statement with a single entry at the
9521   // top and a single exit at the bottom.
9522   // The point of exit cannot be a branch out of the structured block.
9523   // longjmp() and throw() must not violate the entry/exit criteria.
9524   CS->getCapturedDecl()->setNothrow();
9525   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
9526        ThisCaptureLevel > 1; --ThisCaptureLevel) {
9527     CS = cast<CapturedStmt>(CS->getCapturedStmt());
9528     // 1.2.2 OpenMP Language Terminology
9529     // Structured block - An executable statement with a single entry at the
9530     // top and a single exit at the bottom.
9531     // The point of exit cannot be a branch out of the structured block.
9532     // longjmp() and throw() must not violate the entry/exit criteria.
9533     CS->getCapturedDecl()->setNothrow();
9534   }
9535 
9536   OMPLoopDirective::HelperExprs B;
9537   // In presence of clause 'collapse' with number of loops, it will
9538   // define the nested loops number.
9539   unsigned NestedLoopCount =
9540       checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
9541                       nullptr /*ordered not a clause on distribute*/, CS, *this,
9542                       *DSAStack, VarsWithImplicitDSA, B);
9543   if (NestedLoopCount == 0)
9544     return StmtError();
9545 
9546   assert((CurContext->isDependentContext() || B.builtAll()) &&
9547          "omp teams distribute loop exprs were not built");
9548 
9549   setFunctionHasBranchProtectedScope();
9550 
9551   DSAStack->setParentTeamsRegionLoc(StartLoc);
9552 
9553   return OMPTeamsDistributeDirective::Create(
9554       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
9555 }
9556 
9557 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
9558     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9559     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9560   if (!AStmt)
9561     return StmtError();
9562 
9563   auto *CS = cast<CapturedStmt>(AStmt);
9564   // 1.2.2 OpenMP Language Terminology
9565   // Structured block - An executable statement with a single entry at the
9566   // top and a single exit at the bottom.
9567   // The point of exit cannot be a branch out of the structured block.
9568   // longjmp() and throw() must not violate the entry/exit criteria.
9569   CS->getCapturedDecl()->setNothrow();
9570   for (int ThisCaptureLevel =
9571            getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
9572        ThisCaptureLevel > 1; --ThisCaptureLevel) {
9573     CS = cast<CapturedStmt>(CS->getCapturedStmt());
9574     // 1.2.2 OpenMP Language Terminology
9575     // Structured block - An executable statement with a single entry at the
9576     // top and a single exit at the bottom.
9577     // The point of exit cannot be a branch out of the structured block.
9578     // longjmp() and throw() must not violate the entry/exit criteria.
9579     CS->getCapturedDecl()->setNothrow();
9580   }
9581 
9582 
9583   OMPLoopDirective::HelperExprs B;
9584   // In presence of clause 'collapse' with number of loops, it will
9585   // define the nested loops number.
9586   unsigned NestedLoopCount = checkOpenMPLoop(
9587       OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
9588       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
9589       VarsWithImplicitDSA, B);
9590 
9591   if (NestedLoopCount == 0)
9592     return StmtError();
9593 
9594   assert((CurContext->isDependentContext() || B.builtAll()) &&
9595          "omp teams distribute simd loop exprs were not built");
9596 
9597   if (!CurContext->isDependentContext()) {
9598     // Finalize the clauses that need pre-built expressions for CodeGen.
9599     for (OMPClause *C : Clauses) {
9600       if (auto *LC = dyn_cast<OMPLinearClause>(C))
9601         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9602                                      B.NumIterations, *this, CurScope,
9603                                      DSAStack))
9604           return StmtError();
9605     }
9606   }
9607 
9608   if (checkSimdlenSafelenSpecified(*this, Clauses))
9609     return StmtError();
9610 
9611   setFunctionHasBranchProtectedScope();
9612 
9613   DSAStack->setParentTeamsRegionLoc(StartLoc);
9614 
9615   return OMPTeamsDistributeSimdDirective::Create(
9616       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
9617 }
9618 
9619 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
9620     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9621     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9622   if (!AStmt)
9623     return StmtError();
9624 
9625   auto *CS = cast<CapturedStmt>(AStmt);
9626   // 1.2.2 OpenMP Language Terminology
9627   // Structured block - An executable statement with a single entry at the
9628   // top and a single exit at the bottom.
9629   // The point of exit cannot be a branch out of the structured block.
9630   // longjmp() and throw() must not violate the entry/exit criteria.
9631   CS->getCapturedDecl()->setNothrow();
9632 
9633   for (int ThisCaptureLevel =
9634            getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
9635        ThisCaptureLevel > 1; --ThisCaptureLevel) {
9636     CS = cast<CapturedStmt>(CS->getCapturedStmt());
9637     // 1.2.2 OpenMP Language Terminology
9638     // Structured block - An executable statement with a single entry at the
9639     // top and a single exit at the bottom.
9640     // The point of exit cannot be a branch out of the structured block.
9641     // longjmp() and throw() must not violate the entry/exit criteria.
9642     CS->getCapturedDecl()->setNothrow();
9643   }
9644 
9645   OMPLoopDirective::HelperExprs B;
9646   // In presence of clause 'collapse' with number of loops, it will
9647   // define the nested loops number.
9648   unsigned NestedLoopCount = checkOpenMPLoop(
9649       OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
9650       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
9651       VarsWithImplicitDSA, B);
9652 
9653   if (NestedLoopCount == 0)
9654     return StmtError();
9655 
9656   assert((CurContext->isDependentContext() || B.builtAll()) &&
9657          "omp for loop exprs were not built");
9658 
9659   if (!CurContext->isDependentContext()) {
9660     // Finalize the clauses that need pre-built expressions for CodeGen.
9661     for (OMPClause *C : Clauses) {
9662       if (auto *LC = dyn_cast<OMPLinearClause>(C))
9663         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9664                                      B.NumIterations, *this, CurScope,
9665                                      DSAStack))
9666           return StmtError();
9667     }
9668   }
9669 
9670   if (checkSimdlenSafelenSpecified(*this, Clauses))
9671     return StmtError();
9672 
9673   setFunctionHasBranchProtectedScope();
9674 
9675   DSAStack->setParentTeamsRegionLoc(StartLoc);
9676 
9677   return OMPTeamsDistributeParallelForSimdDirective::Create(
9678       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
9679 }
9680 
9681 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
9682     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9683     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9684   if (!AStmt)
9685     return StmtError();
9686 
9687   auto *CS = cast<CapturedStmt>(AStmt);
9688   // 1.2.2 OpenMP Language Terminology
9689   // Structured block - An executable statement with a single entry at the
9690   // top and a single exit at the bottom.
9691   // The point of exit cannot be a branch out of the structured block.
9692   // longjmp() and throw() must not violate the entry/exit criteria.
9693   CS->getCapturedDecl()->setNothrow();
9694 
9695   for (int ThisCaptureLevel =
9696            getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
9697        ThisCaptureLevel > 1; --ThisCaptureLevel) {
9698     CS = cast<CapturedStmt>(CS->getCapturedStmt());
9699     // 1.2.2 OpenMP Language Terminology
9700     // Structured block - An executable statement with a single entry at the
9701     // top and a single exit at the bottom.
9702     // The point of exit cannot be a branch out of the structured block.
9703     // longjmp() and throw() must not violate the entry/exit criteria.
9704     CS->getCapturedDecl()->setNothrow();
9705   }
9706 
9707   OMPLoopDirective::HelperExprs B;
9708   // In presence of clause 'collapse' with number of loops, it will
9709   // define the nested loops number.
9710   unsigned NestedLoopCount = checkOpenMPLoop(
9711       OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
9712       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
9713       VarsWithImplicitDSA, B);
9714 
9715   if (NestedLoopCount == 0)
9716     return StmtError();
9717 
9718   assert((CurContext->isDependentContext() || B.builtAll()) &&
9719          "omp for loop exprs were not built");
9720 
9721   setFunctionHasBranchProtectedScope();
9722 
9723   DSAStack->setParentTeamsRegionLoc(StartLoc);
9724 
9725   return OMPTeamsDistributeParallelForDirective::Create(
9726       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
9727       DSAStack->isCancelRegion());
9728 }
9729 
9730 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
9731                                                  Stmt *AStmt,
9732                                                  SourceLocation StartLoc,
9733                                                  SourceLocation EndLoc) {
9734   if (!AStmt)
9735     return StmtError();
9736 
9737   auto *CS = cast<CapturedStmt>(AStmt);
9738   // 1.2.2 OpenMP Language Terminology
9739   // Structured block - An executable statement with a single entry at the
9740   // top and a single exit at the bottom.
9741   // The point of exit cannot be a branch out of the structured block.
9742   // longjmp() and throw() must not violate the entry/exit criteria.
9743   CS->getCapturedDecl()->setNothrow();
9744 
9745   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
9746        ThisCaptureLevel > 1; --ThisCaptureLevel) {
9747     CS = cast<CapturedStmt>(CS->getCapturedStmt());
9748     // 1.2.2 OpenMP Language Terminology
9749     // Structured block - An executable statement with a single entry at the
9750     // top and a single exit at the bottom.
9751     // The point of exit cannot be a branch out of the structured block.
9752     // longjmp() and throw() must not violate the entry/exit criteria.
9753     CS->getCapturedDecl()->setNothrow();
9754   }
9755   setFunctionHasBranchProtectedScope();
9756 
9757   return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
9758                                          AStmt);
9759 }
9760 
9761 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
9762     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9763     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9764   if (!AStmt)
9765     return StmtError();
9766 
9767   auto *CS = cast<CapturedStmt>(AStmt);
9768   // 1.2.2 OpenMP Language Terminology
9769   // Structured block - An executable statement with a single entry at the
9770   // top and a single exit at the bottom.
9771   // The point of exit cannot be a branch out of the structured block.
9772   // longjmp() and throw() must not violate the entry/exit criteria.
9773   CS->getCapturedDecl()->setNothrow();
9774   for (int ThisCaptureLevel =
9775            getOpenMPCaptureLevels(OMPD_target_teams_distribute);
9776        ThisCaptureLevel > 1; --ThisCaptureLevel) {
9777     CS = cast<CapturedStmt>(CS->getCapturedStmt());
9778     // 1.2.2 OpenMP Language Terminology
9779     // Structured block - An executable statement with a single entry at the
9780     // top and a single exit at the bottom.
9781     // The point of exit cannot be a branch out of the structured block.
9782     // longjmp() and throw() must not violate the entry/exit criteria.
9783     CS->getCapturedDecl()->setNothrow();
9784   }
9785 
9786   OMPLoopDirective::HelperExprs B;
9787   // In presence of clause 'collapse' with number of loops, it will
9788   // define the nested loops number.
9789   unsigned NestedLoopCount = checkOpenMPLoop(
9790       OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
9791       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
9792       VarsWithImplicitDSA, B);
9793   if (NestedLoopCount == 0)
9794     return StmtError();
9795 
9796   assert((CurContext->isDependentContext() || B.builtAll()) &&
9797          "omp target teams distribute loop exprs were not built");
9798 
9799   setFunctionHasBranchProtectedScope();
9800   return OMPTargetTeamsDistributeDirective::Create(
9801       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
9802 }
9803 
9804 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
9805     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9806     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9807   if (!AStmt)
9808     return StmtError();
9809 
9810   auto *CS = cast<CapturedStmt>(AStmt);
9811   // 1.2.2 OpenMP Language Terminology
9812   // Structured block - An executable statement with a single entry at the
9813   // top and a single exit at the bottom.
9814   // The point of exit cannot be a branch out of the structured block.
9815   // longjmp() and throw() must not violate the entry/exit criteria.
9816   CS->getCapturedDecl()->setNothrow();
9817   for (int ThisCaptureLevel =
9818            getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
9819        ThisCaptureLevel > 1; --ThisCaptureLevel) {
9820     CS = cast<CapturedStmt>(CS->getCapturedStmt());
9821     // 1.2.2 OpenMP Language Terminology
9822     // Structured block - An executable statement with a single entry at the
9823     // top and a single exit at the bottom.
9824     // The point of exit cannot be a branch out of the structured block.
9825     // longjmp() and throw() must not violate the entry/exit criteria.
9826     CS->getCapturedDecl()->setNothrow();
9827   }
9828 
9829   OMPLoopDirective::HelperExprs B;
9830   // In presence of clause 'collapse' with number of loops, it will
9831   // define the nested loops number.
9832   unsigned NestedLoopCount = checkOpenMPLoop(
9833       OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
9834       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
9835       VarsWithImplicitDSA, B);
9836   if (NestedLoopCount == 0)
9837     return StmtError();
9838 
9839   assert((CurContext->isDependentContext() || B.builtAll()) &&
9840          "omp target teams distribute parallel for loop exprs were not built");
9841 
9842   if (!CurContext->isDependentContext()) {
9843     // Finalize the clauses that need pre-built expressions for CodeGen.
9844     for (OMPClause *C : Clauses) {
9845       if (auto *LC = dyn_cast<OMPLinearClause>(C))
9846         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9847                                      B.NumIterations, *this, CurScope,
9848                                      DSAStack))
9849           return StmtError();
9850     }
9851   }
9852 
9853   setFunctionHasBranchProtectedScope();
9854   return OMPTargetTeamsDistributeParallelForDirective::Create(
9855       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
9856       DSAStack->isCancelRegion());
9857 }
9858 
9859 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
9860     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9861     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9862   if (!AStmt)
9863     return StmtError();
9864 
9865   auto *CS = cast<CapturedStmt>(AStmt);
9866   // 1.2.2 OpenMP Language Terminology
9867   // Structured block - An executable statement with a single entry at the
9868   // top and a single exit at the bottom.
9869   // The point of exit cannot be a branch out of the structured block.
9870   // longjmp() and throw() must not violate the entry/exit criteria.
9871   CS->getCapturedDecl()->setNothrow();
9872   for (int ThisCaptureLevel = getOpenMPCaptureLevels(
9873            OMPD_target_teams_distribute_parallel_for_simd);
9874        ThisCaptureLevel > 1; --ThisCaptureLevel) {
9875     CS = cast<CapturedStmt>(CS->getCapturedStmt());
9876     // 1.2.2 OpenMP Language Terminology
9877     // Structured block - An executable statement with a single entry at the
9878     // top and a single exit at the bottom.
9879     // The point of exit cannot be a branch out of the structured block.
9880     // longjmp() and throw() must not violate the entry/exit criteria.
9881     CS->getCapturedDecl()->setNothrow();
9882   }
9883 
9884   OMPLoopDirective::HelperExprs B;
9885   // In presence of clause 'collapse' with number of loops, it will
9886   // define the nested loops number.
9887   unsigned NestedLoopCount =
9888       checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd,
9889                       getCollapseNumberExpr(Clauses),
9890                       nullptr /*ordered not a clause on distribute*/, CS, *this,
9891                       *DSAStack, VarsWithImplicitDSA, B);
9892   if (NestedLoopCount == 0)
9893     return StmtError();
9894 
9895   assert((CurContext->isDependentContext() || B.builtAll()) &&
9896          "omp target teams distribute parallel for simd loop exprs were not "
9897          "built");
9898 
9899   if (!CurContext->isDependentContext()) {
9900     // Finalize the clauses that need pre-built expressions for CodeGen.
9901     for (OMPClause *C : Clauses) {
9902       if (auto *LC = dyn_cast<OMPLinearClause>(C))
9903         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9904                                      B.NumIterations, *this, CurScope,
9905                                      DSAStack))
9906           return StmtError();
9907     }
9908   }
9909 
9910   if (checkSimdlenSafelenSpecified(*this, Clauses))
9911     return StmtError();
9912 
9913   setFunctionHasBranchProtectedScope();
9914   return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
9915       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
9916 }
9917 
9918 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
9919     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
9920     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
9921   if (!AStmt)
9922     return StmtError();
9923 
9924   auto *CS = cast<CapturedStmt>(AStmt);
9925   // 1.2.2 OpenMP Language Terminology
9926   // Structured block - An executable statement with a single entry at the
9927   // top and a single exit at the bottom.
9928   // The point of exit cannot be a branch out of the structured block.
9929   // longjmp() and throw() must not violate the entry/exit criteria.
9930   CS->getCapturedDecl()->setNothrow();
9931   for (int ThisCaptureLevel =
9932            getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
9933        ThisCaptureLevel > 1; --ThisCaptureLevel) {
9934     CS = cast<CapturedStmt>(CS->getCapturedStmt());
9935     // 1.2.2 OpenMP Language Terminology
9936     // Structured block - An executable statement with a single entry at the
9937     // top and a single exit at the bottom.
9938     // The point of exit cannot be a branch out of the structured block.
9939     // longjmp() and throw() must not violate the entry/exit criteria.
9940     CS->getCapturedDecl()->setNothrow();
9941   }
9942 
9943   OMPLoopDirective::HelperExprs B;
9944   // In presence of clause 'collapse' with number of loops, it will
9945   // define the nested loops number.
9946   unsigned NestedLoopCount = checkOpenMPLoop(
9947       OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
9948       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
9949       VarsWithImplicitDSA, B);
9950   if (NestedLoopCount == 0)
9951     return StmtError();
9952 
9953   assert((CurContext->isDependentContext() || B.builtAll()) &&
9954          "omp target teams distribute simd loop exprs were not built");
9955 
9956   if (!CurContext->isDependentContext()) {
9957     // Finalize the clauses that need pre-built expressions for CodeGen.
9958     for (OMPClause *C : Clauses) {
9959       if (auto *LC = dyn_cast<OMPLinearClause>(C))
9960         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
9961                                      B.NumIterations, *this, CurScope,
9962                                      DSAStack))
9963           return StmtError();
9964     }
9965   }
9966 
9967   if (checkSimdlenSafelenSpecified(*this, Clauses))
9968     return StmtError();
9969 
9970   setFunctionHasBranchProtectedScope();
9971   return OMPTargetTeamsDistributeSimdDirective::Create(
9972       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
9973 }
9974 
9975 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
9976                                              SourceLocation StartLoc,
9977                                              SourceLocation LParenLoc,
9978                                              SourceLocation EndLoc) {
9979   OMPClause *Res = nullptr;
9980   switch (Kind) {
9981   case OMPC_final:
9982     Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
9983     break;
9984   case OMPC_num_threads:
9985     Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
9986     break;
9987   case OMPC_safelen:
9988     Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
9989     break;
9990   case OMPC_simdlen:
9991     Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
9992     break;
9993   case OMPC_allocator:
9994     Res = ActOnOpenMPAllocatorClause(Expr, StartLoc, LParenLoc, EndLoc);
9995     break;
9996   case OMPC_collapse:
9997     Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
9998     break;
9999   case OMPC_ordered:
10000     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
10001     break;
10002   case OMPC_device:
10003     Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
10004     break;
10005   case OMPC_num_teams:
10006     Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
10007     break;
10008   case OMPC_thread_limit:
10009     Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
10010     break;
10011   case OMPC_priority:
10012     Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
10013     break;
10014   case OMPC_grainsize:
10015     Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
10016     break;
10017   case OMPC_num_tasks:
10018     Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
10019     break;
10020   case OMPC_hint:
10021     Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
10022     break;
10023   case OMPC_if:
10024   case OMPC_default:
10025   case OMPC_proc_bind:
10026   case OMPC_schedule:
10027   case OMPC_private:
10028   case OMPC_firstprivate:
10029   case OMPC_lastprivate:
10030   case OMPC_shared:
10031   case OMPC_reduction:
10032   case OMPC_task_reduction:
10033   case OMPC_in_reduction:
10034   case OMPC_linear:
10035   case OMPC_aligned:
10036   case OMPC_copyin:
10037   case OMPC_copyprivate:
10038   case OMPC_nowait:
10039   case OMPC_untied:
10040   case OMPC_mergeable:
10041   case OMPC_threadprivate:
10042   case OMPC_allocate:
10043   case OMPC_flush:
10044   case OMPC_read:
10045   case OMPC_write:
10046   case OMPC_update:
10047   case OMPC_capture:
10048   case OMPC_seq_cst:
10049   case OMPC_depend:
10050   case OMPC_threads:
10051   case OMPC_simd:
10052   case OMPC_map:
10053   case OMPC_nogroup:
10054   case OMPC_dist_schedule:
10055   case OMPC_defaultmap:
10056   case OMPC_unknown:
10057   case OMPC_uniform:
10058   case OMPC_to:
10059   case OMPC_from:
10060   case OMPC_use_device_ptr:
10061   case OMPC_is_device_ptr:
10062   case OMPC_unified_address:
10063   case OMPC_unified_shared_memory:
10064   case OMPC_reverse_offload:
10065   case OMPC_dynamic_allocators:
10066   case OMPC_atomic_default_mem_order:
10067   case OMPC_device_type:
10068     llvm_unreachable("Clause is not allowed.");
10069   }
10070   return Res;
10071 }
10072 
10073 // An OpenMP directive such as 'target parallel' has two captured regions:
10074 // for the 'target' and 'parallel' respectively.  This function returns
10075 // the region in which to capture expressions associated with a clause.
10076 // A return value of OMPD_unknown signifies that the expression should not
10077 // be captured.
10078 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
10079     OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
10080     OpenMPDirectiveKind NameModifier = OMPD_unknown) {
10081   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
10082   switch (CKind) {
10083   case OMPC_if:
10084     switch (DKind) {
10085     case OMPD_target_parallel:
10086     case OMPD_target_parallel_for:
10087     case OMPD_target_parallel_for_simd:
10088       // If this clause applies to the nested 'parallel' region, capture within
10089       // the 'target' region, otherwise do not capture.
10090       if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
10091         CaptureRegion = OMPD_target;
10092       break;
10093     case OMPD_target_teams_distribute_parallel_for:
10094     case OMPD_target_teams_distribute_parallel_for_simd:
10095       // If this clause applies to the nested 'parallel' region, capture within
10096       // the 'teams' region, otherwise do not capture.
10097       if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
10098         CaptureRegion = OMPD_teams;
10099       break;
10100     case OMPD_teams_distribute_parallel_for:
10101     case OMPD_teams_distribute_parallel_for_simd:
10102       CaptureRegion = OMPD_teams;
10103       break;
10104     case OMPD_target_update:
10105     case OMPD_target_enter_data:
10106     case OMPD_target_exit_data:
10107       CaptureRegion = OMPD_task;
10108       break;
10109     case OMPD_cancel:
10110     case OMPD_parallel:
10111     case OMPD_parallel_sections:
10112     case OMPD_parallel_for:
10113     case OMPD_parallel_for_simd:
10114     case OMPD_target:
10115     case OMPD_target_simd:
10116     case OMPD_target_teams:
10117     case OMPD_target_teams_distribute:
10118     case OMPD_target_teams_distribute_simd:
10119     case OMPD_distribute_parallel_for:
10120     case OMPD_distribute_parallel_for_simd:
10121     case OMPD_task:
10122     case OMPD_taskloop:
10123     case OMPD_taskloop_simd:
10124     case OMPD_target_data:
10125       // Do not capture if-clause expressions.
10126       break;
10127     case OMPD_threadprivate:
10128     case OMPD_allocate:
10129     case OMPD_taskyield:
10130     case OMPD_barrier:
10131     case OMPD_taskwait:
10132     case OMPD_cancellation_point:
10133     case OMPD_flush:
10134     case OMPD_declare_reduction:
10135     case OMPD_declare_mapper:
10136     case OMPD_declare_simd:
10137     case OMPD_declare_variant:
10138     case OMPD_declare_target:
10139     case OMPD_end_declare_target:
10140     case OMPD_teams:
10141     case OMPD_simd:
10142     case OMPD_for:
10143     case OMPD_for_simd:
10144     case OMPD_sections:
10145     case OMPD_section:
10146     case OMPD_single:
10147     case OMPD_master:
10148     case OMPD_critical:
10149     case OMPD_taskgroup:
10150     case OMPD_distribute:
10151     case OMPD_ordered:
10152     case OMPD_atomic:
10153     case OMPD_distribute_simd:
10154     case OMPD_teams_distribute:
10155     case OMPD_teams_distribute_simd:
10156     case OMPD_requires:
10157       llvm_unreachable("Unexpected OpenMP directive with if-clause");
10158     case OMPD_unknown:
10159       llvm_unreachable("Unknown OpenMP directive");
10160     }
10161     break;
10162   case OMPC_num_threads:
10163     switch (DKind) {
10164     case OMPD_target_parallel:
10165     case OMPD_target_parallel_for:
10166     case OMPD_target_parallel_for_simd:
10167       CaptureRegion = OMPD_target;
10168       break;
10169     case OMPD_teams_distribute_parallel_for:
10170     case OMPD_teams_distribute_parallel_for_simd:
10171     case OMPD_target_teams_distribute_parallel_for:
10172     case OMPD_target_teams_distribute_parallel_for_simd:
10173       CaptureRegion = OMPD_teams;
10174       break;
10175     case OMPD_parallel:
10176     case OMPD_parallel_sections:
10177     case OMPD_parallel_for:
10178     case OMPD_parallel_for_simd:
10179     case OMPD_distribute_parallel_for:
10180     case OMPD_distribute_parallel_for_simd:
10181       // Do not capture num_threads-clause expressions.
10182       break;
10183     case OMPD_target_data:
10184     case OMPD_target_enter_data:
10185     case OMPD_target_exit_data:
10186     case OMPD_target_update:
10187     case OMPD_target:
10188     case OMPD_target_simd:
10189     case OMPD_target_teams:
10190     case OMPD_target_teams_distribute:
10191     case OMPD_target_teams_distribute_simd:
10192     case OMPD_cancel:
10193     case OMPD_task:
10194     case OMPD_taskloop:
10195     case OMPD_taskloop_simd:
10196     case OMPD_threadprivate:
10197     case OMPD_allocate:
10198     case OMPD_taskyield:
10199     case OMPD_barrier:
10200     case OMPD_taskwait:
10201     case OMPD_cancellation_point:
10202     case OMPD_flush:
10203     case OMPD_declare_reduction:
10204     case OMPD_declare_mapper:
10205     case OMPD_declare_simd:
10206     case OMPD_declare_variant:
10207     case OMPD_declare_target:
10208     case OMPD_end_declare_target:
10209     case OMPD_teams:
10210     case OMPD_simd:
10211     case OMPD_for:
10212     case OMPD_for_simd:
10213     case OMPD_sections:
10214     case OMPD_section:
10215     case OMPD_single:
10216     case OMPD_master:
10217     case OMPD_critical:
10218     case OMPD_taskgroup:
10219     case OMPD_distribute:
10220     case OMPD_ordered:
10221     case OMPD_atomic:
10222     case OMPD_distribute_simd:
10223     case OMPD_teams_distribute:
10224     case OMPD_teams_distribute_simd:
10225     case OMPD_requires:
10226       llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
10227     case OMPD_unknown:
10228       llvm_unreachable("Unknown OpenMP directive");
10229     }
10230     break;
10231   case OMPC_num_teams:
10232     switch (DKind) {
10233     case OMPD_target_teams:
10234     case OMPD_target_teams_distribute:
10235     case OMPD_target_teams_distribute_simd:
10236     case OMPD_target_teams_distribute_parallel_for:
10237     case OMPD_target_teams_distribute_parallel_for_simd:
10238       CaptureRegion = OMPD_target;
10239       break;
10240     case OMPD_teams_distribute_parallel_for:
10241     case OMPD_teams_distribute_parallel_for_simd:
10242     case OMPD_teams:
10243     case OMPD_teams_distribute:
10244     case OMPD_teams_distribute_simd:
10245       // Do not capture num_teams-clause expressions.
10246       break;
10247     case OMPD_distribute_parallel_for:
10248     case OMPD_distribute_parallel_for_simd:
10249     case OMPD_task:
10250     case OMPD_taskloop:
10251     case OMPD_taskloop_simd:
10252     case OMPD_target_data:
10253     case OMPD_target_enter_data:
10254     case OMPD_target_exit_data:
10255     case OMPD_target_update:
10256     case OMPD_cancel:
10257     case OMPD_parallel:
10258     case OMPD_parallel_sections:
10259     case OMPD_parallel_for:
10260     case OMPD_parallel_for_simd:
10261     case OMPD_target:
10262     case OMPD_target_simd:
10263     case OMPD_target_parallel:
10264     case OMPD_target_parallel_for:
10265     case OMPD_target_parallel_for_simd:
10266     case OMPD_threadprivate:
10267     case OMPD_allocate:
10268     case OMPD_taskyield:
10269     case OMPD_barrier:
10270     case OMPD_taskwait:
10271     case OMPD_cancellation_point:
10272     case OMPD_flush:
10273     case OMPD_declare_reduction:
10274     case OMPD_declare_mapper:
10275     case OMPD_declare_simd:
10276     case OMPD_declare_variant:
10277     case OMPD_declare_target:
10278     case OMPD_end_declare_target:
10279     case OMPD_simd:
10280     case OMPD_for:
10281     case OMPD_for_simd:
10282     case OMPD_sections:
10283     case OMPD_section:
10284     case OMPD_single:
10285     case OMPD_master:
10286     case OMPD_critical:
10287     case OMPD_taskgroup:
10288     case OMPD_distribute:
10289     case OMPD_ordered:
10290     case OMPD_atomic:
10291     case OMPD_distribute_simd:
10292     case OMPD_requires:
10293       llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
10294     case OMPD_unknown:
10295       llvm_unreachable("Unknown OpenMP directive");
10296     }
10297     break;
10298   case OMPC_thread_limit:
10299     switch (DKind) {
10300     case OMPD_target_teams:
10301     case OMPD_target_teams_distribute:
10302     case OMPD_target_teams_distribute_simd:
10303     case OMPD_target_teams_distribute_parallel_for:
10304     case OMPD_target_teams_distribute_parallel_for_simd:
10305       CaptureRegion = OMPD_target;
10306       break;
10307     case OMPD_teams_distribute_parallel_for:
10308     case OMPD_teams_distribute_parallel_for_simd:
10309     case OMPD_teams:
10310     case OMPD_teams_distribute:
10311     case OMPD_teams_distribute_simd:
10312       // Do not capture thread_limit-clause expressions.
10313       break;
10314     case OMPD_distribute_parallel_for:
10315     case OMPD_distribute_parallel_for_simd:
10316     case OMPD_task:
10317     case OMPD_taskloop:
10318     case OMPD_taskloop_simd:
10319     case OMPD_target_data:
10320     case OMPD_target_enter_data:
10321     case OMPD_target_exit_data:
10322     case OMPD_target_update:
10323     case OMPD_cancel:
10324     case OMPD_parallel:
10325     case OMPD_parallel_sections:
10326     case OMPD_parallel_for:
10327     case OMPD_parallel_for_simd:
10328     case OMPD_target:
10329     case OMPD_target_simd:
10330     case OMPD_target_parallel:
10331     case OMPD_target_parallel_for:
10332     case OMPD_target_parallel_for_simd:
10333     case OMPD_threadprivate:
10334     case OMPD_allocate:
10335     case OMPD_taskyield:
10336     case OMPD_barrier:
10337     case OMPD_taskwait:
10338     case OMPD_cancellation_point:
10339     case OMPD_flush:
10340     case OMPD_declare_reduction:
10341     case OMPD_declare_mapper:
10342     case OMPD_declare_simd:
10343     case OMPD_declare_variant:
10344     case OMPD_declare_target:
10345     case OMPD_end_declare_target:
10346     case OMPD_simd:
10347     case OMPD_for:
10348     case OMPD_for_simd:
10349     case OMPD_sections:
10350     case OMPD_section:
10351     case OMPD_single:
10352     case OMPD_master:
10353     case OMPD_critical:
10354     case OMPD_taskgroup:
10355     case OMPD_distribute:
10356     case OMPD_ordered:
10357     case OMPD_atomic:
10358     case OMPD_distribute_simd:
10359     case OMPD_requires:
10360       llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
10361     case OMPD_unknown:
10362       llvm_unreachable("Unknown OpenMP directive");
10363     }
10364     break;
10365   case OMPC_schedule:
10366     switch (DKind) {
10367     case OMPD_parallel_for:
10368     case OMPD_parallel_for_simd:
10369     case OMPD_distribute_parallel_for:
10370     case OMPD_distribute_parallel_for_simd:
10371     case OMPD_teams_distribute_parallel_for:
10372     case OMPD_teams_distribute_parallel_for_simd:
10373     case OMPD_target_parallel_for:
10374     case OMPD_target_parallel_for_simd:
10375     case OMPD_target_teams_distribute_parallel_for:
10376     case OMPD_target_teams_distribute_parallel_for_simd:
10377       CaptureRegion = OMPD_parallel;
10378       break;
10379     case OMPD_for:
10380     case OMPD_for_simd:
10381       // Do not capture schedule-clause expressions.
10382       break;
10383     case OMPD_task:
10384     case OMPD_taskloop:
10385     case OMPD_taskloop_simd:
10386     case OMPD_target_data:
10387     case OMPD_target_enter_data:
10388     case OMPD_target_exit_data:
10389     case OMPD_target_update:
10390     case OMPD_teams:
10391     case OMPD_teams_distribute:
10392     case OMPD_teams_distribute_simd:
10393     case OMPD_target_teams_distribute:
10394     case OMPD_target_teams_distribute_simd:
10395     case OMPD_target:
10396     case OMPD_target_simd:
10397     case OMPD_target_parallel:
10398     case OMPD_cancel:
10399     case OMPD_parallel:
10400     case OMPD_parallel_sections:
10401     case OMPD_threadprivate:
10402     case OMPD_allocate:
10403     case OMPD_taskyield:
10404     case OMPD_barrier:
10405     case OMPD_taskwait:
10406     case OMPD_cancellation_point:
10407     case OMPD_flush:
10408     case OMPD_declare_reduction:
10409     case OMPD_declare_mapper:
10410     case OMPD_declare_simd:
10411     case OMPD_declare_variant:
10412     case OMPD_declare_target:
10413     case OMPD_end_declare_target:
10414     case OMPD_simd:
10415     case OMPD_sections:
10416     case OMPD_section:
10417     case OMPD_single:
10418     case OMPD_master:
10419     case OMPD_critical:
10420     case OMPD_taskgroup:
10421     case OMPD_distribute:
10422     case OMPD_ordered:
10423     case OMPD_atomic:
10424     case OMPD_distribute_simd:
10425     case OMPD_target_teams:
10426     case OMPD_requires:
10427       llvm_unreachable("Unexpected OpenMP directive with schedule clause");
10428     case OMPD_unknown:
10429       llvm_unreachable("Unknown OpenMP directive");
10430     }
10431     break;
10432   case OMPC_dist_schedule:
10433     switch (DKind) {
10434     case OMPD_teams_distribute_parallel_for:
10435     case OMPD_teams_distribute_parallel_for_simd:
10436     case OMPD_teams_distribute:
10437     case OMPD_teams_distribute_simd:
10438     case OMPD_target_teams_distribute_parallel_for:
10439     case OMPD_target_teams_distribute_parallel_for_simd:
10440     case OMPD_target_teams_distribute:
10441     case OMPD_target_teams_distribute_simd:
10442       CaptureRegion = OMPD_teams;
10443       break;
10444     case OMPD_distribute_parallel_for:
10445     case OMPD_distribute_parallel_for_simd:
10446     case OMPD_distribute:
10447     case OMPD_distribute_simd:
10448       // Do not capture thread_limit-clause expressions.
10449       break;
10450     case OMPD_parallel_for:
10451     case OMPD_parallel_for_simd:
10452     case OMPD_target_parallel_for_simd:
10453     case OMPD_target_parallel_for:
10454     case OMPD_task:
10455     case OMPD_taskloop:
10456     case OMPD_taskloop_simd:
10457     case OMPD_target_data:
10458     case OMPD_target_enter_data:
10459     case OMPD_target_exit_data:
10460     case OMPD_target_update:
10461     case OMPD_teams:
10462     case OMPD_target:
10463     case OMPD_target_simd:
10464     case OMPD_target_parallel:
10465     case OMPD_cancel:
10466     case OMPD_parallel:
10467     case OMPD_parallel_sections:
10468     case OMPD_threadprivate:
10469     case OMPD_allocate:
10470     case OMPD_taskyield:
10471     case OMPD_barrier:
10472     case OMPD_taskwait:
10473     case OMPD_cancellation_point:
10474     case OMPD_flush:
10475     case OMPD_declare_reduction:
10476     case OMPD_declare_mapper:
10477     case OMPD_declare_simd:
10478     case OMPD_declare_variant:
10479     case OMPD_declare_target:
10480     case OMPD_end_declare_target:
10481     case OMPD_simd:
10482     case OMPD_for:
10483     case OMPD_for_simd:
10484     case OMPD_sections:
10485     case OMPD_section:
10486     case OMPD_single:
10487     case OMPD_master:
10488     case OMPD_critical:
10489     case OMPD_taskgroup:
10490     case OMPD_ordered:
10491     case OMPD_atomic:
10492     case OMPD_target_teams:
10493     case OMPD_requires:
10494       llvm_unreachable("Unexpected OpenMP directive with schedule clause");
10495     case OMPD_unknown:
10496       llvm_unreachable("Unknown OpenMP directive");
10497     }
10498     break;
10499   case OMPC_device:
10500     switch (DKind) {
10501     case OMPD_target_update:
10502     case OMPD_target_enter_data:
10503     case OMPD_target_exit_data:
10504     case OMPD_target:
10505     case OMPD_target_simd:
10506     case OMPD_target_teams:
10507     case OMPD_target_parallel:
10508     case OMPD_target_teams_distribute:
10509     case OMPD_target_teams_distribute_simd:
10510     case OMPD_target_parallel_for:
10511     case OMPD_target_parallel_for_simd:
10512     case OMPD_target_teams_distribute_parallel_for:
10513     case OMPD_target_teams_distribute_parallel_for_simd:
10514       CaptureRegion = OMPD_task;
10515       break;
10516     case OMPD_target_data:
10517       // Do not capture device-clause expressions.
10518       break;
10519     case OMPD_teams_distribute_parallel_for:
10520     case OMPD_teams_distribute_parallel_for_simd:
10521     case OMPD_teams:
10522     case OMPD_teams_distribute:
10523     case OMPD_teams_distribute_simd:
10524     case OMPD_distribute_parallel_for:
10525     case OMPD_distribute_parallel_for_simd:
10526     case OMPD_task:
10527     case OMPD_taskloop:
10528     case OMPD_taskloop_simd:
10529     case OMPD_cancel:
10530     case OMPD_parallel:
10531     case OMPD_parallel_sections:
10532     case OMPD_parallel_for:
10533     case OMPD_parallel_for_simd:
10534     case OMPD_threadprivate:
10535     case OMPD_allocate:
10536     case OMPD_taskyield:
10537     case OMPD_barrier:
10538     case OMPD_taskwait:
10539     case OMPD_cancellation_point:
10540     case OMPD_flush:
10541     case OMPD_declare_reduction:
10542     case OMPD_declare_mapper:
10543     case OMPD_declare_simd:
10544     case OMPD_declare_variant:
10545     case OMPD_declare_target:
10546     case OMPD_end_declare_target:
10547     case OMPD_simd:
10548     case OMPD_for:
10549     case OMPD_for_simd:
10550     case OMPD_sections:
10551     case OMPD_section:
10552     case OMPD_single:
10553     case OMPD_master:
10554     case OMPD_critical:
10555     case OMPD_taskgroup:
10556     case OMPD_distribute:
10557     case OMPD_ordered:
10558     case OMPD_atomic:
10559     case OMPD_distribute_simd:
10560     case OMPD_requires:
10561       llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
10562     case OMPD_unknown:
10563       llvm_unreachable("Unknown OpenMP directive");
10564     }
10565     break;
10566   case OMPC_firstprivate:
10567   case OMPC_lastprivate:
10568   case OMPC_reduction:
10569   case OMPC_task_reduction:
10570   case OMPC_in_reduction:
10571   case OMPC_linear:
10572   case OMPC_default:
10573   case OMPC_proc_bind:
10574   case OMPC_final:
10575   case OMPC_safelen:
10576   case OMPC_simdlen:
10577   case OMPC_allocator:
10578   case OMPC_collapse:
10579   case OMPC_private:
10580   case OMPC_shared:
10581   case OMPC_aligned:
10582   case OMPC_copyin:
10583   case OMPC_copyprivate:
10584   case OMPC_ordered:
10585   case OMPC_nowait:
10586   case OMPC_untied:
10587   case OMPC_mergeable:
10588   case OMPC_threadprivate:
10589   case OMPC_allocate:
10590   case OMPC_flush:
10591   case OMPC_read:
10592   case OMPC_write:
10593   case OMPC_update:
10594   case OMPC_capture:
10595   case OMPC_seq_cst:
10596   case OMPC_depend:
10597   case OMPC_threads:
10598   case OMPC_simd:
10599   case OMPC_map:
10600   case OMPC_priority:
10601   case OMPC_grainsize:
10602   case OMPC_nogroup:
10603   case OMPC_num_tasks:
10604   case OMPC_hint:
10605   case OMPC_defaultmap:
10606   case OMPC_unknown:
10607   case OMPC_uniform:
10608   case OMPC_to:
10609   case OMPC_from:
10610   case OMPC_use_device_ptr:
10611   case OMPC_is_device_ptr:
10612   case OMPC_unified_address:
10613   case OMPC_unified_shared_memory:
10614   case OMPC_reverse_offload:
10615   case OMPC_dynamic_allocators:
10616   case OMPC_atomic_default_mem_order:
10617   case OMPC_device_type:
10618     llvm_unreachable("Unexpected OpenMP clause.");
10619   }
10620   return CaptureRegion;
10621 }
10622 
10623 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
10624                                      Expr *Condition, SourceLocation StartLoc,
10625                                      SourceLocation LParenLoc,
10626                                      SourceLocation NameModifierLoc,
10627                                      SourceLocation ColonLoc,
10628                                      SourceLocation EndLoc) {
10629   Expr *ValExpr = Condition;
10630   Stmt *HelperValStmt = nullptr;
10631   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
10632   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
10633       !Condition->isInstantiationDependent() &&
10634       !Condition->containsUnexpandedParameterPack()) {
10635     ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
10636     if (Val.isInvalid())
10637       return nullptr;
10638 
10639     ValExpr = Val.get();
10640 
10641     OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
10642     CaptureRegion =
10643         getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier);
10644     if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
10645       ValExpr = MakeFullExpr(ValExpr).get();
10646       llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
10647       ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
10648       HelperValStmt = buildPreInits(Context, Captures);
10649     }
10650   }
10651 
10652   return new (Context)
10653       OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
10654                   LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
10655 }
10656 
10657 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
10658                                         SourceLocation StartLoc,
10659                                         SourceLocation LParenLoc,
10660                                         SourceLocation EndLoc) {
10661   Expr *ValExpr = Condition;
10662   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
10663       !Condition->isInstantiationDependent() &&
10664       !Condition->containsUnexpandedParameterPack()) {
10665     ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
10666     if (Val.isInvalid())
10667       return nullptr;
10668 
10669     ValExpr = MakeFullExpr(Val.get()).get();
10670   }
10671 
10672   return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10673 }
10674 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
10675                                                         Expr *Op) {
10676   if (!Op)
10677     return ExprError();
10678 
10679   class IntConvertDiagnoser : public ICEConvertDiagnoser {
10680   public:
10681     IntConvertDiagnoser()
10682         : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
10683     SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
10684                                          QualType T) override {
10685       return S.Diag(Loc, diag::err_omp_not_integral) << T;
10686     }
10687     SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
10688                                              QualType T) override {
10689       return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
10690     }
10691     SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
10692                                                QualType T,
10693                                                QualType ConvTy) override {
10694       return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
10695     }
10696     SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
10697                                            QualType ConvTy) override {
10698       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
10699              << ConvTy->isEnumeralType() << ConvTy;
10700     }
10701     SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
10702                                             QualType T) override {
10703       return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
10704     }
10705     SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
10706                                         QualType ConvTy) override {
10707       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
10708              << ConvTy->isEnumeralType() << ConvTy;
10709     }
10710     SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
10711                                              QualType) override {
10712       llvm_unreachable("conversion functions are permitted");
10713     }
10714   } ConvertDiagnoser;
10715   return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
10716 }
10717 
10718 static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
10719                                       OpenMPClauseKind CKind,
10720                                       bool StrictlyPositive) {
10721   if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
10722       !ValExpr->isInstantiationDependent()) {
10723     SourceLocation Loc = ValExpr->getExprLoc();
10724     ExprResult Value =
10725         SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
10726     if (Value.isInvalid())
10727       return false;
10728 
10729     ValExpr = Value.get();
10730     // The expression must evaluate to a non-negative integer value.
10731     llvm::APSInt Result;
10732     if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
10733         Result.isSigned() &&
10734         !((!StrictlyPositive && Result.isNonNegative()) ||
10735           (StrictlyPositive && Result.isStrictlyPositive()))) {
10736       SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
10737           << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
10738           << ValExpr->getSourceRange();
10739       return false;
10740     }
10741   }
10742   return true;
10743 }
10744 
10745 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
10746                                              SourceLocation StartLoc,
10747                                              SourceLocation LParenLoc,
10748                                              SourceLocation EndLoc) {
10749   Expr *ValExpr = NumThreads;
10750   Stmt *HelperValStmt = nullptr;
10751 
10752   // OpenMP [2.5, Restrictions]
10753   //  The num_threads expression must evaluate to a positive integer value.
10754   if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
10755                                  /*StrictlyPositive=*/true))
10756     return nullptr;
10757 
10758   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
10759   OpenMPDirectiveKind CaptureRegion =
10760       getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads);
10761   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
10762     ValExpr = MakeFullExpr(ValExpr).get();
10763     llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
10764     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
10765     HelperValStmt = buildPreInits(Context, Captures);
10766   }
10767 
10768   return new (Context) OMPNumThreadsClause(
10769       ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
10770 }
10771 
10772 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
10773                                                        OpenMPClauseKind CKind,
10774                                                        bool StrictlyPositive) {
10775   if (!E)
10776     return ExprError();
10777   if (E->isValueDependent() || E->isTypeDependent() ||
10778       E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
10779     return E;
10780   llvm::APSInt Result;
10781   ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
10782   if (ICE.isInvalid())
10783     return ExprError();
10784   if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
10785       (!StrictlyPositive && !Result.isNonNegative())) {
10786     Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
10787         << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
10788         << E->getSourceRange();
10789     return ExprError();
10790   }
10791   if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
10792     Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
10793         << E->getSourceRange();
10794     return ExprError();
10795   }
10796   if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
10797     DSAStack->setAssociatedLoops(Result.getExtValue());
10798   else if (CKind == OMPC_ordered)
10799     DSAStack->setAssociatedLoops(Result.getExtValue());
10800   return ICE;
10801 }
10802 
10803 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
10804                                           SourceLocation LParenLoc,
10805                                           SourceLocation EndLoc) {
10806   // OpenMP [2.8.1, simd construct, Description]
10807   // The parameter of the safelen clause must be a constant
10808   // positive integer expression.
10809   ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
10810   if (Safelen.isInvalid())
10811     return nullptr;
10812   return new (Context)
10813       OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
10814 }
10815 
10816 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
10817                                           SourceLocation LParenLoc,
10818                                           SourceLocation EndLoc) {
10819   // OpenMP [2.8.1, simd construct, Description]
10820   // The parameter of the simdlen clause must be a constant
10821   // positive integer expression.
10822   ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
10823   if (Simdlen.isInvalid())
10824     return nullptr;
10825   return new (Context)
10826       OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
10827 }
10828 
10829 /// Tries to find omp_allocator_handle_t type.
10830 static bool findOMPAllocatorHandleT(Sema &S, SourceLocation Loc,
10831                                     DSAStackTy *Stack) {
10832   QualType OMPAllocatorHandleT = Stack->getOMPAllocatorHandleT();
10833   if (!OMPAllocatorHandleT.isNull())
10834     return true;
10835   // Build the predefined allocator expressions.
10836   bool ErrorFound = false;
10837   for (int I = OMPAllocateDeclAttr::OMPDefaultMemAlloc;
10838        I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
10839     auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
10840     StringRef Allocator =
10841         OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind);
10842     DeclarationName AllocatorName = &S.getASTContext().Idents.get(Allocator);
10843     auto *VD = dyn_cast_or_null<ValueDecl>(
10844         S.LookupSingleName(S.TUScope, AllocatorName, Loc, Sema::LookupAnyName));
10845     if (!VD) {
10846       ErrorFound = true;
10847       break;
10848     }
10849     QualType AllocatorType =
10850         VD->getType().getNonLValueExprType(S.getASTContext());
10851     ExprResult Res = S.BuildDeclRefExpr(VD, AllocatorType, VK_LValue, Loc);
10852     if (!Res.isUsable()) {
10853       ErrorFound = true;
10854       break;
10855     }
10856     if (OMPAllocatorHandleT.isNull())
10857       OMPAllocatorHandleT = AllocatorType;
10858     if (!S.getASTContext().hasSameType(OMPAllocatorHandleT, AllocatorType)) {
10859       ErrorFound = true;
10860       break;
10861     }
10862     Stack->setAllocator(AllocatorKind, Res.get());
10863   }
10864   if (ErrorFound) {
10865     S.Diag(Loc, diag::err_implied_omp_allocator_handle_t_not_found);
10866     return false;
10867   }
10868   OMPAllocatorHandleT.addConst();
10869   Stack->setOMPAllocatorHandleT(OMPAllocatorHandleT);
10870   return true;
10871 }
10872 
10873 OMPClause *Sema::ActOnOpenMPAllocatorClause(Expr *A, SourceLocation StartLoc,
10874                                             SourceLocation LParenLoc,
10875                                             SourceLocation EndLoc) {
10876   // OpenMP [2.11.3, allocate Directive, Description]
10877   // allocator is an expression of omp_allocator_handle_t type.
10878   if (!findOMPAllocatorHandleT(*this, A->getExprLoc(), DSAStack))
10879     return nullptr;
10880 
10881   ExprResult Allocator = DefaultLvalueConversion(A);
10882   if (Allocator.isInvalid())
10883     return nullptr;
10884   Allocator = PerformImplicitConversion(Allocator.get(),
10885                                         DSAStack->getOMPAllocatorHandleT(),
10886                                         Sema::AA_Initializing,
10887                                         /*AllowExplicit=*/true);
10888   if (Allocator.isInvalid())
10889     return nullptr;
10890   return new (Context)
10891       OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc);
10892 }
10893 
10894 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
10895                                            SourceLocation StartLoc,
10896                                            SourceLocation LParenLoc,
10897                                            SourceLocation EndLoc) {
10898   // OpenMP [2.7.1, loop construct, Description]
10899   // OpenMP [2.8.1, simd construct, Description]
10900   // OpenMP [2.9.6, distribute construct, Description]
10901   // The parameter of the collapse clause must be a constant
10902   // positive integer expression.
10903   ExprResult NumForLoopsResult =
10904       VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
10905   if (NumForLoopsResult.isInvalid())
10906     return nullptr;
10907   return new (Context)
10908       OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
10909 }
10910 
10911 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
10912                                           SourceLocation EndLoc,
10913                                           SourceLocation LParenLoc,
10914                                           Expr *NumForLoops) {
10915   // OpenMP [2.7.1, loop construct, Description]
10916   // OpenMP [2.8.1, simd construct, Description]
10917   // OpenMP [2.9.6, distribute construct, Description]
10918   // The parameter of the ordered clause must be a constant
10919   // positive integer expression if any.
10920   if (NumForLoops && LParenLoc.isValid()) {
10921     ExprResult NumForLoopsResult =
10922         VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
10923     if (NumForLoopsResult.isInvalid())
10924       return nullptr;
10925     NumForLoops = NumForLoopsResult.get();
10926   } else {
10927     NumForLoops = nullptr;
10928   }
10929   auto *Clause = OMPOrderedClause::Create(
10930       Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0,
10931       StartLoc, LParenLoc, EndLoc);
10932   DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause);
10933   return Clause;
10934 }
10935 
10936 OMPClause *Sema::ActOnOpenMPSimpleClause(
10937     OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
10938     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
10939   OMPClause *Res = nullptr;
10940   switch (Kind) {
10941   case OMPC_default:
10942     Res =
10943         ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
10944                                  ArgumentLoc, StartLoc, LParenLoc, EndLoc);
10945     break;
10946   case OMPC_proc_bind:
10947     Res = ActOnOpenMPProcBindClause(
10948         static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
10949         LParenLoc, EndLoc);
10950     break;
10951   case OMPC_atomic_default_mem_order:
10952     Res = ActOnOpenMPAtomicDefaultMemOrderClause(
10953         static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument),
10954         ArgumentLoc, StartLoc, LParenLoc, EndLoc);
10955     break;
10956   case OMPC_if:
10957   case OMPC_final:
10958   case OMPC_num_threads:
10959   case OMPC_safelen:
10960   case OMPC_simdlen:
10961   case OMPC_allocator:
10962   case OMPC_collapse:
10963   case OMPC_schedule:
10964   case OMPC_private:
10965   case OMPC_firstprivate:
10966   case OMPC_lastprivate:
10967   case OMPC_shared:
10968   case OMPC_reduction:
10969   case OMPC_task_reduction:
10970   case OMPC_in_reduction:
10971   case OMPC_linear:
10972   case OMPC_aligned:
10973   case OMPC_copyin:
10974   case OMPC_copyprivate:
10975   case OMPC_ordered:
10976   case OMPC_nowait:
10977   case OMPC_untied:
10978   case OMPC_mergeable:
10979   case OMPC_threadprivate:
10980   case OMPC_allocate:
10981   case OMPC_flush:
10982   case OMPC_read:
10983   case OMPC_write:
10984   case OMPC_update:
10985   case OMPC_capture:
10986   case OMPC_seq_cst:
10987   case OMPC_depend:
10988   case OMPC_device:
10989   case OMPC_threads:
10990   case OMPC_simd:
10991   case OMPC_map:
10992   case OMPC_num_teams:
10993   case OMPC_thread_limit:
10994   case OMPC_priority:
10995   case OMPC_grainsize:
10996   case OMPC_nogroup:
10997   case OMPC_num_tasks:
10998   case OMPC_hint:
10999   case OMPC_dist_schedule:
11000   case OMPC_defaultmap:
11001   case OMPC_unknown:
11002   case OMPC_uniform:
11003   case OMPC_to:
11004   case OMPC_from:
11005   case OMPC_use_device_ptr:
11006   case OMPC_is_device_ptr:
11007   case OMPC_unified_address:
11008   case OMPC_unified_shared_memory:
11009   case OMPC_reverse_offload:
11010   case OMPC_dynamic_allocators:
11011   case OMPC_device_type:
11012     llvm_unreachable("Clause is not allowed.");
11013   }
11014   return Res;
11015 }
11016 
11017 static std::string
11018 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
11019                         ArrayRef<unsigned> Exclude = llvm::None) {
11020   SmallString<256> Buffer;
11021   llvm::raw_svector_ostream Out(Buffer);
11022   unsigned Bound = Last >= 2 ? Last - 2 : 0;
11023   unsigned Skipped = Exclude.size();
11024   auto S = Exclude.begin(), E = Exclude.end();
11025   for (unsigned I = First; I < Last; ++I) {
11026     if (std::find(S, E, I) != E) {
11027       --Skipped;
11028       continue;
11029     }
11030     Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'";
11031     if (I == Bound - Skipped)
11032       Out << " or ";
11033     else if (I != Bound + 1 - Skipped)
11034       Out << ", ";
11035   }
11036   return Out.str();
11037 }
11038 
11039 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
11040                                           SourceLocation KindKwLoc,
11041                                           SourceLocation StartLoc,
11042                                           SourceLocation LParenLoc,
11043                                           SourceLocation EndLoc) {
11044   if (Kind == OMPC_DEFAULT_unknown) {
11045     static_assert(OMPC_DEFAULT_unknown > 0,
11046                   "OMPC_DEFAULT_unknown not greater than 0");
11047     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
11048         << getListOfPossibleValues(OMPC_default, /*First=*/0,
11049                                    /*Last=*/OMPC_DEFAULT_unknown)
11050         << getOpenMPClauseName(OMPC_default);
11051     return nullptr;
11052   }
11053   switch (Kind) {
11054   case OMPC_DEFAULT_none:
11055     DSAStack->setDefaultDSANone(KindKwLoc);
11056     break;
11057   case OMPC_DEFAULT_shared:
11058     DSAStack->setDefaultDSAShared(KindKwLoc);
11059     break;
11060   case OMPC_DEFAULT_unknown:
11061     llvm_unreachable("Clause kind is not allowed.");
11062     break;
11063   }
11064   return new (Context)
11065       OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
11066 }
11067 
11068 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
11069                                            SourceLocation KindKwLoc,
11070                                            SourceLocation StartLoc,
11071                                            SourceLocation LParenLoc,
11072                                            SourceLocation EndLoc) {
11073   if (Kind == OMPC_PROC_BIND_unknown) {
11074     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
11075         << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
11076                                    /*Last=*/OMPC_PROC_BIND_unknown)
11077         << getOpenMPClauseName(OMPC_proc_bind);
11078     return nullptr;
11079   }
11080   return new (Context)
11081       OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
11082 }
11083 
11084 OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause(
11085     OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc,
11086     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
11087   if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) {
11088     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
11089         << getListOfPossibleValues(
11090                OMPC_atomic_default_mem_order, /*First=*/0,
11091                /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown)
11092         << getOpenMPClauseName(OMPC_atomic_default_mem_order);
11093     return nullptr;
11094   }
11095   return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc,
11096                                                       LParenLoc, EndLoc);
11097 }
11098 
11099 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
11100     OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
11101     SourceLocation StartLoc, SourceLocation LParenLoc,
11102     ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
11103     SourceLocation EndLoc) {
11104   OMPClause *Res = nullptr;
11105   switch (Kind) {
11106   case OMPC_schedule:
11107     enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
11108     assert(Argument.size() == NumberOfElements &&
11109            ArgumentLoc.size() == NumberOfElements);
11110     Res = ActOnOpenMPScheduleClause(
11111         static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
11112         static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
11113         static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
11114         StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
11115         ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
11116     break;
11117   case OMPC_if:
11118     assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
11119     Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
11120                               Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
11121                               DelimLoc, EndLoc);
11122     break;
11123   case OMPC_dist_schedule:
11124     Res = ActOnOpenMPDistScheduleClause(
11125         static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
11126         StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
11127     break;
11128   case OMPC_defaultmap:
11129     enum { Modifier, DefaultmapKind };
11130     Res = ActOnOpenMPDefaultmapClause(
11131         static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
11132         static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
11133         StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
11134         EndLoc);
11135     break;
11136   case OMPC_final:
11137   case OMPC_num_threads:
11138   case OMPC_safelen:
11139   case OMPC_simdlen:
11140   case OMPC_allocator:
11141   case OMPC_collapse:
11142   case OMPC_default:
11143   case OMPC_proc_bind:
11144   case OMPC_private:
11145   case OMPC_firstprivate:
11146   case OMPC_lastprivate:
11147   case OMPC_shared:
11148   case OMPC_reduction:
11149   case OMPC_task_reduction:
11150   case OMPC_in_reduction:
11151   case OMPC_linear:
11152   case OMPC_aligned:
11153   case OMPC_copyin:
11154   case OMPC_copyprivate:
11155   case OMPC_ordered:
11156   case OMPC_nowait:
11157   case OMPC_untied:
11158   case OMPC_mergeable:
11159   case OMPC_threadprivate:
11160   case OMPC_allocate:
11161   case OMPC_flush:
11162   case OMPC_read:
11163   case OMPC_write:
11164   case OMPC_update:
11165   case OMPC_capture:
11166   case OMPC_seq_cst:
11167   case OMPC_depend:
11168   case OMPC_device:
11169   case OMPC_threads:
11170   case OMPC_simd:
11171   case OMPC_map:
11172   case OMPC_num_teams:
11173   case OMPC_thread_limit:
11174   case OMPC_priority:
11175   case OMPC_grainsize:
11176   case OMPC_nogroup:
11177   case OMPC_num_tasks:
11178   case OMPC_hint:
11179   case OMPC_unknown:
11180   case OMPC_uniform:
11181   case OMPC_to:
11182   case OMPC_from:
11183   case OMPC_use_device_ptr:
11184   case OMPC_is_device_ptr:
11185   case OMPC_unified_address:
11186   case OMPC_unified_shared_memory:
11187   case OMPC_reverse_offload:
11188   case OMPC_dynamic_allocators:
11189   case OMPC_atomic_default_mem_order:
11190   case OMPC_device_type:
11191     llvm_unreachable("Clause is not allowed.");
11192   }
11193   return Res;
11194 }
11195 
11196 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
11197                                    OpenMPScheduleClauseModifier M2,
11198                                    SourceLocation M1Loc, SourceLocation M2Loc) {
11199   if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
11200     SmallVector<unsigned, 2> Excluded;
11201     if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
11202       Excluded.push_back(M2);
11203     if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
11204       Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
11205     if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
11206       Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
11207     S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
11208         << getListOfPossibleValues(OMPC_schedule,
11209                                    /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
11210                                    /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
11211                                    Excluded)
11212         << getOpenMPClauseName(OMPC_schedule);
11213     return true;
11214   }
11215   return false;
11216 }
11217 
11218 OMPClause *Sema::ActOnOpenMPScheduleClause(
11219     OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
11220     OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
11221     SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
11222     SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
11223   if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
11224       checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
11225     return nullptr;
11226   // OpenMP, 2.7.1, Loop Construct, Restrictions
11227   // Either the monotonic modifier or the nonmonotonic modifier can be specified
11228   // but not both.
11229   if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
11230       (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
11231        M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
11232       (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
11233        M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
11234     Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
11235         << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
11236         << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
11237     return nullptr;
11238   }
11239   if (Kind == OMPC_SCHEDULE_unknown) {
11240     std::string Values;
11241     if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
11242       unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
11243       Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
11244                                        /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
11245                                        Exclude);
11246     } else {
11247       Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
11248                                        /*Last=*/OMPC_SCHEDULE_unknown);
11249     }
11250     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
11251         << Values << getOpenMPClauseName(OMPC_schedule);
11252     return nullptr;
11253   }
11254   // OpenMP, 2.7.1, Loop Construct, Restrictions
11255   // The nonmonotonic modifier can only be specified with schedule(dynamic) or
11256   // schedule(guided).
11257   if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
11258        M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
11259       Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
11260     Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
11261          diag::err_omp_schedule_nonmonotonic_static);
11262     return nullptr;
11263   }
11264   Expr *ValExpr = ChunkSize;
11265   Stmt *HelperValStmt = nullptr;
11266   if (ChunkSize) {
11267     if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
11268         !ChunkSize->isInstantiationDependent() &&
11269         !ChunkSize->containsUnexpandedParameterPack()) {
11270       SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
11271       ExprResult Val =
11272           PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
11273       if (Val.isInvalid())
11274         return nullptr;
11275 
11276       ValExpr = Val.get();
11277 
11278       // OpenMP [2.7.1, Restrictions]
11279       //  chunk_size must be a loop invariant integer expression with a positive
11280       //  value.
11281       llvm::APSInt Result;
11282       if (ValExpr->isIntegerConstantExpr(Result, Context)) {
11283         if (Result.isSigned() && !Result.isStrictlyPositive()) {
11284           Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
11285               << "schedule" << 1 << ChunkSize->getSourceRange();
11286           return nullptr;
11287         }
11288       } else if (getOpenMPCaptureRegionForClause(
11289                      DSAStack->getCurrentDirective(), OMPC_schedule) !=
11290                      OMPD_unknown &&
11291                  !CurContext->isDependentContext()) {
11292         ValExpr = MakeFullExpr(ValExpr).get();
11293         llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
11294         ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11295         HelperValStmt = buildPreInits(Context, Captures);
11296       }
11297     }
11298   }
11299 
11300   return new (Context)
11301       OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
11302                         ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
11303 }
11304 
11305 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
11306                                    SourceLocation StartLoc,
11307                                    SourceLocation EndLoc) {
11308   OMPClause *Res = nullptr;
11309   switch (Kind) {
11310   case OMPC_ordered:
11311     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
11312     break;
11313   case OMPC_nowait:
11314     Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
11315     break;
11316   case OMPC_untied:
11317     Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
11318     break;
11319   case OMPC_mergeable:
11320     Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
11321     break;
11322   case OMPC_read:
11323     Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
11324     break;
11325   case OMPC_write:
11326     Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
11327     break;
11328   case OMPC_update:
11329     Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
11330     break;
11331   case OMPC_capture:
11332     Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
11333     break;
11334   case OMPC_seq_cst:
11335     Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
11336     break;
11337   case OMPC_threads:
11338     Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
11339     break;
11340   case OMPC_simd:
11341     Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
11342     break;
11343   case OMPC_nogroup:
11344     Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
11345     break;
11346   case OMPC_unified_address:
11347     Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc);
11348     break;
11349   case OMPC_unified_shared_memory:
11350     Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
11351     break;
11352   case OMPC_reverse_offload:
11353     Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc);
11354     break;
11355   case OMPC_dynamic_allocators:
11356     Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc);
11357     break;
11358   case OMPC_if:
11359   case OMPC_final:
11360   case OMPC_num_threads:
11361   case OMPC_safelen:
11362   case OMPC_simdlen:
11363   case OMPC_allocator:
11364   case OMPC_collapse:
11365   case OMPC_schedule:
11366   case OMPC_private:
11367   case OMPC_firstprivate:
11368   case OMPC_lastprivate:
11369   case OMPC_shared:
11370   case OMPC_reduction:
11371   case OMPC_task_reduction:
11372   case OMPC_in_reduction:
11373   case OMPC_linear:
11374   case OMPC_aligned:
11375   case OMPC_copyin:
11376   case OMPC_copyprivate:
11377   case OMPC_default:
11378   case OMPC_proc_bind:
11379   case OMPC_threadprivate:
11380   case OMPC_allocate:
11381   case OMPC_flush:
11382   case OMPC_depend:
11383   case OMPC_device:
11384   case OMPC_map:
11385   case OMPC_num_teams:
11386   case OMPC_thread_limit:
11387   case OMPC_priority:
11388   case OMPC_grainsize:
11389   case OMPC_num_tasks:
11390   case OMPC_hint:
11391   case OMPC_dist_schedule:
11392   case OMPC_defaultmap:
11393   case OMPC_unknown:
11394   case OMPC_uniform:
11395   case OMPC_to:
11396   case OMPC_from:
11397   case OMPC_use_device_ptr:
11398   case OMPC_is_device_ptr:
11399   case OMPC_atomic_default_mem_order:
11400   case OMPC_device_type:
11401     llvm_unreachable("Clause is not allowed.");
11402   }
11403   return Res;
11404 }
11405 
11406 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
11407                                          SourceLocation EndLoc) {
11408   DSAStack->setNowaitRegion();
11409   return new (Context) OMPNowaitClause(StartLoc, EndLoc);
11410 }
11411 
11412 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
11413                                          SourceLocation EndLoc) {
11414   return new (Context) OMPUntiedClause(StartLoc, EndLoc);
11415 }
11416 
11417 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
11418                                             SourceLocation EndLoc) {
11419   return new (Context) OMPMergeableClause(StartLoc, EndLoc);
11420 }
11421 
11422 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
11423                                        SourceLocation EndLoc) {
11424   return new (Context) OMPReadClause(StartLoc, EndLoc);
11425 }
11426 
11427 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
11428                                         SourceLocation EndLoc) {
11429   return new (Context) OMPWriteClause(StartLoc, EndLoc);
11430 }
11431 
11432 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
11433                                          SourceLocation EndLoc) {
11434   return new (Context) OMPUpdateClause(StartLoc, EndLoc);
11435 }
11436 
11437 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
11438                                           SourceLocation EndLoc) {
11439   return new (Context) OMPCaptureClause(StartLoc, EndLoc);
11440 }
11441 
11442 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
11443                                          SourceLocation EndLoc) {
11444   return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
11445 }
11446 
11447 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
11448                                           SourceLocation EndLoc) {
11449   return new (Context) OMPThreadsClause(StartLoc, EndLoc);
11450 }
11451 
11452 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
11453                                        SourceLocation EndLoc) {
11454   return new (Context) OMPSIMDClause(StartLoc, EndLoc);
11455 }
11456 
11457 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
11458                                           SourceLocation EndLoc) {
11459   return new (Context) OMPNogroupClause(StartLoc, EndLoc);
11460 }
11461 
11462 OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc,
11463                                                  SourceLocation EndLoc) {
11464   return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc);
11465 }
11466 
11467 OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc,
11468                                                       SourceLocation EndLoc) {
11469   return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
11470 }
11471 
11472 OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc,
11473                                                  SourceLocation EndLoc) {
11474   return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc);
11475 }
11476 
11477 OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc,
11478                                                     SourceLocation EndLoc) {
11479   return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc);
11480 }
11481 
11482 OMPClause *Sema::ActOnOpenMPVarListClause(
11483     OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
11484     const OMPVarListLocTy &Locs, SourceLocation ColonLoc,
11485     CXXScopeSpec &ReductionOrMapperIdScopeSpec,
11486     DeclarationNameInfo &ReductionOrMapperId, OpenMPDependClauseKind DepKind,
11487     OpenMPLinearClauseKind LinKind,
11488     ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
11489     ArrayRef<SourceLocation> MapTypeModifiersLoc, OpenMPMapClauseKind MapType,
11490     bool IsMapTypeImplicit, SourceLocation DepLinMapLoc) {
11491   SourceLocation StartLoc = Locs.StartLoc;
11492   SourceLocation LParenLoc = Locs.LParenLoc;
11493   SourceLocation EndLoc = Locs.EndLoc;
11494   OMPClause *Res = nullptr;
11495   switch (Kind) {
11496   case OMPC_private:
11497     Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
11498     break;
11499   case OMPC_firstprivate:
11500     Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
11501     break;
11502   case OMPC_lastprivate:
11503     Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
11504     break;
11505   case OMPC_shared:
11506     Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
11507     break;
11508   case OMPC_reduction:
11509     Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
11510                                      EndLoc, ReductionOrMapperIdScopeSpec,
11511                                      ReductionOrMapperId);
11512     break;
11513   case OMPC_task_reduction:
11514     Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
11515                                          EndLoc, ReductionOrMapperIdScopeSpec,
11516                                          ReductionOrMapperId);
11517     break;
11518   case OMPC_in_reduction:
11519     Res = ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
11520                                        EndLoc, ReductionOrMapperIdScopeSpec,
11521                                        ReductionOrMapperId);
11522     break;
11523   case OMPC_linear:
11524     Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
11525                                   LinKind, DepLinMapLoc, ColonLoc, EndLoc);
11526     break;
11527   case OMPC_aligned:
11528     Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
11529                                    ColonLoc, EndLoc);
11530     break;
11531   case OMPC_copyin:
11532     Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
11533     break;
11534   case OMPC_copyprivate:
11535     Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
11536     break;
11537   case OMPC_flush:
11538     Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
11539     break;
11540   case OMPC_depend:
11541     Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
11542                                   StartLoc, LParenLoc, EndLoc);
11543     break;
11544   case OMPC_map:
11545     Res = ActOnOpenMPMapClause(MapTypeModifiers, MapTypeModifiersLoc,
11546                                ReductionOrMapperIdScopeSpec,
11547                                ReductionOrMapperId, MapType, IsMapTypeImplicit,
11548                                DepLinMapLoc, ColonLoc, VarList, Locs);
11549     break;
11550   case OMPC_to:
11551     Res = ActOnOpenMPToClause(VarList, ReductionOrMapperIdScopeSpec,
11552                               ReductionOrMapperId, Locs);
11553     break;
11554   case OMPC_from:
11555     Res = ActOnOpenMPFromClause(VarList, ReductionOrMapperIdScopeSpec,
11556                                 ReductionOrMapperId, Locs);
11557     break;
11558   case OMPC_use_device_ptr:
11559     Res = ActOnOpenMPUseDevicePtrClause(VarList, Locs);
11560     break;
11561   case OMPC_is_device_ptr:
11562     Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs);
11563     break;
11564   case OMPC_allocate:
11565     Res = ActOnOpenMPAllocateClause(TailExpr, VarList, StartLoc, LParenLoc,
11566                                     ColonLoc, EndLoc);
11567     break;
11568   case OMPC_if:
11569   case OMPC_final:
11570   case OMPC_num_threads:
11571   case OMPC_safelen:
11572   case OMPC_simdlen:
11573   case OMPC_allocator:
11574   case OMPC_collapse:
11575   case OMPC_default:
11576   case OMPC_proc_bind:
11577   case OMPC_schedule:
11578   case OMPC_ordered:
11579   case OMPC_nowait:
11580   case OMPC_untied:
11581   case OMPC_mergeable:
11582   case OMPC_threadprivate:
11583   case OMPC_read:
11584   case OMPC_write:
11585   case OMPC_update:
11586   case OMPC_capture:
11587   case OMPC_seq_cst:
11588   case OMPC_device:
11589   case OMPC_threads:
11590   case OMPC_simd:
11591   case OMPC_num_teams:
11592   case OMPC_thread_limit:
11593   case OMPC_priority:
11594   case OMPC_grainsize:
11595   case OMPC_nogroup:
11596   case OMPC_num_tasks:
11597   case OMPC_hint:
11598   case OMPC_dist_schedule:
11599   case OMPC_defaultmap:
11600   case OMPC_unknown:
11601   case OMPC_uniform:
11602   case OMPC_unified_address:
11603   case OMPC_unified_shared_memory:
11604   case OMPC_reverse_offload:
11605   case OMPC_dynamic_allocators:
11606   case OMPC_atomic_default_mem_order:
11607   case OMPC_device_type:
11608     llvm_unreachable("Clause is not allowed.");
11609   }
11610   return Res;
11611 }
11612 
11613 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
11614                                        ExprObjectKind OK, SourceLocation Loc) {
11615   ExprResult Res = BuildDeclRefExpr(
11616       Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
11617   if (!Res.isUsable())
11618     return ExprError();
11619   if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
11620     Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
11621     if (!Res.isUsable())
11622       return ExprError();
11623   }
11624   if (VK != VK_LValue && Res.get()->isGLValue()) {
11625     Res = DefaultLvalueConversion(Res.get());
11626     if (!Res.isUsable())
11627       return ExprError();
11628   }
11629   return Res;
11630 }
11631 
11632 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
11633                                           SourceLocation StartLoc,
11634                                           SourceLocation LParenLoc,
11635                                           SourceLocation EndLoc) {
11636   SmallVector<Expr *, 8> Vars;
11637   SmallVector<Expr *, 8> PrivateCopies;
11638   for (Expr *RefExpr : VarList) {
11639     assert(RefExpr && "NULL expr in OpenMP private clause.");
11640     SourceLocation ELoc;
11641     SourceRange ERange;
11642     Expr *SimpleRefExpr = RefExpr;
11643     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11644     if (Res.second) {
11645       // It will be analyzed later.
11646       Vars.push_back(RefExpr);
11647       PrivateCopies.push_back(nullptr);
11648     }
11649     ValueDecl *D = Res.first;
11650     if (!D)
11651       continue;
11652 
11653     QualType Type = D->getType();
11654     auto *VD = dyn_cast<VarDecl>(D);
11655 
11656     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
11657     //  A variable that appears in a private clause must not have an incomplete
11658     //  type or a reference type.
11659     if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
11660       continue;
11661     Type = Type.getNonReferenceType();
11662 
11663     // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
11664     // A variable that is privatized must not have a const-qualified type
11665     // unless it is of class type with a mutable member. This restriction does
11666     // not apply to the firstprivate clause.
11667     //
11668     // OpenMP 3.1 [2.9.3.3, private clause, Restrictions]
11669     // A variable that appears in a private clause must not have a
11670     // const-qualified type unless it is of class type with a mutable member.
11671     if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc))
11672       continue;
11673 
11674     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
11675     // in a Construct]
11676     //  Variables with the predetermined data-sharing attributes may not be
11677     //  listed in data-sharing attributes clauses, except for the cases
11678     //  listed below. For these exceptions only, listing a predetermined
11679     //  variable in a data-sharing attribute clause is allowed and overrides
11680     //  the variable's predetermined data-sharing attributes.
11681     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
11682     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
11683       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
11684                                           << getOpenMPClauseName(OMPC_private);
11685       reportOriginalDsa(*this, DSAStack, D, DVar);
11686       continue;
11687     }
11688 
11689     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
11690     // Variably modified types are not supported for tasks.
11691     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
11692         isOpenMPTaskingDirective(CurrDir)) {
11693       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
11694           << getOpenMPClauseName(OMPC_private) << Type
11695           << getOpenMPDirectiveName(CurrDir);
11696       bool IsDecl =
11697           !VD ||
11698           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11699       Diag(D->getLocation(),
11700            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11701           << D;
11702       continue;
11703     }
11704 
11705     // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
11706     // A list item cannot appear in both a map clause and a data-sharing
11707     // attribute clause on the same construct
11708     //
11709     // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
11710     // A list item cannot appear in both a map clause and a data-sharing
11711     // attribute clause on the same construct unless the construct is a
11712     // combined construct.
11713     if ((LangOpts.OpenMP <= 45 && isOpenMPTargetExecutionDirective(CurrDir)) ||
11714         CurrDir == OMPD_target) {
11715       OpenMPClauseKind ConflictKind;
11716       if (DSAStack->checkMappableExprComponentListsForDecl(
11717               VD, /*CurrentRegionOnly=*/true,
11718               [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
11719                   OpenMPClauseKind WhereFoundClauseKind) -> bool {
11720                 ConflictKind = WhereFoundClauseKind;
11721                 return true;
11722               })) {
11723         Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
11724             << getOpenMPClauseName(OMPC_private)
11725             << getOpenMPClauseName(ConflictKind)
11726             << getOpenMPDirectiveName(CurrDir);
11727         reportOriginalDsa(*this, DSAStack, D, DVar);
11728         continue;
11729       }
11730     }
11731 
11732     // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
11733     //  A variable of class type (or array thereof) that appears in a private
11734     //  clause requires an accessible, unambiguous default constructor for the
11735     //  class type.
11736     // Generate helper private variable and initialize it with the default
11737     // value. The address of the original variable is replaced by the address of
11738     // the new private variable in CodeGen. This new variable is not added to
11739     // IdResolver, so the code in the OpenMP region uses original variable for
11740     // proper diagnostics.
11741     Type = Type.getUnqualifiedType();
11742     VarDecl *VDPrivate =
11743         buildVarDecl(*this, ELoc, Type, D->getName(),
11744                      D->hasAttrs() ? &D->getAttrs() : nullptr,
11745                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
11746     ActOnUninitializedDecl(VDPrivate);
11747     if (VDPrivate->isInvalidDecl())
11748       continue;
11749     DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
11750         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
11751 
11752     DeclRefExpr *Ref = nullptr;
11753     if (!VD && !CurContext->isDependentContext())
11754       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
11755     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
11756     Vars.push_back((VD || CurContext->isDependentContext())
11757                        ? RefExpr->IgnoreParens()
11758                        : Ref);
11759     PrivateCopies.push_back(VDPrivateRefExpr);
11760   }
11761 
11762   if (Vars.empty())
11763     return nullptr;
11764 
11765   return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
11766                                   PrivateCopies);
11767 }
11768 
11769 namespace {
11770 class DiagsUninitializedSeveretyRAII {
11771 private:
11772   DiagnosticsEngine &Diags;
11773   SourceLocation SavedLoc;
11774   bool IsIgnored = false;
11775 
11776 public:
11777   DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
11778                                  bool IsIgnored)
11779       : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
11780     if (!IsIgnored) {
11781       Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
11782                         /*Map*/ diag::Severity::Ignored, Loc);
11783     }
11784   }
11785   ~DiagsUninitializedSeveretyRAII() {
11786     if (!IsIgnored)
11787       Diags.popMappings(SavedLoc);
11788   }
11789 };
11790 }
11791 
11792 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
11793                                                SourceLocation StartLoc,
11794                                                SourceLocation LParenLoc,
11795                                                SourceLocation EndLoc) {
11796   SmallVector<Expr *, 8> Vars;
11797   SmallVector<Expr *, 8> PrivateCopies;
11798   SmallVector<Expr *, 8> Inits;
11799   SmallVector<Decl *, 4> ExprCaptures;
11800   bool IsImplicitClause =
11801       StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
11802   SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
11803 
11804   for (Expr *RefExpr : VarList) {
11805     assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
11806     SourceLocation ELoc;
11807     SourceRange ERange;
11808     Expr *SimpleRefExpr = RefExpr;
11809     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11810     if (Res.second) {
11811       // It will be analyzed later.
11812       Vars.push_back(RefExpr);
11813       PrivateCopies.push_back(nullptr);
11814       Inits.push_back(nullptr);
11815     }
11816     ValueDecl *D = Res.first;
11817     if (!D)
11818       continue;
11819 
11820     ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
11821     QualType Type = D->getType();
11822     auto *VD = dyn_cast<VarDecl>(D);
11823 
11824     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
11825     //  A variable that appears in a private clause must not have an incomplete
11826     //  type or a reference type.
11827     if (RequireCompleteType(ELoc, Type,
11828                             diag::err_omp_firstprivate_incomplete_type))
11829       continue;
11830     Type = Type.getNonReferenceType();
11831 
11832     // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
11833     //  A variable of class type (or array thereof) that appears in a private
11834     //  clause requires an accessible, unambiguous copy constructor for the
11835     //  class type.
11836     QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
11837 
11838     // If an implicit firstprivate variable found it was checked already.
11839     DSAStackTy::DSAVarData TopDVar;
11840     if (!IsImplicitClause) {
11841       DSAStackTy::DSAVarData DVar =
11842           DSAStack->getTopDSA(D, /*FromParent=*/false);
11843       TopDVar = DVar;
11844       OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
11845       bool IsConstant = ElemType.isConstant(Context);
11846       // OpenMP [2.4.13, Data-sharing Attribute Clauses]
11847       //  A list item that specifies a given variable may not appear in more
11848       // than one clause on the same directive, except that a variable may be
11849       //  specified in both firstprivate and lastprivate clauses.
11850       // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
11851       // A list item may appear in a firstprivate or lastprivate clause but not
11852       // both.
11853       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
11854           (isOpenMPDistributeDirective(CurrDir) ||
11855            DVar.CKind != OMPC_lastprivate) &&
11856           DVar.RefExpr) {
11857         Diag(ELoc, diag::err_omp_wrong_dsa)
11858             << getOpenMPClauseName(DVar.CKind)
11859             << getOpenMPClauseName(OMPC_firstprivate);
11860         reportOriginalDsa(*this, DSAStack, D, DVar);
11861         continue;
11862       }
11863 
11864       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
11865       // in a Construct]
11866       //  Variables with the predetermined data-sharing attributes may not be
11867       //  listed in data-sharing attributes clauses, except for the cases
11868       //  listed below. For these exceptions only, listing a predetermined
11869       //  variable in a data-sharing attribute clause is allowed and overrides
11870       //  the variable's predetermined data-sharing attributes.
11871       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
11872       // in a Construct, C/C++, p.2]
11873       //  Variables with const-qualified type having no mutable member may be
11874       //  listed in a firstprivate clause, even if they are static data members.
11875       if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
11876           DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
11877         Diag(ELoc, diag::err_omp_wrong_dsa)
11878             << getOpenMPClauseName(DVar.CKind)
11879             << getOpenMPClauseName(OMPC_firstprivate);
11880         reportOriginalDsa(*this, DSAStack, D, DVar);
11881         continue;
11882       }
11883 
11884       // OpenMP [2.9.3.4, Restrictions, p.2]
11885       //  A list item that is private within a parallel region must not appear
11886       //  in a firstprivate clause on a worksharing construct if any of the
11887       //  worksharing regions arising from the worksharing construct ever bind
11888       //  to any of the parallel regions arising from the parallel construct.
11889       // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
11890       // A list item that is private within a teams region must not appear in a
11891       // firstprivate clause on a distribute construct if any of the distribute
11892       // regions arising from the distribute construct ever bind to any of the
11893       // teams regions arising from the teams construct.
11894       // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
11895       // A list item that appears in a reduction clause of a teams construct
11896       // must not appear in a firstprivate clause on a distribute construct if
11897       // any of the distribute regions arising from the distribute construct
11898       // ever bind to any of the teams regions arising from the teams construct.
11899       if ((isOpenMPWorksharingDirective(CurrDir) ||
11900            isOpenMPDistributeDirective(CurrDir)) &&
11901           !isOpenMPParallelDirective(CurrDir) &&
11902           !isOpenMPTeamsDirective(CurrDir)) {
11903         DVar = DSAStack->getImplicitDSA(D, true);
11904         if (DVar.CKind != OMPC_shared &&
11905             (isOpenMPParallelDirective(DVar.DKind) ||
11906              isOpenMPTeamsDirective(DVar.DKind) ||
11907              DVar.DKind == OMPD_unknown)) {
11908           Diag(ELoc, diag::err_omp_required_access)
11909               << getOpenMPClauseName(OMPC_firstprivate)
11910               << getOpenMPClauseName(OMPC_shared);
11911           reportOriginalDsa(*this, DSAStack, D, DVar);
11912           continue;
11913         }
11914       }
11915       // OpenMP [2.9.3.4, Restrictions, p.3]
11916       //  A list item that appears in a reduction clause of a parallel construct
11917       //  must not appear in a firstprivate clause on a worksharing or task
11918       //  construct if any of the worksharing or task regions arising from the
11919       //  worksharing or task construct ever bind to any of the parallel regions
11920       //  arising from the parallel construct.
11921       // OpenMP [2.9.3.4, Restrictions, p.4]
11922       //  A list item that appears in a reduction clause in worksharing
11923       //  construct must not appear in a firstprivate clause in a task construct
11924       //  encountered during execution of any of the worksharing regions arising
11925       //  from the worksharing construct.
11926       if (isOpenMPTaskingDirective(CurrDir)) {
11927         DVar = DSAStack->hasInnermostDSA(
11928             D, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
11929             [](OpenMPDirectiveKind K) {
11930               return isOpenMPParallelDirective(K) ||
11931                      isOpenMPWorksharingDirective(K) ||
11932                      isOpenMPTeamsDirective(K);
11933             },
11934             /*FromParent=*/true);
11935         if (DVar.CKind == OMPC_reduction &&
11936             (isOpenMPParallelDirective(DVar.DKind) ||
11937              isOpenMPWorksharingDirective(DVar.DKind) ||
11938              isOpenMPTeamsDirective(DVar.DKind))) {
11939           Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
11940               << getOpenMPDirectiveName(DVar.DKind);
11941           reportOriginalDsa(*this, DSAStack, D, DVar);
11942           continue;
11943         }
11944       }
11945 
11946       // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
11947       // A list item cannot appear in both a map clause and a data-sharing
11948       // attribute clause on the same construct
11949       //
11950       // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
11951       // A list item cannot appear in both a map clause and a data-sharing
11952       // attribute clause on the same construct unless the construct is a
11953       // combined construct.
11954       if ((LangOpts.OpenMP <= 45 &&
11955            isOpenMPTargetExecutionDirective(CurrDir)) ||
11956           CurrDir == OMPD_target) {
11957         OpenMPClauseKind ConflictKind;
11958         if (DSAStack->checkMappableExprComponentListsForDecl(
11959                 VD, /*CurrentRegionOnly=*/true,
11960                 [&ConflictKind](
11961                     OMPClauseMappableExprCommon::MappableExprComponentListRef,
11962                     OpenMPClauseKind WhereFoundClauseKind) {
11963                   ConflictKind = WhereFoundClauseKind;
11964                   return true;
11965                 })) {
11966           Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
11967               << getOpenMPClauseName(OMPC_firstprivate)
11968               << getOpenMPClauseName(ConflictKind)
11969               << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
11970           reportOriginalDsa(*this, DSAStack, D, DVar);
11971           continue;
11972         }
11973       }
11974     }
11975 
11976     // Variably modified types are not supported for tasks.
11977     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
11978         isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
11979       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
11980           << getOpenMPClauseName(OMPC_firstprivate) << Type
11981           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
11982       bool IsDecl =
11983           !VD ||
11984           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11985       Diag(D->getLocation(),
11986            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11987           << D;
11988       continue;
11989     }
11990 
11991     Type = Type.getUnqualifiedType();
11992     VarDecl *VDPrivate =
11993         buildVarDecl(*this, ELoc, Type, D->getName(),
11994                      D->hasAttrs() ? &D->getAttrs() : nullptr,
11995                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
11996     // Generate helper private variable and initialize it with the value of the
11997     // original variable. The address of the original variable is replaced by
11998     // the address of the new private variable in the CodeGen. This new variable
11999     // is not added to IdResolver, so the code in the OpenMP region uses
12000     // original variable for proper diagnostics and variable capturing.
12001     Expr *VDInitRefExpr = nullptr;
12002     // For arrays generate initializer for single element and replace it by the
12003     // original array element in CodeGen.
12004     if (Type->isArrayType()) {
12005       VarDecl *VDInit =
12006           buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
12007       VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
12008       Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get();
12009       ElemType = ElemType.getUnqualifiedType();
12010       VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
12011                                          ".firstprivate.temp");
12012       InitializedEntity Entity =
12013           InitializedEntity::InitializeVariable(VDInitTemp);
12014       InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
12015 
12016       InitializationSequence InitSeq(*this, Entity, Kind, Init);
12017       ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
12018       if (Result.isInvalid())
12019         VDPrivate->setInvalidDecl();
12020       else
12021         VDPrivate->setInit(Result.getAs<Expr>());
12022       // Remove temp variable declaration.
12023       Context.Deallocate(VDInitTemp);
12024     } else {
12025       VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
12026                                      ".firstprivate.temp");
12027       VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
12028                                        RefExpr->getExprLoc());
12029       AddInitializerToDecl(VDPrivate,
12030                            DefaultLvalueConversion(VDInitRefExpr).get(),
12031                            /*DirectInit=*/false);
12032     }
12033     if (VDPrivate->isInvalidDecl()) {
12034       if (IsImplicitClause) {
12035         Diag(RefExpr->getExprLoc(),
12036              diag::note_omp_task_predetermined_firstprivate_here);
12037       }
12038       continue;
12039     }
12040     CurContext->addDecl(VDPrivate);
12041     DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
12042         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
12043         RefExpr->getExprLoc());
12044     DeclRefExpr *Ref = nullptr;
12045     if (!VD && !CurContext->isDependentContext()) {
12046       if (TopDVar.CKind == OMPC_lastprivate) {
12047         Ref = TopDVar.PrivateCopy;
12048       } else {
12049         Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
12050         if (!isOpenMPCapturedDecl(D))
12051           ExprCaptures.push_back(Ref->getDecl());
12052       }
12053     }
12054     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
12055     Vars.push_back((VD || CurContext->isDependentContext())
12056                        ? RefExpr->IgnoreParens()
12057                        : Ref);
12058     PrivateCopies.push_back(VDPrivateRefExpr);
12059     Inits.push_back(VDInitRefExpr);
12060   }
12061 
12062   if (Vars.empty())
12063     return nullptr;
12064 
12065   return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12066                                        Vars, PrivateCopies, Inits,
12067                                        buildPreInits(Context, ExprCaptures));
12068 }
12069 
12070 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
12071                                               SourceLocation StartLoc,
12072                                               SourceLocation LParenLoc,
12073                                               SourceLocation EndLoc) {
12074   SmallVector<Expr *, 8> Vars;
12075   SmallVector<Expr *, 8> SrcExprs;
12076   SmallVector<Expr *, 8> DstExprs;
12077   SmallVector<Expr *, 8> AssignmentOps;
12078   SmallVector<Decl *, 4> ExprCaptures;
12079   SmallVector<Expr *, 4> ExprPostUpdates;
12080   for (Expr *RefExpr : VarList) {
12081     assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
12082     SourceLocation ELoc;
12083     SourceRange ERange;
12084     Expr *SimpleRefExpr = RefExpr;
12085     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
12086     if (Res.second) {
12087       // It will be analyzed later.
12088       Vars.push_back(RefExpr);
12089       SrcExprs.push_back(nullptr);
12090       DstExprs.push_back(nullptr);
12091       AssignmentOps.push_back(nullptr);
12092     }
12093     ValueDecl *D = Res.first;
12094     if (!D)
12095       continue;
12096 
12097     QualType Type = D->getType();
12098     auto *VD = dyn_cast<VarDecl>(D);
12099 
12100     // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
12101     //  A variable that appears in a lastprivate clause must not have an
12102     //  incomplete type or a reference type.
12103     if (RequireCompleteType(ELoc, Type,
12104                             diag::err_omp_lastprivate_incomplete_type))
12105       continue;
12106     Type = Type.getNonReferenceType();
12107 
12108     // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
12109     // A variable that is privatized must not have a const-qualified type
12110     // unless it is of class type with a mutable member. This restriction does
12111     // not apply to the firstprivate clause.
12112     //
12113     // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions]
12114     // A variable that appears in a lastprivate clause must not have a
12115     // const-qualified type unless it is of class type with a mutable member.
12116     if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc))
12117       continue;
12118 
12119     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
12120     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
12121     // in a Construct]
12122     //  Variables with the predetermined data-sharing attributes may not be
12123     //  listed in data-sharing attributes clauses, except for the cases
12124     //  listed below.
12125     // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
12126     // A list item may appear in a firstprivate or lastprivate clause but not
12127     // both.
12128     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
12129     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
12130         (isOpenMPDistributeDirective(CurrDir) ||
12131          DVar.CKind != OMPC_firstprivate) &&
12132         (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
12133       Diag(ELoc, diag::err_omp_wrong_dsa)
12134           << getOpenMPClauseName(DVar.CKind)
12135           << getOpenMPClauseName(OMPC_lastprivate);
12136       reportOriginalDsa(*this, DSAStack, D, DVar);
12137       continue;
12138     }
12139 
12140     // OpenMP [2.14.3.5, Restrictions, p.2]
12141     // A list item that is private within a parallel region, or that appears in
12142     // the reduction clause of a parallel construct, must not appear in a
12143     // lastprivate clause on a worksharing construct if any of the corresponding
12144     // worksharing regions ever binds to any of the corresponding parallel
12145     // regions.
12146     DSAStackTy::DSAVarData TopDVar = DVar;
12147     if (isOpenMPWorksharingDirective(CurrDir) &&
12148         !isOpenMPParallelDirective(CurrDir) &&
12149         !isOpenMPTeamsDirective(CurrDir)) {
12150       DVar = DSAStack->getImplicitDSA(D, true);
12151       if (DVar.CKind != OMPC_shared) {
12152         Diag(ELoc, diag::err_omp_required_access)
12153             << getOpenMPClauseName(OMPC_lastprivate)
12154             << getOpenMPClauseName(OMPC_shared);
12155         reportOriginalDsa(*this, DSAStack, D, DVar);
12156         continue;
12157       }
12158     }
12159 
12160     // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
12161     //  A variable of class type (or array thereof) that appears in a
12162     //  lastprivate clause requires an accessible, unambiguous default
12163     //  constructor for the class type, unless the list item is also specified
12164     //  in a firstprivate clause.
12165     //  A variable of class type (or array thereof) that appears in a
12166     //  lastprivate clause requires an accessible, unambiguous copy assignment
12167     //  operator for the class type.
12168     Type = Context.getBaseElementType(Type).getNonReferenceType();
12169     VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(),
12170                                   Type.getUnqualifiedType(), ".lastprivate.src",
12171                                   D->hasAttrs() ? &D->getAttrs() : nullptr);
12172     DeclRefExpr *PseudoSrcExpr =
12173         buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
12174     VarDecl *DstVD =
12175         buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
12176                      D->hasAttrs() ? &D->getAttrs() : nullptr);
12177     DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
12178     // For arrays generate assignment operation for single element and replace
12179     // it by the original array element in CodeGen.
12180     ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
12181                                          PseudoDstExpr, PseudoSrcExpr);
12182     if (AssignmentOp.isInvalid())
12183       continue;
12184     AssignmentOp =
12185         ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
12186     if (AssignmentOp.isInvalid())
12187       continue;
12188 
12189     DeclRefExpr *Ref = nullptr;
12190     if (!VD && !CurContext->isDependentContext()) {
12191       if (TopDVar.CKind == OMPC_firstprivate) {
12192         Ref = TopDVar.PrivateCopy;
12193       } else {
12194         Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
12195         if (!isOpenMPCapturedDecl(D))
12196           ExprCaptures.push_back(Ref->getDecl());
12197       }
12198       if (TopDVar.CKind == OMPC_firstprivate ||
12199           (!isOpenMPCapturedDecl(D) &&
12200            Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
12201         ExprResult RefRes = DefaultLvalueConversion(Ref);
12202         if (!RefRes.isUsable())
12203           continue;
12204         ExprResult PostUpdateRes =
12205             BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
12206                        RefRes.get());
12207         if (!PostUpdateRes.isUsable())
12208           continue;
12209         ExprPostUpdates.push_back(
12210             IgnoredValueConversions(PostUpdateRes.get()).get());
12211       }
12212     }
12213     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
12214     Vars.push_back((VD || CurContext->isDependentContext())
12215                        ? RefExpr->IgnoreParens()
12216                        : Ref);
12217     SrcExprs.push_back(PseudoSrcExpr);
12218     DstExprs.push_back(PseudoDstExpr);
12219     AssignmentOps.push_back(AssignmentOp.get());
12220   }
12221 
12222   if (Vars.empty())
12223     return nullptr;
12224 
12225   return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12226                                       Vars, SrcExprs, DstExprs, AssignmentOps,
12227                                       buildPreInits(Context, ExprCaptures),
12228                                       buildPostUpdate(*this, ExprPostUpdates));
12229 }
12230 
12231 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
12232                                          SourceLocation StartLoc,
12233                                          SourceLocation LParenLoc,
12234                                          SourceLocation EndLoc) {
12235   SmallVector<Expr *, 8> Vars;
12236   for (Expr *RefExpr : VarList) {
12237     assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
12238     SourceLocation ELoc;
12239     SourceRange ERange;
12240     Expr *SimpleRefExpr = RefExpr;
12241     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
12242     if (Res.second) {
12243       // It will be analyzed later.
12244       Vars.push_back(RefExpr);
12245     }
12246     ValueDecl *D = Res.first;
12247     if (!D)
12248       continue;
12249 
12250     auto *VD = dyn_cast<VarDecl>(D);
12251     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
12252     // in a Construct]
12253     //  Variables with the predetermined data-sharing attributes may not be
12254     //  listed in data-sharing attributes clauses, except for the cases
12255     //  listed below. For these exceptions only, listing a predetermined
12256     //  variable in a data-sharing attribute clause is allowed and overrides
12257     //  the variable's predetermined data-sharing attributes.
12258     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
12259     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
12260         DVar.RefExpr) {
12261       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
12262                                           << getOpenMPClauseName(OMPC_shared);
12263       reportOriginalDsa(*this, DSAStack, D, DVar);
12264       continue;
12265     }
12266 
12267     DeclRefExpr *Ref = nullptr;
12268     if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
12269       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
12270     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
12271     Vars.push_back((VD || !Ref || CurContext->isDependentContext())
12272                        ? RefExpr->IgnoreParens()
12273                        : Ref);
12274   }
12275 
12276   if (Vars.empty())
12277     return nullptr;
12278 
12279   return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
12280 }
12281 
12282 namespace {
12283 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
12284   DSAStackTy *Stack;
12285 
12286 public:
12287   bool VisitDeclRefExpr(DeclRefExpr *E) {
12288     if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
12289       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
12290       if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
12291         return false;
12292       if (DVar.CKind != OMPC_unknown)
12293         return true;
12294       DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
12295           VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; },
12296           /*FromParent=*/true);
12297       return DVarPrivate.CKind != OMPC_unknown;
12298     }
12299     return false;
12300   }
12301   bool VisitStmt(Stmt *S) {
12302     for (Stmt *Child : S->children()) {
12303       if (Child && Visit(Child))
12304         return true;
12305     }
12306     return false;
12307   }
12308   explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
12309 };
12310 } // namespace
12311 
12312 namespace {
12313 // Transform MemberExpression for specified FieldDecl of current class to
12314 // DeclRefExpr to specified OMPCapturedExprDecl.
12315 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
12316   typedef TreeTransform<TransformExprToCaptures> BaseTransform;
12317   ValueDecl *Field = nullptr;
12318   DeclRefExpr *CapturedExpr = nullptr;
12319 
12320 public:
12321   TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
12322       : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
12323 
12324   ExprResult TransformMemberExpr(MemberExpr *E) {
12325     if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
12326         E->getMemberDecl() == Field) {
12327       CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
12328       return CapturedExpr;
12329     }
12330     return BaseTransform::TransformMemberExpr(E);
12331   }
12332   DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
12333 };
12334 } // namespace
12335 
12336 template <typename T, typename U>
12337 static T filterLookupForUDReductionAndMapper(
12338     SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) {
12339   for (U &Set : Lookups) {
12340     for (auto *D : Set) {
12341       if (T Res = Gen(cast<ValueDecl>(D)))
12342         return Res;
12343     }
12344   }
12345   return T();
12346 }
12347 
12348 static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
12349   assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
12350 
12351   for (auto RD : D->redecls()) {
12352     // Don't bother with extra checks if we already know this one isn't visible.
12353     if (RD == D)
12354       continue;
12355 
12356     auto ND = cast<NamedDecl>(RD);
12357     if (LookupResult::isVisible(SemaRef, ND))
12358       return ND;
12359   }
12360 
12361   return nullptr;
12362 }
12363 
12364 static void
12365 argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id,
12366                         SourceLocation Loc, QualType Ty,
12367                         SmallVectorImpl<UnresolvedSet<8>> &Lookups) {
12368   // Find all of the associated namespaces and classes based on the
12369   // arguments we have.
12370   Sema::AssociatedNamespaceSet AssociatedNamespaces;
12371   Sema::AssociatedClassSet AssociatedClasses;
12372   OpaqueValueExpr OVE(Loc, Ty, VK_LValue);
12373   SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces,
12374                                              AssociatedClasses);
12375 
12376   // C++ [basic.lookup.argdep]p3:
12377   //   Let X be the lookup set produced by unqualified lookup (3.4.1)
12378   //   and let Y be the lookup set produced by argument dependent
12379   //   lookup (defined as follows). If X contains [...] then Y is
12380   //   empty. Otherwise Y is the set of declarations found in the
12381   //   namespaces associated with the argument types as described
12382   //   below. The set of declarations found by the lookup of the name
12383   //   is the union of X and Y.
12384   //
12385   // Here, we compute Y and add its members to the overloaded
12386   // candidate set.
12387   for (auto *NS : AssociatedNamespaces) {
12388     //   When considering an associated namespace, the lookup is the
12389     //   same as the lookup performed when the associated namespace is
12390     //   used as a qualifier (3.4.3.2) except that:
12391     //
12392     //     -- Any using-directives in the associated namespace are
12393     //        ignored.
12394     //
12395     //     -- Any namespace-scope friend functions declared in
12396     //        associated classes are visible within their respective
12397     //        namespaces even if they are not visible during an ordinary
12398     //        lookup (11.4).
12399     DeclContext::lookup_result R = NS->lookup(Id.getName());
12400     for (auto *D : R) {
12401       auto *Underlying = D;
12402       if (auto *USD = dyn_cast<UsingShadowDecl>(D))
12403         Underlying = USD->getTargetDecl();
12404 
12405       if (!isa<OMPDeclareReductionDecl>(Underlying) &&
12406           !isa<OMPDeclareMapperDecl>(Underlying))
12407         continue;
12408 
12409       if (!SemaRef.isVisible(D)) {
12410         D = findAcceptableDecl(SemaRef, D);
12411         if (!D)
12412           continue;
12413         if (auto *USD = dyn_cast<UsingShadowDecl>(D))
12414           Underlying = USD->getTargetDecl();
12415       }
12416       Lookups.emplace_back();
12417       Lookups.back().addDecl(Underlying);
12418     }
12419   }
12420 }
12421 
12422 static ExprResult
12423 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
12424                          Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
12425                          const DeclarationNameInfo &ReductionId, QualType Ty,
12426                          CXXCastPath &BasePath, Expr *UnresolvedReduction) {
12427   if (ReductionIdScopeSpec.isInvalid())
12428     return ExprError();
12429   SmallVector<UnresolvedSet<8>, 4> Lookups;
12430   if (S) {
12431     LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
12432     Lookup.suppressDiagnostics();
12433     while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
12434       NamedDecl *D = Lookup.getRepresentativeDecl();
12435       do {
12436         S = S->getParent();
12437       } while (S && !S->isDeclScope(D));
12438       if (S)
12439         S = S->getParent();
12440       Lookups.emplace_back();
12441       Lookups.back().append(Lookup.begin(), Lookup.end());
12442       Lookup.clear();
12443     }
12444   } else if (auto *ULE =
12445                  cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
12446     Lookups.push_back(UnresolvedSet<8>());
12447     Decl *PrevD = nullptr;
12448     for (NamedDecl *D : ULE->decls()) {
12449       if (D == PrevD)
12450         Lookups.push_back(UnresolvedSet<8>());
12451       else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(D))
12452         Lookups.back().addDecl(DRD);
12453       PrevD = D;
12454     }
12455   }
12456   if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
12457       Ty->isInstantiationDependentType() ||
12458       Ty->containsUnexpandedParameterPack() ||
12459       filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) {
12460         return !D->isInvalidDecl() &&
12461                (D->getType()->isDependentType() ||
12462                 D->getType()->isInstantiationDependentType() ||
12463                 D->getType()->containsUnexpandedParameterPack());
12464       })) {
12465     UnresolvedSet<8> ResSet;
12466     for (const UnresolvedSet<8> &Set : Lookups) {
12467       if (Set.empty())
12468         continue;
12469       ResSet.append(Set.begin(), Set.end());
12470       // The last item marks the end of all declarations at the specified scope.
12471       ResSet.addDecl(Set[Set.size() - 1]);
12472     }
12473     return UnresolvedLookupExpr::Create(
12474         SemaRef.Context, /*NamingClass=*/nullptr,
12475         ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
12476         /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
12477   }
12478   // Lookup inside the classes.
12479   // C++ [over.match.oper]p3:
12480   //   For a unary operator @ with an operand of a type whose
12481   //   cv-unqualified version is T1, and for a binary operator @ with
12482   //   a left operand of a type whose cv-unqualified version is T1 and
12483   //   a right operand of a type whose cv-unqualified version is T2,
12484   //   three sets of candidate functions, designated member
12485   //   candidates, non-member candidates and built-in candidates, are
12486   //   constructed as follows:
12487   //     -- If T1 is a complete class type or a class currently being
12488   //        defined, the set of member candidates is the result of the
12489   //        qualified lookup of T1::operator@ (13.3.1.1.1); otherwise,
12490   //        the set of member candidates is empty.
12491   LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
12492   Lookup.suppressDiagnostics();
12493   if (const auto *TyRec = Ty->getAs<RecordType>()) {
12494     // Complete the type if it can be completed.
12495     // If the type is neither complete nor being defined, bail out now.
12496     if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() ||
12497         TyRec->getDecl()->getDefinition()) {
12498       Lookup.clear();
12499       SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl());
12500       if (Lookup.empty()) {
12501         Lookups.emplace_back();
12502         Lookups.back().append(Lookup.begin(), Lookup.end());
12503       }
12504     }
12505   }
12506   // Perform ADL.
12507   if (SemaRef.getLangOpts().CPlusPlus)
12508     argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups);
12509   if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
12510           Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
12511             if (!D->isInvalidDecl() &&
12512                 SemaRef.Context.hasSameType(D->getType(), Ty))
12513               return D;
12514             return nullptr;
12515           }))
12516     return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(),
12517                                     VK_LValue, Loc);
12518   if (SemaRef.getLangOpts().CPlusPlus) {
12519     if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
12520             Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
12521               if (!D->isInvalidDecl() &&
12522                   SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
12523                   !Ty.isMoreQualifiedThan(D->getType()))
12524                 return D;
12525               return nullptr;
12526             })) {
12527       CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
12528                          /*DetectVirtual=*/false);
12529       if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
12530         if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
12531                 VD->getType().getUnqualifiedType()))) {
12532           if (SemaRef.CheckBaseClassAccess(
12533                   Loc, VD->getType(), Ty, Paths.front(),
12534                   /*DiagID=*/0) != Sema::AR_inaccessible) {
12535             SemaRef.BuildBasePathArray(Paths, BasePath);
12536             return SemaRef.BuildDeclRefExpr(
12537                 VD, VD->getType().getNonReferenceType(), VK_LValue, Loc);
12538           }
12539         }
12540       }
12541     }
12542   }
12543   if (ReductionIdScopeSpec.isSet()) {
12544     SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
12545     return ExprError();
12546   }
12547   return ExprEmpty();
12548 }
12549 
12550 namespace {
12551 /// Data for the reduction-based clauses.
12552 struct ReductionData {
12553   /// List of original reduction items.
12554   SmallVector<Expr *, 8> Vars;
12555   /// List of private copies of the reduction items.
12556   SmallVector<Expr *, 8> Privates;
12557   /// LHS expressions for the reduction_op expressions.
12558   SmallVector<Expr *, 8> LHSs;
12559   /// RHS expressions for the reduction_op expressions.
12560   SmallVector<Expr *, 8> RHSs;
12561   /// Reduction operation expression.
12562   SmallVector<Expr *, 8> ReductionOps;
12563   /// Taskgroup descriptors for the corresponding reduction items in
12564   /// in_reduction clauses.
12565   SmallVector<Expr *, 8> TaskgroupDescriptors;
12566   /// List of captures for clause.
12567   SmallVector<Decl *, 4> ExprCaptures;
12568   /// List of postupdate expressions.
12569   SmallVector<Expr *, 4> ExprPostUpdates;
12570   ReductionData() = delete;
12571   /// Reserves required memory for the reduction data.
12572   ReductionData(unsigned Size) {
12573     Vars.reserve(Size);
12574     Privates.reserve(Size);
12575     LHSs.reserve(Size);
12576     RHSs.reserve(Size);
12577     ReductionOps.reserve(Size);
12578     TaskgroupDescriptors.reserve(Size);
12579     ExprCaptures.reserve(Size);
12580     ExprPostUpdates.reserve(Size);
12581   }
12582   /// Stores reduction item and reduction operation only (required for dependent
12583   /// reduction item).
12584   void push(Expr *Item, Expr *ReductionOp) {
12585     Vars.emplace_back(Item);
12586     Privates.emplace_back(nullptr);
12587     LHSs.emplace_back(nullptr);
12588     RHSs.emplace_back(nullptr);
12589     ReductionOps.emplace_back(ReductionOp);
12590     TaskgroupDescriptors.emplace_back(nullptr);
12591   }
12592   /// Stores reduction data.
12593   void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
12594             Expr *TaskgroupDescriptor) {
12595     Vars.emplace_back(Item);
12596     Privates.emplace_back(Private);
12597     LHSs.emplace_back(LHS);
12598     RHSs.emplace_back(RHS);
12599     ReductionOps.emplace_back(ReductionOp);
12600     TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
12601   }
12602 };
12603 } // namespace
12604 
12605 static bool checkOMPArraySectionConstantForReduction(
12606     ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
12607     SmallVectorImpl<llvm::APSInt> &ArraySizes) {
12608   const Expr *Length = OASE->getLength();
12609   if (Length == nullptr) {
12610     // For array sections of the form [1:] or [:], we would need to analyze
12611     // the lower bound...
12612     if (OASE->getColonLoc().isValid())
12613       return false;
12614 
12615     // This is an array subscript which has implicit length 1!
12616     SingleElement = true;
12617     ArraySizes.push_back(llvm::APSInt::get(1));
12618   } else {
12619     Expr::EvalResult Result;
12620     if (!Length->EvaluateAsInt(Result, Context))
12621       return false;
12622 
12623     llvm::APSInt ConstantLengthValue = Result.Val.getInt();
12624     SingleElement = (ConstantLengthValue.getSExtValue() == 1);
12625     ArraySizes.push_back(ConstantLengthValue);
12626   }
12627 
12628   // Get the base of this array section and walk up from there.
12629   const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
12630 
12631   // We require length = 1 for all array sections except the right-most to
12632   // guarantee that the memory region is contiguous and has no holes in it.
12633   while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
12634     Length = TempOASE->getLength();
12635     if (Length == nullptr) {
12636       // For array sections of the form [1:] or [:], we would need to analyze
12637       // the lower bound...
12638       if (OASE->getColonLoc().isValid())
12639         return false;
12640 
12641       // This is an array subscript which has implicit length 1!
12642       ArraySizes.push_back(llvm::APSInt::get(1));
12643     } else {
12644       Expr::EvalResult Result;
12645       if (!Length->EvaluateAsInt(Result, Context))
12646         return false;
12647 
12648       llvm::APSInt ConstantLengthValue = Result.Val.getInt();
12649       if (ConstantLengthValue.getSExtValue() != 1)
12650         return false;
12651 
12652       ArraySizes.push_back(ConstantLengthValue);
12653     }
12654     Base = TempOASE->getBase()->IgnoreParenImpCasts();
12655   }
12656 
12657   // If we have a single element, we don't need to add the implicit lengths.
12658   if (!SingleElement) {
12659     while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
12660       // Has implicit length 1!
12661       ArraySizes.push_back(llvm::APSInt::get(1));
12662       Base = TempASE->getBase()->IgnoreParenImpCasts();
12663     }
12664   }
12665 
12666   // This array section can be privatized as a single value or as a constant
12667   // sized array.
12668   return true;
12669 }
12670 
12671 static bool actOnOMPReductionKindClause(
12672     Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
12673     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
12674     SourceLocation ColonLoc, SourceLocation EndLoc,
12675     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
12676     ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
12677   DeclarationName DN = ReductionId.getName();
12678   OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
12679   BinaryOperatorKind BOK = BO_Comma;
12680 
12681   ASTContext &Context = S.Context;
12682   // OpenMP [2.14.3.6, reduction clause]
12683   // C
12684   // reduction-identifier is either an identifier or one of the following
12685   // operators: +, -, *,  &, |, ^, && and ||
12686   // C++
12687   // reduction-identifier is either an id-expression or one of the following
12688   // operators: +, -, *, &, |, ^, && and ||
12689   switch (OOK) {
12690   case OO_Plus:
12691   case OO_Minus:
12692     BOK = BO_Add;
12693     break;
12694   case OO_Star:
12695     BOK = BO_Mul;
12696     break;
12697   case OO_Amp:
12698     BOK = BO_And;
12699     break;
12700   case OO_Pipe:
12701     BOK = BO_Or;
12702     break;
12703   case OO_Caret:
12704     BOK = BO_Xor;
12705     break;
12706   case OO_AmpAmp:
12707     BOK = BO_LAnd;
12708     break;
12709   case OO_PipePipe:
12710     BOK = BO_LOr;
12711     break;
12712   case OO_New:
12713   case OO_Delete:
12714   case OO_Array_New:
12715   case OO_Array_Delete:
12716   case OO_Slash:
12717   case OO_Percent:
12718   case OO_Tilde:
12719   case OO_Exclaim:
12720   case OO_Equal:
12721   case OO_Less:
12722   case OO_Greater:
12723   case OO_LessEqual:
12724   case OO_GreaterEqual:
12725   case OO_PlusEqual:
12726   case OO_MinusEqual:
12727   case OO_StarEqual:
12728   case OO_SlashEqual:
12729   case OO_PercentEqual:
12730   case OO_CaretEqual:
12731   case OO_AmpEqual:
12732   case OO_PipeEqual:
12733   case OO_LessLess:
12734   case OO_GreaterGreater:
12735   case OO_LessLessEqual:
12736   case OO_GreaterGreaterEqual:
12737   case OO_EqualEqual:
12738   case OO_ExclaimEqual:
12739   case OO_Spaceship:
12740   case OO_PlusPlus:
12741   case OO_MinusMinus:
12742   case OO_Comma:
12743   case OO_ArrowStar:
12744   case OO_Arrow:
12745   case OO_Call:
12746   case OO_Subscript:
12747   case OO_Conditional:
12748   case OO_Coawait:
12749   case NUM_OVERLOADED_OPERATORS:
12750     llvm_unreachable("Unexpected reduction identifier");
12751   case OO_None:
12752     if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
12753       if (II->isStr("max"))
12754         BOK = BO_GT;
12755       else if (II->isStr("min"))
12756         BOK = BO_LT;
12757     }
12758     break;
12759   }
12760   SourceRange ReductionIdRange;
12761   if (ReductionIdScopeSpec.isValid())
12762     ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
12763   else
12764     ReductionIdRange.setBegin(ReductionId.getBeginLoc());
12765   ReductionIdRange.setEnd(ReductionId.getEndLoc());
12766 
12767   auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
12768   bool FirstIter = true;
12769   for (Expr *RefExpr : VarList) {
12770     assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
12771     // OpenMP [2.1, C/C++]
12772     //  A list item is a variable or array section, subject to the restrictions
12773     //  specified in Section 2.4 on page 42 and in each of the sections
12774     // describing clauses and directives for which a list appears.
12775     // OpenMP  [2.14.3.3, Restrictions, p.1]
12776     //  A variable that is part of another variable (as an array or
12777     //  structure element) cannot appear in a private clause.
12778     if (!FirstIter && IR != ER)
12779       ++IR;
12780     FirstIter = false;
12781     SourceLocation ELoc;
12782     SourceRange ERange;
12783     Expr *SimpleRefExpr = RefExpr;
12784     auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
12785                               /*AllowArraySection=*/true);
12786     if (Res.second) {
12787       // Try to find 'declare reduction' corresponding construct before using
12788       // builtin/overloaded operators.
12789       QualType Type = Context.DependentTy;
12790       CXXCastPath BasePath;
12791       ExprResult DeclareReductionRef = buildDeclareReductionRef(
12792           S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
12793           ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
12794       Expr *ReductionOp = nullptr;
12795       if (S.CurContext->isDependentContext() &&
12796           (DeclareReductionRef.isUnset() ||
12797            isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
12798         ReductionOp = DeclareReductionRef.get();
12799       // It will be analyzed later.
12800       RD.push(RefExpr, ReductionOp);
12801     }
12802     ValueDecl *D = Res.first;
12803     if (!D)
12804       continue;
12805 
12806     Expr *TaskgroupDescriptor = nullptr;
12807     QualType Type;
12808     auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
12809     auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
12810     if (ASE) {
12811       Type = ASE->getType().getNonReferenceType();
12812     } else if (OASE) {
12813       QualType BaseType =
12814           OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
12815       if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
12816         Type = ATy->getElementType();
12817       else
12818         Type = BaseType->getPointeeType();
12819       Type = Type.getNonReferenceType();
12820     } else {
12821       Type = Context.getBaseElementType(D->getType().getNonReferenceType());
12822     }
12823     auto *VD = dyn_cast<VarDecl>(D);
12824 
12825     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
12826     //  A variable that appears in a private clause must not have an incomplete
12827     //  type or a reference type.
12828     if (S.RequireCompleteType(ELoc, D->getType(),
12829                               diag::err_omp_reduction_incomplete_type))
12830       continue;
12831     // OpenMP [2.14.3.6, reduction clause, Restrictions]
12832     // A list item that appears in a reduction clause must not be
12833     // const-qualified.
12834     if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc,
12835                                   /*AcceptIfMutable*/ false, ASE || OASE))
12836       continue;
12837 
12838     OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
12839     // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
12840     //  If a list-item is a reference type then it must bind to the same object
12841     //  for all threads of the team.
12842     if (!ASE && !OASE) {
12843       if (VD) {
12844         VarDecl *VDDef = VD->getDefinition();
12845         if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
12846           DSARefChecker Check(Stack);
12847           if (Check.Visit(VDDef->getInit())) {
12848             S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
12849                 << getOpenMPClauseName(ClauseKind) << ERange;
12850             S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
12851             continue;
12852           }
12853         }
12854       }
12855 
12856       // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
12857       // in a Construct]
12858       //  Variables with the predetermined data-sharing attributes may not be
12859       //  listed in data-sharing attributes clauses, except for the cases
12860       //  listed below. For these exceptions only, listing a predetermined
12861       //  variable in a data-sharing attribute clause is allowed and overrides
12862       //  the variable's predetermined data-sharing attributes.
12863       // OpenMP [2.14.3.6, Restrictions, p.3]
12864       //  Any number of reduction clauses can be specified on the directive,
12865       //  but a list item can appear only once in the reduction clauses for that
12866       //  directive.
12867       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
12868       if (DVar.CKind == OMPC_reduction) {
12869         S.Diag(ELoc, diag::err_omp_once_referenced)
12870             << getOpenMPClauseName(ClauseKind);
12871         if (DVar.RefExpr)
12872           S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
12873         continue;
12874       }
12875       if (DVar.CKind != OMPC_unknown) {
12876         S.Diag(ELoc, diag::err_omp_wrong_dsa)
12877             << getOpenMPClauseName(DVar.CKind)
12878             << getOpenMPClauseName(OMPC_reduction);
12879         reportOriginalDsa(S, Stack, D, DVar);
12880         continue;
12881       }
12882 
12883       // OpenMP [2.14.3.6, Restrictions, p.1]
12884       //  A list item that appears in a reduction clause of a worksharing
12885       //  construct must be shared in the parallel regions to which any of the
12886       //  worksharing regions arising from the worksharing construct bind.
12887       if (isOpenMPWorksharingDirective(CurrDir) &&
12888           !isOpenMPParallelDirective(CurrDir) &&
12889           !isOpenMPTeamsDirective(CurrDir)) {
12890         DVar = Stack->getImplicitDSA(D, true);
12891         if (DVar.CKind != OMPC_shared) {
12892           S.Diag(ELoc, diag::err_omp_required_access)
12893               << getOpenMPClauseName(OMPC_reduction)
12894               << getOpenMPClauseName(OMPC_shared);
12895           reportOriginalDsa(S, Stack, D, DVar);
12896           continue;
12897         }
12898       }
12899     }
12900 
12901     // Try to find 'declare reduction' corresponding construct before using
12902     // builtin/overloaded operators.
12903     CXXCastPath BasePath;
12904     ExprResult DeclareReductionRef = buildDeclareReductionRef(
12905         S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
12906         ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
12907     if (DeclareReductionRef.isInvalid())
12908       continue;
12909     if (S.CurContext->isDependentContext() &&
12910         (DeclareReductionRef.isUnset() ||
12911          isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
12912       RD.push(RefExpr, DeclareReductionRef.get());
12913       continue;
12914     }
12915     if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
12916       // Not allowed reduction identifier is found.
12917       S.Diag(ReductionId.getBeginLoc(),
12918              diag::err_omp_unknown_reduction_identifier)
12919           << Type << ReductionIdRange;
12920       continue;
12921     }
12922 
12923     // OpenMP [2.14.3.6, reduction clause, Restrictions]
12924     // The type of a list item that appears in a reduction clause must be valid
12925     // for the reduction-identifier. For a max or min reduction in C, the type
12926     // of the list item must be an allowed arithmetic data type: char, int,
12927     // float, double, or _Bool, possibly modified with long, short, signed, or
12928     // unsigned. For a max or min reduction in C++, the type of the list item
12929     // must be an allowed arithmetic data type: char, wchar_t, int, float,
12930     // double, or bool, possibly modified with long, short, signed, or unsigned.
12931     if (DeclareReductionRef.isUnset()) {
12932       if ((BOK == BO_GT || BOK == BO_LT) &&
12933           !(Type->isScalarType() ||
12934             (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
12935         S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
12936             << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
12937         if (!ASE && !OASE) {
12938           bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
12939                                    VarDecl::DeclarationOnly;
12940           S.Diag(D->getLocation(),
12941                  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
12942               << D;
12943         }
12944         continue;
12945       }
12946       if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
12947           !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
12948         S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
12949             << getOpenMPClauseName(ClauseKind);
12950         if (!ASE && !OASE) {
12951           bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
12952                                    VarDecl::DeclarationOnly;
12953           S.Diag(D->getLocation(),
12954                  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
12955               << D;
12956         }
12957         continue;
12958       }
12959     }
12960 
12961     Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
12962     VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
12963                                   D->hasAttrs() ? &D->getAttrs() : nullptr);
12964     VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
12965                                   D->hasAttrs() ? &D->getAttrs() : nullptr);
12966     QualType PrivateTy = Type;
12967 
12968     // Try if we can determine constant lengths for all array sections and avoid
12969     // the VLA.
12970     bool ConstantLengthOASE = false;
12971     if (OASE) {
12972       bool SingleElement;
12973       llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
12974       ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
12975           Context, OASE, SingleElement, ArraySizes);
12976 
12977       // If we don't have a single element, we must emit a constant array type.
12978       if (ConstantLengthOASE && !SingleElement) {
12979         for (llvm::APSInt &Size : ArraySizes)
12980           PrivateTy = Context.getConstantArrayType(
12981               PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0);
12982       }
12983     }
12984 
12985     if ((OASE && !ConstantLengthOASE) ||
12986         (!OASE && !ASE &&
12987          D->getType().getNonReferenceType()->isVariablyModifiedType())) {
12988       if (!Context.getTargetInfo().isVLASupported()) {
12989         if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective())) {
12990           S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
12991           S.Diag(ELoc, diag::note_vla_unsupported);
12992         } else {
12993           S.targetDiag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
12994           S.targetDiag(ELoc, diag::note_vla_unsupported);
12995         }
12996         continue;
12997       }
12998       // For arrays/array sections only:
12999       // Create pseudo array type for private copy. The size for this array will
13000       // be generated during codegen.
13001       // For array subscripts or single variables Private Ty is the same as Type
13002       // (type of the variable or single array element).
13003       PrivateTy = Context.getVariableArrayType(
13004           Type,
13005           new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue),
13006           ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
13007     } else if (!ASE && !OASE &&
13008                Context.getAsArrayType(D->getType().getNonReferenceType())) {
13009       PrivateTy = D->getType().getNonReferenceType();
13010     }
13011     // Private copy.
13012     VarDecl *PrivateVD =
13013         buildVarDecl(S, ELoc, PrivateTy, D->getName(),
13014                      D->hasAttrs() ? &D->getAttrs() : nullptr,
13015                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
13016     // Add initializer for private variable.
13017     Expr *Init = nullptr;
13018     DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
13019     DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
13020     if (DeclareReductionRef.isUsable()) {
13021       auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
13022       auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
13023       if (DRD->getInitializer()) {
13024         Init = DRDRef;
13025         RHSVD->setInit(DRDRef);
13026         RHSVD->setInitStyle(VarDecl::CallInit);
13027       }
13028     } else {
13029       switch (BOK) {
13030       case BO_Add:
13031       case BO_Xor:
13032       case BO_Or:
13033       case BO_LOr:
13034         // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
13035         if (Type->isScalarType() || Type->isAnyComplexType())
13036           Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
13037         break;
13038       case BO_Mul:
13039       case BO_LAnd:
13040         if (Type->isScalarType() || Type->isAnyComplexType()) {
13041           // '*' and '&&' reduction ops - initializer is '1'.
13042           Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
13043         }
13044         break;
13045       case BO_And: {
13046         // '&' reduction op - initializer is '~0'.
13047         QualType OrigType = Type;
13048         if (auto *ComplexTy = OrigType->getAs<ComplexType>())
13049           Type = ComplexTy->getElementType();
13050         if (Type->isRealFloatingType()) {
13051           llvm::APFloat InitValue =
13052               llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
13053                                              /*isIEEE=*/true);
13054           Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
13055                                          Type, ELoc);
13056         } else if (Type->isScalarType()) {
13057           uint64_t Size = Context.getTypeSize(Type);
13058           QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
13059           llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
13060           Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
13061         }
13062         if (Init && OrigType->isAnyComplexType()) {
13063           // Init = 0xFFFF + 0xFFFFi;
13064           auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
13065           Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
13066         }
13067         Type = OrigType;
13068         break;
13069       }
13070       case BO_LT:
13071       case BO_GT: {
13072         // 'min' reduction op - initializer is 'Largest representable number in
13073         // the reduction list item type'.
13074         // 'max' reduction op - initializer is 'Least representable number in
13075         // the reduction list item type'.
13076         if (Type->isIntegerType() || Type->isPointerType()) {
13077           bool IsSigned = Type->hasSignedIntegerRepresentation();
13078           uint64_t Size = Context.getTypeSize(Type);
13079           QualType IntTy =
13080               Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
13081           llvm::APInt InitValue =
13082               (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
13083                                         : llvm::APInt::getMinValue(Size)
13084                              : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
13085                                         : llvm::APInt::getMaxValue(Size);
13086           Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
13087           if (Type->isPointerType()) {
13088             // Cast to pointer type.
13089             ExprResult CastExpr = S.BuildCStyleCastExpr(
13090                 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
13091             if (CastExpr.isInvalid())
13092               continue;
13093             Init = CastExpr.get();
13094           }
13095         } else if (Type->isRealFloatingType()) {
13096           llvm::APFloat InitValue = llvm::APFloat::getLargest(
13097               Context.getFloatTypeSemantics(Type), BOK != BO_LT);
13098           Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
13099                                          Type, ELoc);
13100         }
13101         break;
13102       }
13103       case BO_PtrMemD:
13104       case BO_PtrMemI:
13105       case BO_MulAssign:
13106       case BO_Div:
13107       case BO_Rem:
13108       case BO_Sub:
13109       case BO_Shl:
13110       case BO_Shr:
13111       case BO_LE:
13112       case BO_GE:
13113       case BO_EQ:
13114       case BO_NE:
13115       case BO_Cmp:
13116       case BO_AndAssign:
13117       case BO_XorAssign:
13118       case BO_OrAssign:
13119       case BO_Assign:
13120       case BO_AddAssign:
13121       case BO_SubAssign:
13122       case BO_DivAssign:
13123       case BO_RemAssign:
13124       case BO_ShlAssign:
13125       case BO_ShrAssign:
13126       case BO_Comma:
13127         llvm_unreachable("Unexpected reduction operation");
13128       }
13129     }
13130     if (Init && DeclareReductionRef.isUnset())
13131       S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
13132     else if (!Init)
13133       S.ActOnUninitializedDecl(RHSVD);
13134     if (RHSVD->isInvalidDecl())
13135       continue;
13136     if (!RHSVD->hasInit() &&
13137         (DeclareReductionRef.isUnset() || !S.LangOpts.CPlusPlus)) {
13138       S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
13139           << Type << ReductionIdRange;
13140       bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
13141                                VarDecl::DeclarationOnly;
13142       S.Diag(D->getLocation(),
13143              IsDecl ? diag::note_previous_decl : diag::note_defined_here)
13144           << D;
13145       continue;
13146     }
13147     // Store initializer for single element in private copy. Will be used during
13148     // codegen.
13149     PrivateVD->setInit(RHSVD->getInit());
13150     PrivateVD->setInitStyle(RHSVD->getInitStyle());
13151     DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
13152     ExprResult ReductionOp;
13153     if (DeclareReductionRef.isUsable()) {
13154       QualType RedTy = DeclareReductionRef.get()->getType();
13155       QualType PtrRedTy = Context.getPointerType(RedTy);
13156       ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
13157       ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
13158       if (!BasePath.empty()) {
13159         LHS = S.DefaultLvalueConversion(LHS.get());
13160         RHS = S.DefaultLvalueConversion(RHS.get());
13161         LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
13162                                        CK_UncheckedDerivedToBase, LHS.get(),
13163                                        &BasePath, LHS.get()->getValueKind());
13164         RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
13165                                        CK_UncheckedDerivedToBase, RHS.get(),
13166                                        &BasePath, RHS.get()->getValueKind());
13167       }
13168       FunctionProtoType::ExtProtoInfo EPI;
13169       QualType Params[] = {PtrRedTy, PtrRedTy};
13170       QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
13171       auto *OVE = new (Context) OpaqueValueExpr(
13172           ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
13173           S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
13174       Expr *Args[] = {LHS.get(), RHS.get()};
13175       ReductionOp =
13176           CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
13177     } else {
13178       ReductionOp = S.BuildBinOp(
13179           Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE);
13180       if (ReductionOp.isUsable()) {
13181         if (BOK != BO_LT && BOK != BO_GT) {
13182           ReductionOp =
13183               S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
13184                            BO_Assign, LHSDRE, ReductionOp.get());
13185         } else {
13186           auto *ConditionalOp = new (Context)
13187               ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
13188                                   Type, VK_LValue, OK_Ordinary);
13189           ReductionOp =
13190               S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
13191                            BO_Assign, LHSDRE, ConditionalOp);
13192         }
13193         if (ReductionOp.isUsable())
13194           ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(),
13195                                               /*DiscardedValue*/ false);
13196       }
13197       if (!ReductionOp.isUsable())
13198         continue;
13199     }
13200 
13201     // OpenMP [2.15.4.6, Restrictions, p.2]
13202     // A list item that appears in an in_reduction clause of a task construct
13203     // must appear in a task_reduction clause of a construct associated with a
13204     // taskgroup region that includes the participating task in its taskgroup
13205     // set. The construct associated with the innermost region that meets this
13206     // condition must specify the same reduction-identifier as the in_reduction
13207     // clause.
13208     if (ClauseKind == OMPC_in_reduction) {
13209       SourceRange ParentSR;
13210       BinaryOperatorKind ParentBOK;
13211       const Expr *ParentReductionOp;
13212       Expr *ParentBOKTD, *ParentReductionOpTD;
13213       DSAStackTy::DSAVarData ParentBOKDSA =
13214           Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
13215                                                   ParentBOKTD);
13216       DSAStackTy::DSAVarData ParentReductionOpDSA =
13217           Stack->getTopMostTaskgroupReductionData(
13218               D, ParentSR, ParentReductionOp, ParentReductionOpTD);
13219       bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
13220       bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
13221       if (!IsParentBOK && !IsParentReductionOp) {
13222         S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction);
13223         continue;
13224       }
13225       if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
13226           (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK ||
13227           IsParentReductionOp) {
13228         bool EmitError = true;
13229         if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
13230           llvm::FoldingSetNodeID RedId, ParentRedId;
13231           ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
13232           DeclareReductionRef.get()->Profile(RedId, Context,
13233                                              /*Canonical=*/true);
13234           EmitError = RedId != ParentRedId;
13235         }
13236         if (EmitError) {
13237           S.Diag(ReductionId.getBeginLoc(),
13238                  diag::err_omp_reduction_identifier_mismatch)
13239               << ReductionIdRange << RefExpr->getSourceRange();
13240           S.Diag(ParentSR.getBegin(),
13241                  diag::note_omp_previous_reduction_identifier)
13242               << ParentSR
13243               << (IsParentBOK ? ParentBOKDSA.RefExpr
13244                               : ParentReductionOpDSA.RefExpr)
13245                      ->getSourceRange();
13246           continue;
13247         }
13248       }
13249       TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
13250       assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined.");
13251     }
13252 
13253     DeclRefExpr *Ref = nullptr;
13254     Expr *VarsExpr = RefExpr->IgnoreParens();
13255     if (!VD && !S.CurContext->isDependentContext()) {
13256       if (ASE || OASE) {
13257         TransformExprToCaptures RebuildToCapture(S, D);
13258         VarsExpr =
13259             RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
13260         Ref = RebuildToCapture.getCapturedExpr();
13261       } else {
13262         VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
13263       }
13264       if (!S.isOpenMPCapturedDecl(D)) {
13265         RD.ExprCaptures.emplace_back(Ref->getDecl());
13266         if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
13267           ExprResult RefRes = S.DefaultLvalueConversion(Ref);
13268           if (!RefRes.isUsable())
13269             continue;
13270           ExprResult PostUpdateRes =
13271               S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
13272                            RefRes.get());
13273           if (!PostUpdateRes.isUsable())
13274             continue;
13275           if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
13276               Stack->getCurrentDirective() == OMPD_taskgroup) {
13277             S.Diag(RefExpr->getExprLoc(),
13278                    diag::err_omp_reduction_non_addressable_expression)
13279                 << RefExpr->getSourceRange();
13280             continue;
13281           }
13282           RD.ExprPostUpdates.emplace_back(
13283               S.IgnoredValueConversions(PostUpdateRes.get()).get());
13284         }
13285       }
13286     }
13287     // All reduction items are still marked as reduction (to do not increase
13288     // code base size).
13289     Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
13290     if (CurrDir == OMPD_taskgroup) {
13291       if (DeclareReductionRef.isUsable())
13292         Stack->addTaskgroupReductionData(D, ReductionIdRange,
13293                                          DeclareReductionRef.get());
13294       else
13295         Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
13296     }
13297     RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
13298             TaskgroupDescriptor);
13299   }
13300   return RD.Vars.empty();
13301 }
13302 
13303 OMPClause *Sema::ActOnOpenMPReductionClause(
13304     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
13305     SourceLocation ColonLoc, SourceLocation EndLoc,
13306     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
13307     ArrayRef<Expr *> UnresolvedReductions) {
13308   ReductionData RD(VarList.size());
13309   if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
13310                                   StartLoc, LParenLoc, ColonLoc, EndLoc,
13311                                   ReductionIdScopeSpec, ReductionId,
13312                                   UnresolvedReductions, RD))
13313     return nullptr;
13314 
13315   return OMPReductionClause::Create(
13316       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
13317       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
13318       RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
13319       buildPreInits(Context, RD.ExprCaptures),
13320       buildPostUpdate(*this, RD.ExprPostUpdates));
13321 }
13322 
13323 OMPClause *Sema::ActOnOpenMPTaskReductionClause(
13324     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
13325     SourceLocation ColonLoc, SourceLocation EndLoc,
13326     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
13327     ArrayRef<Expr *> UnresolvedReductions) {
13328   ReductionData RD(VarList.size());
13329   if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList,
13330                                   StartLoc, LParenLoc, ColonLoc, EndLoc,
13331                                   ReductionIdScopeSpec, ReductionId,
13332                                   UnresolvedReductions, RD))
13333     return nullptr;
13334 
13335   return OMPTaskReductionClause::Create(
13336       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
13337       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
13338       RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
13339       buildPreInits(Context, RD.ExprCaptures),
13340       buildPostUpdate(*this, RD.ExprPostUpdates));
13341 }
13342 
13343 OMPClause *Sema::ActOnOpenMPInReductionClause(
13344     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
13345     SourceLocation ColonLoc, SourceLocation EndLoc,
13346     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
13347     ArrayRef<Expr *> UnresolvedReductions) {
13348   ReductionData RD(VarList.size());
13349   if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
13350                                   StartLoc, LParenLoc, ColonLoc, EndLoc,
13351                                   ReductionIdScopeSpec, ReductionId,
13352                                   UnresolvedReductions, RD))
13353     return nullptr;
13354 
13355   return OMPInReductionClause::Create(
13356       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
13357       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
13358       RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
13359       buildPreInits(Context, RD.ExprCaptures),
13360       buildPostUpdate(*this, RD.ExprPostUpdates));
13361 }
13362 
13363 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
13364                                      SourceLocation LinLoc) {
13365   if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
13366       LinKind == OMPC_LINEAR_unknown) {
13367     Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
13368     return true;
13369   }
13370   return false;
13371 }
13372 
13373 bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
13374                                  OpenMPLinearClauseKind LinKind,
13375                                  QualType Type) {
13376   const auto *VD = dyn_cast_or_null<VarDecl>(D);
13377   // A variable must not have an incomplete type or a reference type.
13378   if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
13379     return true;
13380   if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
13381       !Type->isReferenceType()) {
13382     Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
13383         << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
13384     return true;
13385   }
13386   Type = Type.getNonReferenceType();
13387 
13388   // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
13389   // A variable that is privatized must not have a const-qualified type
13390   // unless it is of class type with a mutable member. This restriction does
13391   // not apply to the firstprivate clause.
13392   if (rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc))
13393     return true;
13394 
13395   // A list item must be of integral or pointer type.
13396   Type = Type.getUnqualifiedType().getCanonicalType();
13397   const auto *Ty = Type.getTypePtrOrNull();
13398   if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
13399               !Ty->isPointerType())) {
13400     Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
13401     if (D) {
13402       bool IsDecl =
13403           !VD ||
13404           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
13405       Diag(D->getLocation(),
13406            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
13407           << D;
13408     }
13409     return true;
13410   }
13411   return false;
13412 }
13413 
13414 OMPClause *Sema::ActOnOpenMPLinearClause(
13415     ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
13416     SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
13417     SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
13418   SmallVector<Expr *, 8> Vars;
13419   SmallVector<Expr *, 8> Privates;
13420   SmallVector<Expr *, 8> Inits;
13421   SmallVector<Decl *, 4> ExprCaptures;
13422   SmallVector<Expr *, 4> ExprPostUpdates;
13423   if (CheckOpenMPLinearModifier(LinKind, LinLoc))
13424     LinKind = OMPC_LINEAR_val;
13425   for (Expr *RefExpr : VarList) {
13426     assert(RefExpr && "NULL expr in OpenMP linear clause.");
13427     SourceLocation ELoc;
13428     SourceRange ERange;
13429     Expr *SimpleRefExpr = RefExpr;
13430     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13431     if (Res.second) {
13432       // It will be analyzed later.
13433       Vars.push_back(RefExpr);
13434       Privates.push_back(nullptr);
13435       Inits.push_back(nullptr);
13436     }
13437     ValueDecl *D = Res.first;
13438     if (!D)
13439       continue;
13440 
13441     QualType Type = D->getType();
13442     auto *VD = dyn_cast<VarDecl>(D);
13443 
13444     // OpenMP [2.14.3.7, linear clause]
13445     //  A list-item cannot appear in more than one linear clause.
13446     //  A list-item that appears in a linear clause cannot appear in any
13447     //  other data-sharing attribute clause.
13448     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
13449     if (DVar.RefExpr) {
13450       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
13451                                           << getOpenMPClauseName(OMPC_linear);
13452       reportOriginalDsa(*this, DSAStack, D, DVar);
13453       continue;
13454     }
13455 
13456     if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
13457       continue;
13458     Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
13459 
13460     // Build private copy of original var.
13461     VarDecl *Private =
13462         buildVarDecl(*this, ELoc, Type, D->getName(),
13463                      D->hasAttrs() ? &D->getAttrs() : nullptr,
13464                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
13465     DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
13466     // Build var to save initial value.
13467     VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
13468     Expr *InitExpr;
13469     DeclRefExpr *Ref = nullptr;
13470     if (!VD && !CurContext->isDependentContext()) {
13471       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
13472       if (!isOpenMPCapturedDecl(D)) {
13473         ExprCaptures.push_back(Ref->getDecl());
13474         if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
13475           ExprResult RefRes = DefaultLvalueConversion(Ref);
13476           if (!RefRes.isUsable())
13477             continue;
13478           ExprResult PostUpdateRes =
13479               BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
13480                          SimpleRefExpr, RefRes.get());
13481           if (!PostUpdateRes.isUsable())
13482             continue;
13483           ExprPostUpdates.push_back(
13484               IgnoredValueConversions(PostUpdateRes.get()).get());
13485         }
13486       }
13487     }
13488     if (LinKind == OMPC_LINEAR_uval)
13489       InitExpr = VD ? VD->getInit() : SimpleRefExpr;
13490     else
13491       InitExpr = VD ? SimpleRefExpr : Ref;
13492     AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
13493                          /*DirectInit=*/false);
13494     DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
13495 
13496     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
13497     Vars.push_back((VD || CurContext->isDependentContext())
13498                        ? RefExpr->IgnoreParens()
13499                        : Ref);
13500     Privates.push_back(PrivateRef);
13501     Inits.push_back(InitRef);
13502   }
13503 
13504   if (Vars.empty())
13505     return nullptr;
13506 
13507   Expr *StepExpr = Step;
13508   Expr *CalcStepExpr = nullptr;
13509   if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
13510       !Step->isInstantiationDependent() &&
13511       !Step->containsUnexpandedParameterPack()) {
13512     SourceLocation StepLoc = Step->getBeginLoc();
13513     ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
13514     if (Val.isInvalid())
13515       return nullptr;
13516     StepExpr = Val.get();
13517 
13518     // Build var to save the step value.
13519     VarDecl *SaveVar =
13520         buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
13521     ExprResult SaveRef =
13522         buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
13523     ExprResult CalcStep =
13524         BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
13525     CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false);
13526 
13527     // Warn about zero linear step (it would be probably better specified as
13528     // making corresponding variables 'const').
13529     llvm::APSInt Result;
13530     bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
13531     if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
13532       Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
13533                                                      << (Vars.size() > 1);
13534     if (!IsConstant && CalcStep.isUsable()) {
13535       // Calculate the step beforehand instead of doing this on each iteration.
13536       // (This is not used if the number of iterations may be kfold-ed).
13537       CalcStepExpr = CalcStep.get();
13538     }
13539   }
13540 
13541   return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
13542                                  ColonLoc, EndLoc, Vars, Privates, Inits,
13543                                  StepExpr, CalcStepExpr,
13544                                  buildPreInits(Context, ExprCaptures),
13545                                  buildPostUpdate(*this, ExprPostUpdates));
13546 }
13547 
13548 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
13549                                      Expr *NumIterations, Sema &SemaRef,
13550                                      Scope *S, DSAStackTy *Stack) {
13551   // Walk the vars and build update/final expressions for the CodeGen.
13552   SmallVector<Expr *, 8> Updates;
13553   SmallVector<Expr *, 8> Finals;
13554   SmallVector<Expr *, 8> UsedExprs;
13555   Expr *Step = Clause.getStep();
13556   Expr *CalcStep = Clause.getCalcStep();
13557   // OpenMP [2.14.3.7, linear clause]
13558   // If linear-step is not specified it is assumed to be 1.
13559   if (!Step)
13560     Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
13561   else if (CalcStep)
13562     Step = cast<BinaryOperator>(CalcStep)->getLHS();
13563   bool HasErrors = false;
13564   auto CurInit = Clause.inits().begin();
13565   auto CurPrivate = Clause.privates().begin();
13566   OpenMPLinearClauseKind LinKind = Clause.getModifier();
13567   for (Expr *RefExpr : Clause.varlists()) {
13568     SourceLocation ELoc;
13569     SourceRange ERange;
13570     Expr *SimpleRefExpr = RefExpr;
13571     auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange);
13572     ValueDecl *D = Res.first;
13573     if (Res.second || !D) {
13574       Updates.push_back(nullptr);
13575       Finals.push_back(nullptr);
13576       HasErrors = true;
13577       continue;
13578     }
13579     auto &&Info = Stack->isLoopControlVariable(D);
13580     // OpenMP [2.15.11, distribute simd Construct]
13581     // A list item may not appear in a linear clause, unless it is the loop
13582     // iteration variable.
13583     if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
13584         isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
13585       SemaRef.Diag(ELoc,
13586                    diag::err_omp_linear_distribute_var_non_loop_iteration);
13587       Updates.push_back(nullptr);
13588       Finals.push_back(nullptr);
13589       HasErrors = true;
13590       continue;
13591     }
13592     Expr *InitExpr = *CurInit;
13593 
13594     // Build privatized reference to the current linear var.
13595     auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
13596     Expr *CapturedRef;
13597     if (LinKind == OMPC_LINEAR_uval)
13598       CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
13599     else
13600       CapturedRef =
13601           buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
13602                            DE->getType().getUnqualifiedType(), DE->getExprLoc(),
13603                            /*RefersToCapture=*/true);
13604 
13605     // Build update: Var = InitExpr + IV * Step
13606     ExprResult Update;
13607     if (!Info.first)
13608       Update = buildCounterUpdate(
13609           SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, InitExpr, IV, Step,
13610           /*Subtract=*/false, /*IsNonRectangularLB=*/false);
13611     else
13612       Update = *CurPrivate;
13613     Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(),
13614                                          /*DiscardedValue*/ false);
13615 
13616     // Build final: Var = InitExpr + NumIterations * Step
13617     ExprResult Final;
13618     if (!Info.first)
13619       Final =
13620           buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
13621                              InitExpr, NumIterations, Step, /*Subtract=*/false,
13622                              /*IsNonRectangularLB=*/false);
13623     else
13624       Final = *CurPrivate;
13625     Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(),
13626                                         /*DiscardedValue*/ false);
13627 
13628     if (!Update.isUsable() || !Final.isUsable()) {
13629       Updates.push_back(nullptr);
13630       Finals.push_back(nullptr);
13631       UsedExprs.push_back(nullptr);
13632       HasErrors = true;
13633     } else {
13634       Updates.push_back(Update.get());
13635       Finals.push_back(Final.get());
13636       if (!Info.first)
13637         UsedExprs.push_back(SimpleRefExpr);
13638     }
13639     ++CurInit;
13640     ++CurPrivate;
13641   }
13642   if (Expr *S = Clause.getStep())
13643     UsedExprs.push_back(S);
13644   // Fill the remaining part with the nullptr.
13645   UsedExprs.append(Clause.varlist_size() + 1 - UsedExprs.size(), nullptr);
13646   Clause.setUpdates(Updates);
13647   Clause.setFinals(Finals);
13648   Clause.setUsedExprs(UsedExprs);
13649   return HasErrors;
13650 }
13651 
13652 OMPClause *Sema::ActOnOpenMPAlignedClause(
13653     ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
13654     SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
13655   SmallVector<Expr *, 8> Vars;
13656   for (Expr *RefExpr : VarList) {
13657     assert(RefExpr && "NULL expr in OpenMP linear clause.");
13658     SourceLocation ELoc;
13659     SourceRange ERange;
13660     Expr *SimpleRefExpr = RefExpr;
13661     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13662     if (Res.second) {
13663       // It will be analyzed later.
13664       Vars.push_back(RefExpr);
13665     }
13666     ValueDecl *D = Res.first;
13667     if (!D)
13668       continue;
13669 
13670     QualType QType = D->getType();
13671     auto *VD = dyn_cast<VarDecl>(D);
13672 
13673     // OpenMP  [2.8.1, simd construct, Restrictions]
13674     // The type of list items appearing in the aligned clause must be
13675     // array, pointer, reference to array, or reference to pointer.
13676     QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
13677     const Type *Ty = QType.getTypePtrOrNull();
13678     if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
13679       Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
13680           << QType << getLangOpts().CPlusPlus << ERange;
13681       bool IsDecl =
13682           !VD ||
13683           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
13684       Diag(D->getLocation(),
13685            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
13686           << D;
13687       continue;
13688     }
13689 
13690     // OpenMP  [2.8.1, simd construct, Restrictions]
13691     // A list-item cannot appear in more than one aligned clause.
13692     if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
13693       Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
13694       Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
13695           << getOpenMPClauseName(OMPC_aligned);
13696       continue;
13697     }
13698 
13699     DeclRefExpr *Ref = nullptr;
13700     if (!VD && isOpenMPCapturedDecl(D))
13701       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
13702     Vars.push_back(DefaultFunctionArrayConversion(
13703                        (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
13704                        .get());
13705   }
13706 
13707   // OpenMP [2.8.1, simd construct, Description]
13708   // The parameter of the aligned clause, alignment, must be a constant
13709   // positive integer expression.
13710   // If no optional parameter is specified, implementation-defined default
13711   // alignments for SIMD instructions on the target platforms are assumed.
13712   if (Alignment != nullptr) {
13713     ExprResult AlignResult =
13714         VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
13715     if (AlignResult.isInvalid())
13716       return nullptr;
13717     Alignment = AlignResult.get();
13718   }
13719   if (Vars.empty())
13720     return nullptr;
13721 
13722   return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
13723                                   EndLoc, Vars, Alignment);
13724 }
13725 
13726 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
13727                                          SourceLocation StartLoc,
13728                                          SourceLocation LParenLoc,
13729                                          SourceLocation EndLoc) {
13730   SmallVector<Expr *, 8> Vars;
13731   SmallVector<Expr *, 8> SrcExprs;
13732   SmallVector<Expr *, 8> DstExprs;
13733   SmallVector<Expr *, 8> AssignmentOps;
13734   for (Expr *RefExpr : VarList) {
13735     assert(RefExpr && "NULL expr in OpenMP copyin clause.");
13736     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
13737       // It will be analyzed later.
13738       Vars.push_back(RefExpr);
13739       SrcExprs.push_back(nullptr);
13740       DstExprs.push_back(nullptr);
13741       AssignmentOps.push_back(nullptr);
13742       continue;
13743     }
13744 
13745     SourceLocation ELoc = RefExpr->getExprLoc();
13746     // OpenMP [2.1, C/C++]
13747     //  A list item is a variable name.
13748     // OpenMP  [2.14.4.1, Restrictions, p.1]
13749     //  A list item that appears in a copyin clause must be threadprivate.
13750     auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
13751     if (!DE || !isa<VarDecl>(DE->getDecl())) {
13752       Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
13753           << 0 << RefExpr->getSourceRange();
13754       continue;
13755     }
13756 
13757     Decl *D = DE->getDecl();
13758     auto *VD = cast<VarDecl>(D);
13759 
13760     QualType Type = VD->getType();
13761     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
13762       // It will be analyzed later.
13763       Vars.push_back(DE);
13764       SrcExprs.push_back(nullptr);
13765       DstExprs.push_back(nullptr);
13766       AssignmentOps.push_back(nullptr);
13767       continue;
13768     }
13769 
13770     // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
13771     //  A list item that appears in a copyin clause must be threadprivate.
13772     if (!DSAStack->isThreadPrivate(VD)) {
13773       Diag(ELoc, diag::err_omp_required_access)
13774           << getOpenMPClauseName(OMPC_copyin)
13775           << getOpenMPDirectiveName(OMPD_threadprivate);
13776       continue;
13777     }
13778 
13779     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
13780     //  A variable of class type (or array thereof) that appears in a
13781     //  copyin clause requires an accessible, unambiguous copy assignment
13782     //  operator for the class type.
13783     QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
13784     VarDecl *SrcVD =
13785         buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(),
13786                      ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
13787     DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
13788         *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
13789     VarDecl *DstVD =
13790         buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst",
13791                      VD->hasAttrs() ? &VD->getAttrs() : nullptr);
13792     DeclRefExpr *PseudoDstExpr =
13793         buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
13794     // For arrays generate assignment operation for single element and replace
13795     // it by the original array element in CodeGen.
13796     ExprResult AssignmentOp =
13797         BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr,
13798                    PseudoSrcExpr);
13799     if (AssignmentOp.isInvalid())
13800       continue;
13801     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
13802                                        /*DiscardedValue*/ false);
13803     if (AssignmentOp.isInvalid())
13804       continue;
13805 
13806     DSAStack->addDSA(VD, DE, OMPC_copyin);
13807     Vars.push_back(DE);
13808     SrcExprs.push_back(PseudoSrcExpr);
13809     DstExprs.push_back(PseudoDstExpr);
13810     AssignmentOps.push_back(AssignmentOp.get());
13811   }
13812 
13813   if (Vars.empty())
13814     return nullptr;
13815 
13816   return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
13817                                  SrcExprs, DstExprs, AssignmentOps);
13818 }
13819 
13820 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
13821                                               SourceLocation StartLoc,
13822                                               SourceLocation LParenLoc,
13823                                               SourceLocation EndLoc) {
13824   SmallVector<Expr *, 8> Vars;
13825   SmallVector<Expr *, 8> SrcExprs;
13826   SmallVector<Expr *, 8> DstExprs;
13827   SmallVector<Expr *, 8> AssignmentOps;
13828   for (Expr *RefExpr : VarList) {
13829     assert(RefExpr && "NULL expr in OpenMP linear clause.");
13830     SourceLocation ELoc;
13831     SourceRange ERange;
13832     Expr *SimpleRefExpr = RefExpr;
13833     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13834     if (Res.second) {
13835       // It will be analyzed later.
13836       Vars.push_back(RefExpr);
13837       SrcExprs.push_back(nullptr);
13838       DstExprs.push_back(nullptr);
13839       AssignmentOps.push_back(nullptr);
13840     }
13841     ValueDecl *D = Res.first;
13842     if (!D)
13843       continue;
13844 
13845     QualType Type = D->getType();
13846     auto *VD = dyn_cast<VarDecl>(D);
13847 
13848     // OpenMP [2.14.4.2, Restrictions, p.2]
13849     //  A list item that appears in a copyprivate clause may not appear in a
13850     //  private or firstprivate clause on the single construct.
13851     if (!VD || !DSAStack->isThreadPrivate(VD)) {
13852       DSAStackTy::DSAVarData DVar =
13853           DSAStack->getTopDSA(D, /*FromParent=*/false);
13854       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
13855           DVar.RefExpr) {
13856         Diag(ELoc, diag::err_omp_wrong_dsa)
13857             << getOpenMPClauseName(DVar.CKind)
13858             << getOpenMPClauseName(OMPC_copyprivate);
13859         reportOriginalDsa(*this, DSAStack, D, DVar);
13860         continue;
13861       }
13862 
13863       // OpenMP [2.11.4.2, Restrictions, p.1]
13864       //  All list items that appear in a copyprivate clause must be either
13865       //  threadprivate or private in the enclosing context.
13866       if (DVar.CKind == OMPC_unknown) {
13867         DVar = DSAStack->getImplicitDSA(D, false);
13868         if (DVar.CKind == OMPC_shared) {
13869           Diag(ELoc, diag::err_omp_required_access)
13870               << getOpenMPClauseName(OMPC_copyprivate)
13871               << "threadprivate or private in the enclosing context";
13872           reportOriginalDsa(*this, DSAStack, D, DVar);
13873           continue;
13874         }
13875       }
13876     }
13877 
13878     // Variably modified types are not supported.
13879     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
13880       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
13881           << getOpenMPClauseName(OMPC_copyprivate) << Type
13882           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
13883       bool IsDecl =
13884           !VD ||
13885           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
13886       Diag(D->getLocation(),
13887            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
13888           << D;
13889       continue;
13890     }
13891 
13892     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
13893     //  A variable of class type (or array thereof) that appears in a
13894     //  copyin clause requires an accessible, unambiguous copy assignment
13895     //  operator for the class type.
13896     Type = Context.getBaseElementType(Type.getNonReferenceType())
13897                .getUnqualifiedType();
13898     VarDecl *SrcVD =
13899         buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src",
13900                      D->hasAttrs() ? &D->getAttrs() : nullptr);
13901     DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
13902     VarDecl *DstVD =
13903         buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst",
13904                      D->hasAttrs() ? &D->getAttrs() : nullptr);
13905     DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
13906     ExprResult AssignmentOp = BuildBinOp(
13907         DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr);
13908     if (AssignmentOp.isInvalid())
13909       continue;
13910     AssignmentOp =
13911         ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
13912     if (AssignmentOp.isInvalid())
13913       continue;
13914 
13915     // No need to mark vars as copyprivate, they are already threadprivate or
13916     // implicitly private.
13917     assert(VD || isOpenMPCapturedDecl(D));
13918     Vars.push_back(
13919         VD ? RefExpr->IgnoreParens()
13920            : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
13921     SrcExprs.push_back(PseudoSrcExpr);
13922     DstExprs.push_back(PseudoDstExpr);
13923     AssignmentOps.push_back(AssignmentOp.get());
13924   }
13925 
13926   if (Vars.empty())
13927     return nullptr;
13928 
13929   return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13930                                       Vars, SrcExprs, DstExprs, AssignmentOps);
13931 }
13932 
13933 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
13934                                         SourceLocation StartLoc,
13935                                         SourceLocation LParenLoc,
13936                                         SourceLocation EndLoc) {
13937   if (VarList.empty())
13938     return nullptr;
13939 
13940   return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
13941 }
13942 
13943 OMPClause *
13944 Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
13945                               SourceLocation DepLoc, SourceLocation ColonLoc,
13946                               ArrayRef<Expr *> VarList, SourceLocation StartLoc,
13947                               SourceLocation LParenLoc, SourceLocation EndLoc) {
13948   if (DSAStack->getCurrentDirective() == OMPD_ordered &&
13949       DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
13950     Diag(DepLoc, diag::err_omp_unexpected_clause_value)
13951         << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
13952     return nullptr;
13953   }
13954   if (DSAStack->getCurrentDirective() != OMPD_ordered &&
13955       (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
13956        DepKind == OMPC_DEPEND_sink)) {
13957     unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
13958     Diag(DepLoc, diag::err_omp_unexpected_clause_value)
13959         << getListOfPossibleValues(OMPC_depend, /*First=*/0,
13960                                    /*Last=*/OMPC_DEPEND_unknown, Except)
13961         << getOpenMPClauseName(OMPC_depend);
13962     return nullptr;
13963   }
13964   SmallVector<Expr *, 8> Vars;
13965   DSAStackTy::OperatorOffsetTy OpsOffs;
13966   llvm::APSInt DepCounter(/*BitWidth=*/32);
13967   llvm::APSInt TotalDepCount(/*BitWidth=*/32);
13968   if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
13969     if (const Expr *OrderedCountExpr =
13970             DSAStack->getParentOrderedRegionParam().first) {
13971       TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
13972       TotalDepCount.setIsUnsigned(/*Val=*/true);
13973     }
13974   }
13975   for (Expr *RefExpr : VarList) {
13976     assert(RefExpr && "NULL expr in OpenMP shared clause.");
13977     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
13978       // It will be analyzed later.
13979       Vars.push_back(RefExpr);
13980       continue;
13981     }
13982 
13983     SourceLocation ELoc = RefExpr->getExprLoc();
13984     Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
13985     if (DepKind == OMPC_DEPEND_sink) {
13986       if (DSAStack->getParentOrderedRegionParam().first &&
13987           DepCounter >= TotalDepCount) {
13988         Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
13989         continue;
13990       }
13991       ++DepCounter;
13992       // OpenMP  [2.13.9, Summary]
13993       // depend(dependence-type : vec), where dependence-type is:
13994       // 'sink' and where vec is the iteration vector, which has the form:
13995       //  x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
13996       // where n is the value specified by the ordered clause in the loop
13997       // directive, xi denotes the loop iteration variable of the i-th nested
13998       // loop associated with the loop directive, and di is a constant
13999       // non-negative integer.
14000       if (CurContext->isDependentContext()) {
14001         // It will be analyzed later.
14002         Vars.push_back(RefExpr);
14003         continue;
14004       }
14005       SimpleExpr = SimpleExpr->IgnoreImplicit();
14006       OverloadedOperatorKind OOK = OO_None;
14007       SourceLocation OOLoc;
14008       Expr *LHS = SimpleExpr;
14009       Expr *RHS = nullptr;
14010       if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
14011         OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
14012         OOLoc = BO->getOperatorLoc();
14013         LHS = BO->getLHS()->IgnoreParenImpCasts();
14014         RHS = BO->getRHS()->IgnoreParenImpCasts();
14015       } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
14016         OOK = OCE->getOperator();
14017         OOLoc = OCE->getOperatorLoc();
14018         LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
14019         RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
14020       } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
14021         OOK = MCE->getMethodDecl()
14022                   ->getNameInfo()
14023                   .getName()
14024                   .getCXXOverloadedOperator();
14025         OOLoc = MCE->getCallee()->getExprLoc();
14026         LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
14027         RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
14028       }
14029       SourceLocation ELoc;
14030       SourceRange ERange;
14031       auto Res = getPrivateItem(*this, LHS, ELoc, ERange);
14032       if (Res.second) {
14033         // It will be analyzed later.
14034         Vars.push_back(RefExpr);
14035       }
14036       ValueDecl *D = Res.first;
14037       if (!D)
14038         continue;
14039 
14040       if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
14041         Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
14042         continue;
14043       }
14044       if (RHS) {
14045         ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
14046             RHS, OMPC_depend, /*StrictlyPositive=*/false);
14047         if (RHSRes.isInvalid())
14048           continue;
14049       }
14050       if (!CurContext->isDependentContext() &&
14051           DSAStack->getParentOrderedRegionParam().first &&
14052           DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
14053         const ValueDecl *VD =
14054             DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue());
14055         if (VD)
14056           Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
14057               << 1 << VD;
14058         else
14059           Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
14060         continue;
14061       }
14062       OpsOffs.emplace_back(RHS, OOK);
14063     } else {
14064       auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
14065       if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
14066           (ASE &&
14067            !ASE->getBase()->getType().getNonReferenceType()->isPointerType() &&
14068            !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
14069         Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
14070             << RefExpr->getSourceRange();
14071         continue;
14072       }
14073 
14074       ExprResult Res;
14075       {
14076         Sema::TentativeAnalysisScope Trap(*this);
14077         Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf,
14078                                    RefExpr->IgnoreParenImpCasts());
14079       }
14080       if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) {
14081         Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
14082             << RefExpr->getSourceRange();
14083         continue;
14084       }
14085     }
14086     Vars.push_back(RefExpr->IgnoreParenImpCasts());
14087   }
14088 
14089   if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
14090       TotalDepCount > VarList.size() &&
14091       DSAStack->getParentOrderedRegionParam().first &&
14092       DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
14093     Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
14094         << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
14095   }
14096   if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
14097       Vars.empty())
14098     return nullptr;
14099 
14100   auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
14101                                     DepKind, DepLoc, ColonLoc, Vars,
14102                                     TotalDepCount.getZExtValue());
14103   if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
14104       DSAStack->isParentOrderedRegion())
14105     DSAStack->addDoacrossDependClause(C, OpsOffs);
14106   return C;
14107 }
14108 
14109 OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
14110                                          SourceLocation LParenLoc,
14111                                          SourceLocation EndLoc) {
14112   Expr *ValExpr = Device;
14113   Stmt *HelperValStmt = nullptr;
14114 
14115   // OpenMP [2.9.1, Restrictions]
14116   // The device expression must evaluate to a non-negative integer value.
14117   if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
14118                                  /*StrictlyPositive=*/false))
14119     return nullptr;
14120 
14121   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
14122   OpenMPDirectiveKind CaptureRegion =
14123       getOpenMPCaptureRegionForClause(DKind, OMPC_device);
14124   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
14125     ValExpr = MakeFullExpr(ValExpr).get();
14126     llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
14127     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
14128     HelperValStmt = buildPreInits(Context, Captures);
14129   }
14130 
14131   return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion,
14132                                        StartLoc, LParenLoc, EndLoc);
14133 }
14134 
14135 static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
14136                               DSAStackTy *Stack, QualType QTy,
14137                               bool FullCheck = true) {
14138   NamedDecl *ND;
14139   if (QTy->isIncompleteType(&ND)) {
14140     SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
14141     return false;
14142   }
14143   if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
14144       !QTy.isTrivialType(SemaRef.Context))
14145     SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
14146   return true;
14147 }
14148 
14149 /// Return true if it can be proven that the provided array expression
14150 /// (array section or array subscript) does NOT specify the whole size of the
14151 /// array whose base type is \a BaseQTy.
14152 static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
14153                                                         const Expr *E,
14154                                                         QualType BaseQTy) {
14155   const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
14156 
14157   // If this is an array subscript, it refers to the whole size if the size of
14158   // the dimension is constant and equals 1. Also, an array section assumes the
14159   // format of an array subscript if no colon is used.
14160   if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
14161     if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
14162       return ATy->getSize().getSExtValue() != 1;
14163     // Size can't be evaluated statically.
14164     return false;
14165   }
14166 
14167   assert(OASE && "Expecting array section if not an array subscript.");
14168   const Expr *LowerBound = OASE->getLowerBound();
14169   const Expr *Length = OASE->getLength();
14170 
14171   // If there is a lower bound that does not evaluates to zero, we are not
14172   // covering the whole dimension.
14173   if (LowerBound) {
14174     Expr::EvalResult Result;
14175     if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext()))
14176       return false; // Can't get the integer value as a constant.
14177 
14178     llvm::APSInt ConstLowerBound = Result.Val.getInt();
14179     if (ConstLowerBound.getSExtValue())
14180       return true;
14181   }
14182 
14183   // If we don't have a length we covering the whole dimension.
14184   if (!Length)
14185     return false;
14186 
14187   // If the base is a pointer, we don't have a way to get the size of the
14188   // pointee.
14189   if (BaseQTy->isPointerType())
14190     return false;
14191 
14192   // We can only check if the length is the same as the size of the dimension
14193   // if we have a constant array.
14194   const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
14195   if (!CATy)
14196     return false;
14197 
14198   Expr::EvalResult Result;
14199   if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
14200     return false; // Can't get the integer value as a constant.
14201 
14202   llvm::APSInt ConstLength = Result.Val.getInt();
14203   return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
14204 }
14205 
14206 // Return true if it can be proven that the provided array expression (array
14207 // section or array subscript) does NOT specify a single element of the array
14208 // whose base type is \a BaseQTy.
14209 static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
14210                                                         const Expr *E,
14211                                                         QualType BaseQTy) {
14212   const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
14213 
14214   // An array subscript always refer to a single element. Also, an array section
14215   // assumes the format of an array subscript if no colon is used.
14216   if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
14217     return false;
14218 
14219   assert(OASE && "Expecting array section if not an array subscript.");
14220   const Expr *Length = OASE->getLength();
14221 
14222   // If we don't have a length we have to check if the array has unitary size
14223   // for this dimension. Also, we should always expect a length if the base type
14224   // is pointer.
14225   if (!Length) {
14226     if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
14227       return ATy->getSize().getSExtValue() != 1;
14228     // We cannot assume anything.
14229     return false;
14230   }
14231 
14232   // Check if the length evaluates to 1.
14233   Expr::EvalResult Result;
14234   if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
14235     return false; // Can't get the integer value as a constant.
14236 
14237   llvm::APSInt ConstLength = Result.Val.getInt();
14238   return ConstLength.getSExtValue() != 1;
14239 }
14240 
14241 // Return the expression of the base of the mappable expression or null if it
14242 // cannot be determined and do all the necessary checks to see if the expression
14243 // is valid as a standalone mappable expression. In the process, record all the
14244 // components of the expression.
14245 static const Expr *checkMapClauseExpressionBase(
14246     Sema &SemaRef, Expr *E,
14247     OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
14248     OpenMPClauseKind CKind, bool NoDiagnose) {
14249   SourceLocation ELoc = E->getExprLoc();
14250   SourceRange ERange = E->getSourceRange();
14251 
14252   // The base of elements of list in a map clause have to be either:
14253   //  - a reference to variable or field.
14254   //  - a member expression.
14255   //  - an array expression.
14256   //
14257   // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
14258   // reference to 'r'.
14259   //
14260   // If we have:
14261   //
14262   // struct SS {
14263   //   Bla S;
14264   //   foo() {
14265   //     #pragma omp target map (S.Arr[:12]);
14266   //   }
14267   // }
14268   //
14269   // We want to retrieve the member expression 'this->S';
14270 
14271   const Expr *RelevantExpr = nullptr;
14272 
14273   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
14274   //  If a list item is an array section, it must specify contiguous storage.
14275   //
14276   // For this restriction it is sufficient that we make sure only references
14277   // to variables or fields and array expressions, and that no array sections
14278   // exist except in the rightmost expression (unless they cover the whole
14279   // dimension of the array). E.g. these would be invalid:
14280   //
14281   //   r.ArrS[3:5].Arr[6:7]
14282   //
14283   //   r.ArrS[3:5].x
14284   //
14285   // but these would be valid:
14286   //   r.ArrS[3].Arr[6:7]
14287   //
14288   //   r.ArrS[3].x
14289 
14290   bool AllowUnitySizeArraySection = true;
14291   bool AllowWholeSizeArraySection = true;
14292 
14293   while (!RelevantExpr) {
14294     E = E->IgnoreParenImpCasts();
14295 
14296     if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
14297       if (!isa<VarDecl>(CurE->getDecl()))
14298         return nullptr;
14299 
14300       RelevantExpr = CurE;
14301 
14302       // If we got a reference to a declaration, we should not expect any array
14303       // section before that.
14304       AllowUnitySizeArraySection = false;
14305       AllowWholeSizeArraySection = false;
14306 
14307       // Record the component.
14308       CurComponents.emplace_back(CurE, CurE->getDecl());
14309     } else if (auto *CurE = dyn_cast<MemberExpr>(E)) {
14310       Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts();
14311 
14312       if (isa<CXXThisExpr>(BaseE))
14313         // We found a base expression: this->Val.
14314         RelevantExpr = CurE;
14315       else
14316         E = BaseE;
14317 
14318       if (!isa<FieldDecl>(CurE->getMemberDecl())) {
14319         if (!NoDiagnose) {
14320           SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
14321               << CurE->getSourceRange();
14322           return nullptr;
14323         }
14324         if (RelevantExpr)
14325           return nullptr;
14326         continue;
14327       }
14328 
14329       auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
14330 
14331       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
14332       //  A bit-field cannot appear in a map clause.
14333       //
14334       if (FD->isBitField()) {
14335         if (!NoDiagnose) {
14336           SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
14337               << CurE->getSourceRange() << getOpenMPClauseName(CKind);
14338           return nullptr;
14339         }
14340         if (RelevantExpr)
14341           return nullptr;
14342         continue;
14343       }
14344 
14345       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
14346       //  If the type of a list item is a reference to a type T then the type
14347       //  will be considered to be T for all purposes of this clause.
14348       QualType CurType = BaseE->getType().getNonReferenceType();
14349 
14350       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
14351       //  A list item cannot be a variable that is a member of a structure with
14352       //  a union type.
14353       //
14354       if (CurType->isUnionType()) {
14355         if (!NoDiagnose) {
14356           SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
14357               << CurE->getSourceRange();
14358           return nullptr;
14359         }
14360         continue;
14361       }
14362 
14363       // If we got a member expression, we should not expect any array section
14364       // before that:
14365       //
14366       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
14367       //  If a list item is an element of a structure, only the rightmost symbol
14368       //  of the variable reference can be an array section.
14369       //
14370       AllowUnitySizeArraySection = false;
14371       AllowWholeSizeArraySection = false;
14372 
14373       // Record the component.
14374       CurComponents.emplace_back(CurE, FD);
14375     } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
14376       E = CurE->getBase()->IgnoreParenImpCasts();
14377 
14378       if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
14379         if (!NoDiagnose) {
14380           SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
14381               << 0 << CurE->getSourceRange();
14382           return nullptr;
14383         }
14384         continue;
14385       }
14386 
14387       // If we got an array subscript that express the whole dimension we
14388       // can have any array expressions before. If it only expressing part of
14389       // the dimension, we can only have unitary-size array expressions.
14390       if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
14391                                                       E->getType()))
14392         AllowWholeSizeArraySection = false;
14393 
14394       if (const auto *TE = dyn_cast<CXXThisExpr>(E)) {
14395         Expr::EvalResult Result;
14396         if (CurE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext())) {
14397           if (!Result.Val.getInt().isNullValue()) {
14398             SemaRef.Diag(CurE->getIdx()->getExprLoc(),
14399                          diag::err_omp_invalid_map_this_expr);
14400             SemaRef.Diag(CurE->getIdx()->getExprLoc(),
14401                          diag::note_omp_invalid_subscript_on_this_ptr_map);
14402           }
14403         }
14404         RelevantExpr = TE;
14405       }
14406 
14407       // Record the component - we don't have any declaration associated.
14408       CurComponents.emplace_back(CurE, nullptr);
14409     } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
14410       assert(!NoDiagnose && "Array sections cannot be implicitly mapped.");
14411       E = CurE->getBase()->IgnoreParenImpCasts();
14412 
14413       QualType CurType =
14414           OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
14415 
14416       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
14417       //  If the type of a list item is a reference to a type T then the type
14418       //  will be considered to be T for all purposes of this clause.
14419       if (CurType->isReferenceType())
14420         CurType = CurType->getPointeeType();
14421 
14422       bool IsPointer = CurType->isAnyPointerType();
14423 
14424       if (!IsPointer && !CurType->isArrayType()) {
14425         SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
14426             << 0 << CurE->getSourceRange();
14427         return nullptr;
14428       }
14429 
14430       bool NotWhole =
14431           checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
14432       bool NotUnity =
14433           checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
14434 
14435       if (AllowWholeSizeArraySection) {
14436         // Any array section is currently allowed. Allowing a whole size array
14437         // section implies allowing a unity array section as well.
14438         //
14439         // If this array section refers to the whole dimension we can still
14440         // accept other array sections before this one, except if the base is a
14441         // pointer. Otherwise, only unitary sections are accepted.
14442         if (NotWhole || IsPointer)
14443           AllowWholeSizeArraySection = false;
14444       } else if (AllowUnitySizeArraySection && NotUnity) {
14445         // A unity or whole array section is not allowed and that is not
14446         // compatible with the properties of the current array section.
14447         SemaRef.Diag(
14448             ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
14449             << CurE->getSourceRange();
14450         return nullptr;
14451       }
14452 
14453       if (const auto *TE = dyn_cast<CXXThisExpr>(E)) {
14454         Expr::EvalResult ResultR;
14455         Expr::EvalResult ResultL;
14456         if (CurE->getLength()->EvaluateAsInt(ResultR,
14457                                              SemaRef.getASTContext())) {
14458           if (!ResultR.Val.getInt().isOneValue()) {
14459             SemaRef.Diag(CurE->getLength()->getExprLoc(),
14460                          diag::err_omp_invalid_map_this_expr);
14461             SemaRef.Diag(CurE->getLength()->getExprLoc(),
14462                          diag::note_omp_invalid_length_on_this_ptr_mapping);
14463           }
14464         }
14465         if (CurE->getLowerBound() && CurE->getLowerBound()->EvaluateAsInt(
14466                                         ResultL, SemaRef.getASTContext())) {
14467           if (!ResultL.Val.getInt().isNullValue()) {
14468             SemaRef.Diag(CurE->getLowerBound()->getExprLoc(),
14469                          diag::err_omp_invalid_map_this_expr);
14470             SemaRef.Diag(CurE->getLowerBound()->getExprLoc(),
14471                          diag::note_omp_invalid_lower_bound_on_this_ptr_mapping);
14472           }
14473         }
14474         RelevantExpr = TE;
14475       }
14476 
14477       // Record the component - we don't have any declaration associated.
14478       CurComponents.emplace_back(CurE, nullptr);
14479     } else {
14480       if (!NoDiagnose) {
14481         // If nothing else worked, this is not a valid map clause expression.
14482         SemaRef.Diag(
14483             ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
14484             << ERange;
14485       }
14486       return nullptr;
14487     }
14488   }
14489 
14490   return RelevantExpr;
14491 }
14492 
14493 // Return true if expression E associated with value VD has conflicts with other
14494 // map information.
14495 static bool checkMapConflicts(
14496     Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
14497     bool CurrentRegionOnly,
14498     OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
14499     OpenMPClauseKind CKind) {
14500   assert(VD && E);
14501   SourceLocation ELoc = E->getExprLoc();
14502   SourceRange ERange = E->getSourceRange();
14503 
14504   // In order to easily check the conflicts we need to match each component of
14505   // the expression under test with the components of the expressions that are
14506   // already in the stack.
14507 
14508   assert(!CurComponents.empty() && "Map clause expression with no components!");
14509   assert(CurComponents.back().getAssociatedDeclaration() == VD &&
14510          "Map clause expression with unexpected base!");
14511 
14512   // Variables to help detecting enclosing problems in data environment nests.
14513   bool IsEnclosedByDataEnvironmentExpr = false;
14514   const Expr *EnclosingExpr = nullptr;
14515 
14516   bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
14517       VD, CurrentRegionOnly,
14518       [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
14519        ERange, CKind, &EnclosingExpr,
14520        CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
14521                           StackComponents,
14522                       OpenMPClauseKind) {
14523         assert(!StackComponents.empty() &&
14524                "Map clause expression with no components!");
14525         assert(StackComponents.back().getAssociatedDeclaration() == VD &&
14526                "Map clause expression with unexpected base!");
14527         (void)VD;
14528 
14529         // The whole expression in the stack.
14530         const Expr *RE = StackComponents.front().getAssociatedExpression();
14531 
14532         // Expressions must start from the same base. Here we detect at which
14533         // point both expressions diverge from each other and see if we can
14534         // detect if the memory referred to both expressions is contiguous and
14535         // do not overlap.
14536         auto CI = CurComponents.rbegin();
14537         auto CE = CurComponents.rend();
14538         auto SI = StackComponents.rbegin();
14539         auto SE = StackComponents.rend();
14540         for (; CI != CE && SI != SE; ++CI, ++SI) {
14541 
14542           // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
14543           //  At most one list item can be an array item derived from a given
14544           //  variable in map clauses of the same construct.
14545           if (CurrentRegionOnly &&
14546               (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
14547                isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
14548               (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
14549                isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
14550             SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
14551                          diag::err_omp_multiple_array_items_in_map_clause)
14552                 << CI->getAssociatedExpression()->getSourceRange();
14553             SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
14554                          diag::note_used_here)
14555                 << SI->getAssociatedExpression()->getSourceRange();
14556             return true;
14557           }
14558 
14559           // Do both expressions have the same kind?
14560           if (CI->getAssociatedExpression()->getStmtClass() !=
14561               SI->getAssociatedExpression()->getStmtClass())
14562             break;
14563 
14564           // Are we dealing with different variables/fields?
14565           if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
14566             break;
14567         }
14568         // Check if the extra components of the expressions in the enclosing
14569         // data environment are redundant for the current base declaration.
14570         // If they are, the maps completely overlap, which is legal.
14571         for (; SI != SE; ++SI) {
14572           QualType Type;
14573           if (const auto *ASE =
14574                   dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
14575             Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
14576           } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(
14577                          SI->getAssociatedExpression())) {
14578             const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
14579             Type =
14580                 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
14581           }
14582           if (Type.isNull() || Type->isAnyPointerType() ||
14583               checkArrayExpressionDoesNotReferToWholeSize(
14584                   SemaRef, SI->getAssociatedExpression(), Type))
14585             break;
14586         }
14587 
14588         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
14589         //  List items of map clauses in the same construct must not share
14590         //  original storage.
14591         //
14592         // If the expressions are exactly the same or one is a subset of the
14593         // other, it means they are sharing storage.
14594         if (CI == CE && SI == SE) {
14595           if (CurrentRegionOnly) {
14596             if (CKind == OMPC_map) {
14597               SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
14598             } else {
14599               assert(CKind == OMPC_to || CKind == OMPC_from);
14600               SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
14601                   << ERange;
14602             }
14603             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
14604                 << RE->getSourceRange();
14605             return true;
14606           }
14607           // If we find the same expression in the enclosing data environment,
14608           // that is legal.
14609           IsEnclosedByDataEnvironmentExpr = true;
14610           return false;
14611         }
14612 
14613         QualType DerivedType =
14614             std::prev(CI)->getAssociatedDeclaration()->getType();
14615         SourceLocation DerivedLoc =
14616             std::prev(CI)->getAssociatedExpression()->getExprLoc();
14617 
14618         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
14619         //  If the type of a list item is a reference to a type T then the type
14620         //  will be considered to be T for all purposes of this clause.
14621         DerivedType = DerivedType.getNonReferenceType();
14622 
14623         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
14624         //  A variable for which the type is pointer and an array section
14625         //  derived from that variable must not appear as list items of map
14626         //  clauses of the same construct.
14627         //
14628         // Also, cover one of the cases in:
14629         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
14630         //  If any part of the original storage of a list item has corresponding
14631         //  storage in the device data environment, all of the original storage
14632         //  must have corresponding storage in the device data environment.
14633         //
14634         if (DerivedType->isAnyPointerType()) {
14635           if (CI == CE || SI == SE) {
14636             SemaRef.Diag(
14637                 DerivedLoc,
14638                 diag::err_omp_pointer_mapped_along_with_derived_section)
14639                 << DerivedLoc;
14640             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
14641                 << RE->getSourceRange();
14642             return true;
14643           }
14644           if (CI->getAssociatedExpression()->getStmtClass() !=
14645                          SI->getAssociatedExpression()->getStmtClass() ||
14646                      CI->getAssociatedDeclaration()->getCanonicalDecl() ==
14647                          SI->getAssociatedDeclaration()->getCanonicalDecl()) {
14648             assert(CI != CE && SI != SE);
14649             SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced)
14650                 << DerivedLoc;
14651             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
14652                 << RE->getSourceRange();
14653             return true;
14654           }
14655         }
14656 
14657         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
14658         //  List items of map clauses in the same construct must not share
14659         //  original storage.
14660         //
14661         // An expression is a subset of the other.
14662         if (CurrentRegionOnly && (CI == CE || SI == SE)) {
14663           if (CKind == OMPC_map) {
14664             if (CI != CE || SI != SE) {
14665               // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is
14666               // a pointer.
14667               auto Begin =
14668                   CI != CE ? CurComponents.begin() : StackComponents.begin();
14669               auto End = CI != CE ? CurComponents.end() : StackComponents.end();
14670               auto It = Begin;
14671               while (It != End && !It->getAssociatedDeclaration())
14672                 std::advance(It, 1);
14673               assert(It != End &&
14674                      "Expected at least one component with the declaration.");
14675               if (It != Begin && It->getAssociatedDeclaration()
14676                                      ->getType()
14677                                      .getCanonicalType()
14678                                      ->isAnyPointerType()) {
14679                 IsEnclosedByDataEnvironmentExpr = false;
14680                 EnclosingExpr = nullptr;
14681                 return false;
14682               }
14683             }
14684             SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
14685           } else {
14686             assert(CKind == OMPC_to || CKind == OMPC_from);
14687             SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
14688                 << ERange;
14689           }
14690           SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
14691               << RE->getSourceRange();
14692           return true;
14693         }
14694 
14695         // The current expression uses the same base as other expression in the
14696         // data environment but does not contain it completely.
14697         if (!CurrentRegionOnly && SI != SE)
14698           EnclosingExpr = RE;
14699 
14700         // The current expression is a subset of the expression in the data
14701         // environment.
14702         IsEnclosedByDataEnvironmentExpr |=
14703             (!CurrentRegionOnly && CI != CE && SI == SE);
14704 
14705         return false;
14706       });
14707 
14708   if (CurrentRegionOnly)
14709     return FoundError;
14710 
14711   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
14712   //  If any part of the original storage of a list item has corresponding
14713   //  storage in the device data environment, all of the original storage must
14714   //  have corresponding storage in the device data environment.
14715   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
14716   //  If a list item is an element of a structure, and a different element of
14717   //  the structure has a corresponding list item in the device data environment
14718   //  prior to a task encountering the construct associated with the map clause,
14719   //  then the list item must also have a corresponding list item in the device
14720   //  data environment prior to the task encountering the construct.
14721   //
14722   if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
14723     SemaRef.Diag(ELoc,
14724                  diag::err_omp_original_storage_is_shared_and_does_not_contain)
14725         << ERange;
14726     SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
14727         << EnclosingExpr->getSourceRange();
14728     return true;
14729   }
14730 
14731   return FoundError;
14732 }
14733 
14734 // Look up the user-defined mapper given the mapper name and mapped type, and
14735 // build a reference to it.
14736 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S,
14737                                             CXXScopeSpec &MapperIdScopeSpec,
14738                                             const DeclarationNameInfo &MapperId,
14739                                             QualType Type,
14740                                             Expr *UnresolvedMapper) {
14741   if (MapperIdScopeSpec.isInvalid())
14742     return ExprError();
14743   // Find all user-defined mappers with the given MapperId.
14744   SmallVector<UnresolvedSet<8>, 4> Lookups;
14745   LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName);
14746   Lookup.suppressDiagnostics();
14747   if (S) {
14748     while (S && SemaRef.LookupParsedName(Lookup, S, &MapperIdScopeSpec)) {
14749       NamedDecl *D = Lookup.getRepresentativeDecl();
14750       while (S && !S->isDeclScope(D))
14751         S = S->getParent();
14752       if (S)
14753         S = S->getParent();
14754       Lookups.emplace_back();
14755       Lookups.back().append(Lookup.begin(), Lookup.end());
14756       Lookup.clear();
14757     }
14758   } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(UnresolvedMapper)) {
14759     // Extract the user-defined mappers with the given MapperId.
14760     Lookups.push_back(UnresolvedSet<8>());
14761     for (NamedDecl *D : ULE->decls()) {
14762       auto *DMD = cast<OMPDeclareMapperDecl>(D);
14763       assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation.");
14764       Lookups.back().addDecl(DMD);
14765     }
14766   }
14767   // Defer the lookup for dependent types. The results will be passed through
14768   // UnresolvedMapper on instantiation.
14769   if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() ||
14770       Type->isInstantiationDependentType() ||
14771       Type->containsUnexpandedParameterPack() ||
14772       filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) {
14773         return !D->isInvalidDecl() &&
14774                (D->getType()->isDependentType() ||
14775                 D->getType()->isInstantiationDependentType() ||
14776                 D->getType()->containsUnexpandedParameterPack());
14777       })) {
14778     UnresolvedSet<8> URS;
14779     for (const UnresolvedSet<8> &Set : Lookups) {
14780       if (Set.empty())
14781         continue;
14782       URS.append(Set.begin(), Set.end());
14783     }
14784     return UnresolvedLookupExpr::Create(
14785         SemaRef.Context, /*NamingClass=*/nullptr,
14786         MapperIdScopeSpec.getWithLocInContext(SemaRef.Context), MapperId,
14787         /*ADL=*/false, /*Overloaded=*/true, URS.begin(), URS.end());
14788   }
14789   // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
14790   //  The type must be of struct, union or class type in C and C++
14791   if (!Type->isStructureOrClassType() && !Type->isUnionType())
14792     return ExprEmpty();
14793   SourceLocation Loc = MapperId.getLoc();
14794   // Perform argument dependent lookup.
14795   if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet())
14796     argumentDependentLookup(SemaRef, MapperId, Loc, Type, Lookups);
14797   // Return the first user-defined mapper with the desired type.
14798   if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
14799           Lookups, [&SemaRef, Type](ValueDecl *D) -> ValueDecl * {
14800             if (!D->isInvalidDecl() &&
14801                 SemaRef.Context.hasSameType(D->getType(), Type))
14802               return D;
14803             return nullptr;
14804           }))
14805     return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc);
14806   // Find the first user-defined mapper with a type derived from the desired
14807   // type.
14808   if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
14809           Lookups, [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * {
14810             if (!D->isInvalidDecl() &&
14811                 SemaRef.IsDerivedFrom(Loc, Type, D->getType()) &&
14812                 !Type.isMoreQualifiedThan(D->getType()))
14813               return D;
14814             return nullptr;
14815           })) {
14816     CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
14817                        /*DetectVirtual=*/false);
14818     if (SemaRef.IsDerivedFrom(Loc, Type, VD->getType(), Paths)) {
14819       if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
14820               VD->getType().getUnqualifiedType()))) {
14821         if (SemaRef.CheckBaseClassAccess(
14822                 Loc, VD->getType(), Type, Paths.front(),
14823                 /*DiagID=*/0) != Sema::AR_inaccessible) {
14824           return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc);
14825         }
14826       }
14827     }
14828   }
14829   // Report error if a mapper is specified, but cannot be found.
14830   if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") {
14831     SemaRef.Diag(Loc, diag::err_omp_invalid_mapper)
14832         << Type << MapperId.getName();
14833     return ExprError();
14834   }
14835   return ExprEmpty();
14836 }
14837 
14838 namespace {
14839 // Utility struct that gathers all the related lists associated with a mappable
14840 // expression.
14841 struct MappableVarListInfo {
14842   // The list of expressions.
14843   ArrayRef<Expr *> VarList;
14844   // The list of processed expressions.
14845   SmallVector<Expr *, 16> ProcessedVarList;
14846   // The mappble components for each expression.
14847   OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
14848   // The base declaration of the variable.
14849   SmallVector<ValueDecl *, 16> VarBaseDeclarations;
14850   // The reference to the user-defined mapper associated with every expression.
14851   SmallVector<Expr *, 16> UDMapperList;
14852 
14853   MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
14854     // We have a list of components and base declarations for each entry in the
14855     // variable list.
14856     VarComponents.reserve(VarList.size());
14857     VarBaseDeclarations.reserve(VarList.size());
14858   }
14859 };
14860 }
14861 
14862 // Check the validity of the provided variable list for the provided clause kind
14863 // \a CKind. In the check process the valid expressions, mappable expression
14864 // components, variables, and user-defined mappers are extracted and used to
14865 // fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a
14866 // UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec,
14867 // and \a MapperId are expected to be valid if the clause kind is 'map'.
14868 static void checkMappableExpressionList(
14869     Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind,
14870     MappableVarListInfo &MVLI, SourceLocation StartLoc,
14871     CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId,
14872     ArrayRef<Expr *> UnresolvedMappers,
14873     OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
14874     bool IsMapTypeImplicit = false) {
14875   // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
14876   assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
14877          "Unexpected clause kind with mappable expressions!");
14878 
14879   // If the identifier of user-defined mapper is not specified, it is "default".
14880   // We do not change the actual name in this clause to distinguish whether a
14881   // mapper is specified explicitly, i.e., it is not explicitly specified when
14882   // MapperId.getName() is empty.
14883   if (!MapperId.getName() || MapperId.getName().isEmpty()) {
14884     auto &DeclNames = SemaRef.getASTContext().DeclarationNames;
14885     MapperId.setName(DeclNames.getIdentifier(
14886         &SemaRef.getASTContext().Idents.get("default")));
14887   }
14888 
14889   // Iterators to find the current unresolved mapper expression.
14890   auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end();
14891   bool UpdateUMIt = false;
14892   Expr *UnresolvedMapper = nullptr;
14893 
14894   // Keep track of the mappable components and base declarations in this clause.
14895   // Each entry in the list is going to have a list of components associated. We
14896   // record each set of the components so that we can build the clause later on.
14897   // In the end we should have the same amount of declarations and component
14898   // lists.
14899 
14900   for (Expr *RE : MVLI.VarList) {
14901     assert(RE && "Null expr in omp to/from/map clause");
14902     SourceLocation ELoc = RE->getExprLoc();
14903 
14904     // Find the current unresolved mapper expression.
14905     if (UpdateUMIt && UMIt != UMEnd) {
14906       UMIt++;
14907       assert(
14908           UMIt != UMEnd &&
14909           "Expect the size of UnresolvedMappers to match with that of VarList");
14910     }
14911     UpdateUMIt = true;
14912     if (UMIt != UMEnd)
14913       UnresolvedMapper = *UMIt;
14914 
14915     const Expr *VE = RE->IgnoreParenLValueCasts();
14916 
14917     if (VE->isValueDependent() || VE->isTypeDependent() ||
14918         VE->isInstantiationDependent() ||
14919         VE->containsUnexpandedParameterPack()) {
14920       // Try to find the associated user-defined mapper.
14921       ExprResult ER = buildUserDefinedMapperRef(
14922           SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
14923           VE->getType().getCanonicalType(), UnresolvedMapper);
14924       if (ER.isInvalid())
14925         continue;
14926       MVLI.UDMapperList.push_back(ER.get());
14927       // We can only analyze this information once the missing information is
14928       // resolved.
14929       MVLI.ProcessedVarList.push_back(RE);
14930       continue;
14931     }
14932 
14933     Expr *SimpleExpr = RE->IgnoreParenCasts();
14934 
14935     if (!RE->IgnoreParenImpCasts()->isLValue()) {
14936       SemaRef.Diag(ELoc,
14937                    diag::err_omp_expected_named_var_member_or_array_expression)
14938           << RE->getSourceRange();
14939       continue;
14940     }
14941 
14942     OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
14943     ValueDecl *CurDeclaration = nullptr;
14944 
14945     // Obtain the array or member expression bases if required. Also, fill the
14946     // components array with all the components identified in the process.
14947     const Expr *BE = checkMapClauseExpressionBase(
14948         SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false);
14949     if (!BE)
14950       continue;
14951 
14952     assert(!CurComponents.empty() &&
14953            "Invalid mappable expression information.");
14954 
14955     if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) {
14956       // Add store "this" pointer to class in DSAStackTy for future checking
14957       DSAS->addMappedClassesQualTypes(TE->getType());
14958       // Try to find the associated user-defined mapper.
14959       ExprResult ER = buildUserDefinedMapperRef(
14960           SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
14961           VE->getType().getCanonicalType(), UnresolvedMapper);
14962       if (ER.isInvalid())
14963         continue;
14964       MVLI.UDMapperList.push_back(ER.get());
14965       // Skip restriction checking for variable or field declarations
14966       MVLI.ProcessedVarList.push_back(RE);
14967       MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
14968       MVLI.VarComponents.back().append(CurComponents.begin(),
14969                                        CurComponents.end());
14970       MVLI.VarBaseDeclarations.push_back(nullptr);
14971       continue;
14972     }
14973 
14974     // For the following checks, we rely on the base declaration which is
14975     // expected to be associated with the last component. The declaration is
14976     // expected to be a variable or a field (if 'this' is being mapped).
14977     CurDeclaration = CurComponents.back().getAssociatedDeclaration();
14978     assert(CurDeclaration && "Null decl on map clause.");
14979     assert(
14980         CurDeclaration->isCanonicalDecl() &&
14981         "Expecting components to have associated only canonical declarations.");
14982 
14983     auto *VD = dyn_cast<VarDecl>(CurDeclaration);
14984     const auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
14985 
14986     assert((VD || FD) && "Only variables or fields are expected here!");
14987     (void)FD;
14988 
14989     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
14990     // threadprivate variables cannot appear in a map clause.
14991     // OpenMP 4.5 [2.10.5, target update Construct]
14992     // threadprivate variables cannot appear in a from clause.
14993     if (VD && DSAS->isThreadPrivate(VD)) {
14994       DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
14995       SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
14996           << getOpenMPClauseName(CKind);
14997       reportOriginalDsa(SemaRef, DSAS, VD, DVar);
14998       continue;
14999     }
15000 
15001     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
15002     //  A list item cannot appear in both a map clause and a data-sharing
15003     //  attribute clause on the same construct.
15004 
15005     // Check conflicts with other map clause expressions. We check the conflicts
15006     // with the current construct separately from the enclosing data
15007     // environment, because the restrictions are different. We only have to
15008     // check conflicts across regions for the map clauses.
15009     if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
15010                           /*CurrentRegionOnly=*/true, CurComponents, CKind))
15011       break;
15012     if (CKind == OMPC_map &&
15013         checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
15014                           /*CurrentRegionOnly=*/false, CurComponents, CKind))
15015       break;
15016 
15017     // OpenMP 4.5 [2.10.5, target update Construct]
15018     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
15019     //  If the type of a list item is a reference to a type T then the type will
15020     //  be considered to be T for all purposes of this clause.
15021     auto I = llvm::find_if(
15022         CurComponents,
15023         [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
15024           return MC.getAssociatedDeclaration();
15025         });
15026     assert(I != CurComponents.end() && "Null decl on map clause.");
15027     QualType Type =
15028         I->getAssociatedDeclaration()->getType().getNonReferenceType();
15029 
15030     // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
15031     // A list item in a to or from clause must have a mappable type.
15032     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
15033     //  A list item must have a mappable type.
15034     if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
15035                            DSAS, Type))
15036       continue;
15037 
15038     if (CKind == OMPC_map) {
15039       // target enter data
15040       // OpenMP [2.10.2, Restrictions, p. 99]
15041       // A map-type must be specified in all map clauses and must be either
15042       // to or alloc.
15043       OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
15044       if (DKind == OMPD_target_enter_data &&
15045           !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
15046         SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
15047             << (IsMapTypeImplicit ? 1 : 0)
15048             << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
15049             << getOpenMPDirectiveName(DKind);
15050         continue;
15051       }
15052 
15053       // target exit_data
15054       // OpenMP [2.10.3, Restrictions, p. 102]
15055       // A map-type must be specified in all map clauses and must be either
15056       // from, release, or delete.
15057       if (DKind == OMPD_target_exit_data &&
15058           !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
15059             MapType == OMPC_MAP_delete)) {
15060         SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
15061             << (IsMapTypeImplicit ? 1 : 0)
15062             << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
15063             << getOpenMPDirectiveName(DKind);
15064         continue;
15065       }
15066 
15067       // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
15068       // A list item cannot appear in both a map clause and a data-sharing
15069       // attribute clause on the same construct
15070       //
15071       // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
15072       // A list item cannot appear in both a map clause and a data-sharing
15073       // attribute clause on the same construct unless the construct is a
15074       // combined construct.
15075       if (VD && ((SemaRef.LangOpts.OpenMP <= 45 &&
15076                   isOpenMPTargetExecutionDirective(DKind)) ||
15077                  DKind == OMPD_target)) {
15078         DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
15079         if (isOpenMPPrivate(DVar.CKind)) {
15080           SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
15081               << getOpenMPClauseName(DVar.CKind)
15082               << getOpenMPClauseName(OMPC_map)
15083               << getOpenMPDirectiveName(DSAS->getCurrentDirective());
15084           reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar);
15085           continue;
15086         }
15087       }
15088     }
15089 
15090     // Try to find the associated user-defined mapper.
15091     ExprResult ER = buildUserDefinedMapperRef(
15092         SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
15093         Type.getCanonicalType(), UnresolvedMapper);
15094     if (ER.isInvalid())
15095       continue;
15096     MVLI.UDMapperList.push_back(ER.get());
15097 
15098     // Save the current expression.
15099     MVLI.ProcessedVarList.push_back(RE);
15100 
15101     // Store the components in the stack so that they can be used to check
15102     // against other clauses later on.
15103     DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
15104                                           /*WhereFoundClauseKind=*/OMPC_map);
15105 
15106     // Save the components and declaration to create the clause. For purposes of
15107     // the clause creation, any component list that has has base 'this' uses
15108     // null as base declaration.
15109     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
15110     MVLI.VarComponents.back().append(CurComponents.begin(),
15111                                      CurComponents.end());
15112     MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
15113                                                            : CurDeclaration);
15114   }
15115 }
15116 
15117 OMPClause *Sema::ActOnOpenMPMapClause(
15118     ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
15119     ArrayRef<SourceLocation> MapTypeModifiersLoc,
15120     CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId,
15121     OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc,
15122     SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
15123     const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) {
15124   OpenMPMapModifierKind Modifiers[] = {OMPC_MAP_MODIFIER_unknown,
15125                                        OMPC_MAP_MODIFIER_unknown,
15126                                        OMPC_MAP_MODIFIER_unknown};
15127   SourceLocation ModifiersLoc[OMPMapClause::NumberOfModifiers];
15128 
15129   // Process map-type-modifiers, flag errors for duplicate modifiers.
15130   unsigned Count = 0;
15131   for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) {
15132     if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown &&
15133         llvm::find(Modifiers, MapTypeModifiers[I]) != std::end(Modifiers)) {
15134       Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier);
15135       continue;
15136     }
15137     assert(Count < OMPMapClause::NumberOfModifiers &&
15138            "Modifiers exceed the allowed number of map type modifiers");
15139     Modifiers[Count] = MapTypeModifiers[I];
15140     ModifiersLoc[Count] = MapTypeModifiersLoc[I];
15141     ++Count;
15142   }
15143 
15144   MappableVarListInfo MVLI(VarList);
15145   checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, Locs.StartLoc,
15146                               MapperIdScopeSpec, MapperId, UnresolvedMappers,
15147                               MapType, IsMapTypeImplicit);
15148 
15149   // We need to produce a map clause even if we don't have variables so that
15150   // other diagnostics related with non-existing map clauses are accurate.
15151   return OMPMapClause::Create(Context, Locs, MVLI.ProcessedVarList,
15152                               MVLI.VarBaseDeclarations, MVLI.VarComponents,
15153                               MVLI.UDMapperList, Modifiers, ModifiersLoc,
15154                               MapperIdScopeSpec.getWithLocInContext(Context),
15155                               MapperId, MapType, IsMapTypeImplicit, MapLoc);
15156 }
15157 
15158 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
15159                                                TypeResult ParsedType) {
15160   assert(ParsedType.isUsable());
15161 
15162   QualType ReductionType = GetTypeFromParser(ParsedType.get());
15163   if (ReductionType.isNull())
15164     return QualType();
15165 
15166   // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
15167   // A type name in a declare reduction directive cannot be a function type, an
15168   // array type, a reference type, or a type qualified with const, volatile or
15169   // restrict.
15170   if (ReductionType.hasQualifiers()) {
15171     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
15172     return QualType();
15173   }
15174 
15175   if (ReductionType->isFunctionType()) {
15176     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
15177     return QualType();
15178   }
15179   if (ReductionType->isReferenceType()) {
15180     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
15181     return QualType();
15182   }
15183   if (ReductionType->isArrayType()) {
15184     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
15185     return QualType();
15186   }
15187   return ReductionType;
15188 }
15189 
15190 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
15191     Scope *S, DeclContext *DC, DeclarationName Name,
15192     ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
15193     AccessSpecifier AS, Decl *PrevDeclInScope) {
15194   SmallVector<Decl *, 8> Decls;
15195   Decls.reserve(ReductionTypes.size());
15196 
15197   LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
15198                       forRedeclarationInCurContext());
15199   // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
15200   // A reduction-identifier may not be re-declared in the current scope for the
15201   // same type or for a type that is compatible according to the base language
15202   // rules.
15203   llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
15204   OMPDeclareReductionDecl *PrevDRD = nullptr;
15205   bool InCompoundScope = true;
15206   if (S != nullptr) {
15207     // Find previous declaration with the same name not referenced in other
15208     // declarations.
15209     FunctionScopeInfo *ParentFn = getEnclosingFunction();
15210     InCompoundScope =
15211         (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
15212     LookupName(Lookup, S);
15213     FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
15214                          /*AllowInlineNamespace=*/false);
15215     llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
15216     LookupResult::Filter Filter = Lookup.makeFilter();
15217     while (Filter.hasNext()) {
15218       auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
15219       if (InCompoundScope) {
15220         auto I = UsedAsPrevious.find(PrevDecl);
15221         if (I == UsedAsPrevious.end())
15222           UsedAsPrevious[PrevDecl] = false;
15223         if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
15224           UsedAsPrevious[D] = true;
15225       }
15226       PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
15227           PrevDecl->getLocation();
15228     }
15229     Filter.done();
15230     if (InCompoundScope) {
15231       for (const auto &PrevData : UsedAsPrevious) {
15232         if (!PrevData.second) {
15233           PrevDRD = PrevData.first;
15234           break;
15235         }
15236       }
15237     }
15238   } else if (PrevDeclInScope != nullptr) {
15239     auto *PrevDRDInScope = PrevDRD =
15240         cast<OMPDeclareReductionDecl>(PrevDeclInScope);
15241     do {
15242       PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
15243           PrevDRDInScope->getLocation();
15244       PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
15245     } while (PrevDRDInScope != nullptr);
15246   }
15247   for (const auto &TyData : ReductionTypes) {
15248     const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
15249     bool Invalid = false;
15250     if (I != PreviousRedeclTypes.end()) {
15251       Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
15252           << TyData.first;
15253       Diag(I->second, diag::note_previous_definition);
15254       Invalid = true;
15255     }
15256     PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
15257     auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
15258                                                 Name, TyData.first, PrevDRD);
15259     DC->addDecl(DRD);
15260     DRD->setAccess(AS);
15261     Decls.push_back(DRD);
15262     if (Invalid)
15263       DRD->setInvalidDecl();
15264     else
15265       PrevDRD = DRD;
15266   }
15267 
15268   return DeclGroupPtrTy::make(
15269       DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
15270 }
15271 
15272 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
15273   auto *DRD = cast<OMPDeclareReductionDecl>(D);
15274 
15275   // Enter new function scope.
15276   PushFunctionScope();
15277   setFunctionHasBranchProtectedScope();
15278   getCurFunction()->setHasOMPDeclareReductionCombiner();
15279 
15280   if (S != nullptr)
15281     PushDeclContext(S, DRD);
15282   else
15283     CurContext = DRD;
15284 
15285   PushExpressionEvaluationContext(
15286       ExpressionEvaluationContext::PotentiallyEvaluated);
15287 
15288   QualType ReductionType = DRD->getType();
15289   // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
15290   // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
15291   // uses semantics of argument handles by value, but it should be passed by
15292   // reference. C lang does not support references, so pass all parameters as
15293   // pointers.
15294   // Create 'T omp_in;' variable.
15295   VarDecl *OmpInParm =
15296       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
15297   // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
15298   // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
15299   // uses semantics of argument handles by value, but it should be passed by
15300   // reference. C lang does not support references, so pass all parameters as
15301   // pointers.
15302   // Create 'T omp_out;' variable.
15303   VarDecl *OmpOutParm =
15304       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
15305   if (S != nullptr) {
15306     PushOnScopeChains(OmpInParm, S);
15307     PushOnScopeChains(OmpOutParm, S);
15308   } else {
15309     DRD->addDecl(OmpInParm);
15310     DRD->addDecl(OmpOutParm);
15311   }
15312   Expr *InE =
15313       ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation());
15314   Expr *OutE =
15315       ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation());
15316   DRD->setCombinerData(InE, OutE);
15317 }
15318 
15319 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
15320   auto *DRD = cast<OMPDeclareReductionDecl>(D);
15321   DiscardCleanupsInEvaluationContext();
15322   PopExpressionEvaluationContext();
15323 
15324   PopDeclContext();
15325   PopFunctionScopeInfo();
15326 
15327   if (Combiner != nullptr)
15328     DRD->setCombiner(Combiner);
15329   else
15330     DRD->setInvalidDecl();
15331 }
15332 
15333 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
15334   auto *DRD = cast<OMPDeclareReductionDecl>(D);
15335 
15336   // Enter new function scope.
15337   PushFunctionScope();
15338   setFunctionHasBranchProtectedScope();
15339 
15340   if (S != nullptr)
15341     PushDeclContext(S, DRD);
15342   else
15343     CurContext = DRD;
15344 
15345   PushExpressionEvaluationContext(
15346       ExpressionEvaluationContext::PotentiallyEvaluated);
15347 
15348   QualType ReductionType = DRD->getType();
15349   // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
15350   // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
15351   // uses semantics of argument handles by value, but it should be passed by
15352   // reference. C lang does not support references, so pass all parameters as
15353   // pointers.
15354   // Create 'T omp_priv;' variable.
15355   VarDecl *OmpPrivParm =
15356       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
15357   // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
15358   // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
15359   // uses semantics of argument handles by value, but it should be passed by
15360   // reference. C lang does not support references, so pass all parameters as
15361   // pointers.
15362   // Create 'T omp_orig;' variable.
15363   VarDecl *OmpOrigParm =
15364       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
15365   if (S != nullptr) {
15366     PushOnScopeChains(OmpPrivParm, S);
15367     PushOnScopeChains(OmpOrigParm, S);
15368   } else {
15369     DRD->addDecl(OmpPrivParm);
15370     DRD->addDecl(OmpOrigParm);
15371   }
15372   Expr *OrigE =
15373       ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation());
15374   Expr *PrivE =
15375       ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation());
15376   DRD->setInitializerData(OrigE, PrivE);
15377   return OmpPrivParm;
15378 }
15379 
15380 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
15381                                                      VarDecl *OmpPrivParm) {
15382   auto *DRD = cast<OMPDeclareReductionDecl>(D);
15383   DiscardCleanupsInEvaluationContext();
15384   PopExpressionEvaluationContext();
15385 
15386   PopDeclContext();
15387   PopFunctionScopeInfo();
15388 
15389   if (Initializer != nullptr) {
15390     DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
15391   } else if (OmpPrivParm->hasInit()) {
15392     DRD->setInitializer(OmpPrivParm->getInit(),
15393                         OmpPrivParm->isDirectInit()
15394                             ? OMPDeclareReductionDecl::DirectInit
15395                             : OMPDeclareReductionDecl::CopyInit);
15396   } else {
15397     DRD->setInvalidDecl();
15398   }
15399 }
15400 
15401 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
15402     Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
15403   for (Decl *D : DeclReductions.get()) {
15404     if (IsValid) {
15405       if (S)
15406         PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S,
15407                           /*AddToContext=*/false);
15408     } else {
15409       D->setInvalidDecl();
15410     }
15411   }
15412   return DeclReductions;
15413 }
15414 
15415 TypeResult Sema::ActOnOpenMPDeclareMapperVarDecl(Scope *S, Declarator &D) {
15416   TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
15417   QualType T = TInfo->getType();
15418   if (D.isInvalidType())
15419     return true;
15420 
15421   if (getLangOpts().CPlusPlus) {
15422     // Check that there are no default arguments (C++ only).
15423     CheckExtraCXXDefaultArguments(D);
15424   }
15425 
15426   return CreateParsedType(T, TInfo);
15427 }
15428 
15429 QualType Sema::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc,
15430                                             TypeResult ParsedType) {
15431   assert(ParsedType.isUsable() && "Expect usable parsed mapper type");
15432 
15433   QualType MapperType = GetTypeFromParser(ParsedType.get());
15434   assert(!MapperType.isNull() && "Expect valid mapper type");
15435 
15436   // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
15437   //  The type must be of struct, union or class type in C and C++
15438   if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) {
15439     Diag(TyLoc, diag::err_omp_mapper_wrong_type);
15440     return QualType();
15441   }
15442   return MapperType;
15443 }
15444 
15445 OMPDeclareMapperDecl *Sema::ActOnOpenMPDeclareMapperDirectiveStart(
15446     Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType,
15447     SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS,
15448     Decl *PrevDeclInScope) {
15449   LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPMapperName,
15450                       forRedeclarationInCurContext());
15451   // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
15452   //  A mapper-identifier may not be redeclared in the current scope for the
15453   //  same type or for a type that is compatible according to the base language
15454   //  rules.
15455   llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
15456   OMPDeclareMapperDecl *PrevDMD = nullptr;
15457   bool InCompoundScope = true;
15458   if (S != nullptr) {
15459     // Find previous declaration with the same name not referenced in other
15460     // declarations.
15461     FunctionScopeInfo *ParentFn = getEnclosingFunction();
15462     InCompoundScope =
15463         (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
15464     LookupName(Lookup, S);
15465     FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
15466                          /*AllowInlineNamespace=*/false);
15467     llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious;
15468     LookupResult::Filter Filter = Lookup.makeFilter();
15469     while (Filter.hasNext()) {
15470       auto *PrevDecl = cast<OMPDeclareMapperDecl>(Filter.next());
15471       if (InCompoundScope) {
15472         auto I = UsedAsPrevious.find(PrevDecl);
15473         if (I == UsedAsPrevious.end())
15474           UsedAsPrevious[PrevDecl] = false;
15475         if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope())
15476           UsedAsPrevious[D] = true;
15477       }
15478       PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
15479           PrevDecl->getLocation();
15480     }
15481     Filter.done();
15482     if (InCompoundScope) {
15483       for (const auto &PrevData : UsedAsPrevious) {
15484         if (!PrevData.second) {
15485           PrevDMD = PrevData.first;
15486           break;
15487         }
15488       }
15489     }
15490   } else if (PrevDeclInScope) {
15491     auto *PrevDMDInScope = PrevDMD =
15492         cast<OMPDeclareMapperDecl>(PrevDeclInScope);
15493     do {
15494       PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] =
15495           PrevDMDInScope->getLocation();
15496       PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope();
15497     } while (PrevDMDInScope != nullptr);
15498   }
15499   const auto I = PreviousRedeclTypes.find(MapperType.getCanonicalType());
15500   bool Invalid = false;
15501   if (I != PreviousRedeclTypes.end()) {
15502     Diag(StartLoc, diag::err_omp_declare_mapper_redefinition)
15503         << MapperType << Name;
15504     Diag(I->second, diag::note_previous_definition);
15505     Invalid = true;
15506   }
15507   auto *DMD = OMPDeclareMapperDecl::Create(Context, DC, StartLoc, Name,
15508                                            MapperType, VN, PrevDMD);
15509   DC->addDecl(DMD);
15510   DMD->setAccess(AS);
15511   if (Invalid)
15512     DMD->setInvalidDecl();
15513 
15514   // Enter new function scope.
15515   PushFunctionScope();
15516   setFunctionHasBranchProtectedScope();
15517 
15518   CurContext = DMD;
15519 
15520   return DMD;
15521 }
15522 
15523 void Sema::ActOnOpenMPDeclareMapperDirectiveVarDecl(OMPDeclareMapperDecl *DMD,
15524                                                     Scope *S,
15525                                                     QualType MapperType,
15526                                                     SourceLocation StartLoc,
15527                                                     DeclarationName VN) {
15528   VarDecl *VD = buildVarDecl(*this, StartLoc, MapperType, VN.getAsString());
15529   if (S)
15530     PushOnScopeChains(VD, S);
15531   else
15532     DMD->addDecl(VD);
15533   Expr *MapperVarRefExpr = buildDeclRefExpr(*this, VD, MapperType, StartLoc);
15534   DMD->setMapperVarRef(MapperVarRefExpr);
15535 }
15536 
15537 Sema::DeclGroupPtrTy
15538 Sema::ActOnOpenMPDeclareMapperDirectiveEnd(OMPDeclareMapperDecl *D, Scope *S,
15539                                            ArrayRef<OMPClause *> ClauseList) {
15540   PopDeclContext();
15541   PopFunctionScopeInfo();
15542 
15543   if (D) {
15544     if (S)
15545       PushOnScopeChains(D, S, /*AddToContext=*/false);
15546     D->CreateClauses(Context, ClauseList);
15547   }
15548 
15549   return DeclGroupPtrTy::make(DeclGroupRef(D));
15550 }
15551 
15552 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
15553                                            SourceLocation StartLoc,
15554                                            SourceLocation LParenLoc,
15555                                            SourceLocation EndLoc) {
15556   Expr *ValExpr = NumTeams;
15557   Stmt *HelperValStmt = nullptr;
15558 
15559   // OpenMP [teams Constrcut, Restrictions]
15560   // The num_teams expression must evaluate to a positive integer value.
15561   if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
15562                                  /*StrictlyPositive=*/true))
15563     return nullptr;
15564 
15565   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
15566   OpenMPDirectiveKind CaptureRegion =
15567       getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams);
15568   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
15569     ValExpr = MakeFullExpr(ValExpr).get();
15570     llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
15571     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
15572     HelperValStmt = buildPreInits(Context, Captures);
15573   }
15574 
15575   return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
15576                                          StartLoc, LParenLoc, EndLoc);
15577 }
15578 
15579 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
15580                                               SourceLocation StartLoc,
15581                                               SourceLocation LParenLoc,
15582                                               SourceLocation EndLoc) {
15583   Expr *ValExpr = ThreadLimit;
15584   Stmt *HelperValStmt = nullptr;
15585 
15586   // OpenMP [teams Constrcut, Restrictions]
15587   // The thread_limit expression must evaluate to a positive integer value.
15588   if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
15589                                  /*StrictlyPositive=*/true))
15590     return nullptr;
15591 
15592   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
15593   OpenMPDirectiveKind CaptureRegion =
15594       getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit);
15595   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
15596     ValExpr = MakeFullExpr(ValExpr).get();
15597     llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
15598     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
15599     HelperValStmt = buildPreInits(Context, Captures);
15600   }
15601 
15602   return new (Context) OMPThreadLimitClause(
15603       ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
15604 }
15605 
15606 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
15607                                            SourceLocation StartLoc,
15608                                            SourceLocation LParenLoc,
15609                                            SourceLocation EndLoc) {
15610   Expr *ValExpr = Priority;
15611 
15612   // OpenMP [2.9.1, task Constrcut]
15613   // The priority-value is a non-negative numerical scalar expression.
15614   if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
15615                                  /*StrictlyPositive=*/false))
15616     return nullptr;
15617 
15618   return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
15619 }
15620 
15621 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
15622                                             SourceLocation StartLoc,
15623                                             SourceLocation LParenLoc,
15624                                             SourceLocation EndLoc) {
15625   Expr *ValExpr = Grainsize;
15626 
15627   // OpenMP [2.9.2, taskloop Constrcut]
15628   // The parameter of the grainsize clause must be a positive integer
15629   // expression.
15630   if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
15631                                  /*StrictlyPositive=*/true))
15632     return nullptr;
15633 
15634   return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
15635 }
15636 
15637 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
15638                                            SourceLocation StartLoc,
15639                                            SourceLocation LParenLoc,
15640                                            SourceLocation EndLoc) {
15641   Expr *ValExpr = NumTasks;
15642 
15643   // OpenMP [2.9.2, taskloop Constrcut]
15644   // The parameter of the num_tasks clause must be a positive integer
15645   // expression.
15646   if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
15647                                  /*StrictlyPositive=*/true))
15648     return nullptr;
15649 
15650   return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
15651 }
15652 
15653 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
15654                                        SourceLocation LParenLoc,
15655                                        SourceLocation EndLoc) {
15656   // OpenMP [2.13.2, critical construct, Description]
15657   // ... where hint-expression is an integer constant expression that evaluates
15658   // to a valid lock hint.
15659   ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
15660   if (HintExpr.isInvalid())
15661     return nullptr;
15662   return new (Context)
15663       OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
15664 }
15665 
15666 OMPClause *Sema::ActOnOpenMPDistScheduleClause(
15667     OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
15668     SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
15669     SourceLocation EndLoc) {
15670   if (Kind == OMPC_DIST_SCHEDULE_unknown) {
15671     std::string Values;
15672     Values += "'";
15673     Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
15674     Values += "'";
15675     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
15676         << Values << getOpenMPClauseName(OMPC_dist_schedule);
15677     return nullptr;
15678   }
15679   Expr *ValExpr = ChunkSize;
15680   Stmt *HelperValStmt = nullptr;
15681   if (ChunkSize) {
15682     if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
15683         !ChunkSize->isInstantiationDependent() &&
15684         !ChunkSize->containsUnexpandedParameterPack()) {
15685       SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
15686       ExprResult Val =
15687           PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
15688       if (Val.isInvalid())
15689         return nullptr;
15690 
15691       ValExpr = Val.get();
15692 
15693       // OpenMP [2.7.1, Restrictions]
15694       //  chunk_size must be a loop invariant integer expression with a positive
15695       //  value.
15696       llvm::APSInt Result;
15697       if (ValExpr->isIntegerConstantExpr(Result, Context)) {
15698         if (Result.isSigned() && !Result.isStrictlyPositive()) {
15699           Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
15700               << "dist_schedule" << ChunkSize->getSourceRange();
15701           return nullptr;
15702         }
15703       } else if (getOpenMPCaptureRegionForClause(
15704                      DSAStack->getCurrentDirective(), OMPC_dist_schedule) !=
15705                      OMPD_unknown &&
15706                  !CurContext->isDependentContext()) {
15707         ValExpr = MakeFullExpr(ValExpr).get();
15708         llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
15709         ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
15710         HelperValStmt = buildPreInits(Context, Captures);
15711       }
15712     }
15713   }
15714 
15715   return new (Context)
15716       OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
15717                             Kind, ValExpr, HelperValStmt);
15718 }
15719 
15720 OMPClause *Sema::ActOnOpenMPDefaultmapClause(
15721     OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
15722     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
15723     SourceLocation KindLoc, SourceLocation EndLoc) {
15724   // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
15725   if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
15726     std::string Value;
15727     SourceLocation Loc;
15728     Value += "'";
15729     if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
15730       Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
15731                                              OMPC_DEFAULTMAP_MODIFIER_tofrom);
15732       Loc = MLoc;
15733     } else {
15734       Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
15735                                              OMPC_DEFAULTMAP_scalar);
15736       Loc = KindLoc;
15737     }
15738     Value += "'";
15739     Diag(Loc, diag::err_omp_unexpected_clause_value)
15740         << Value << getOpenMPClauseName(OMPC_defaultmap);
15741     return nullptr;
15742   }
15743   DSAStack->setDefaultDMAToFromScalar(StartLoc);
15744 
15745   return new (Context)
15746       OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
15747 }
15748 
15749 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
15750   DeclContext *CurLexicalContext = getCurLexicalContext();
15751   if (!CurLexicalContext->isFileContext() &&
15752       !CurLexicalContext->isExternCContext() &&
15753       !CurLexicalContext->isExternCXXContext() &&
15754       !isa<CXXRecordDecl>(CurLexicalContext) &&
15755       !isa<ClassTemplateDecl>(CurLexicalContext) &&
15756       !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
15757       !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
15758     Diag(Loc, diag::err_omp_region_not_file_context);
15759     return false;
15760   }
15761   ++DeclareTargetNestingLevel;
15762   return true;
15763 }
15764 
15765 void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
15766   assert(DeclareTargetNestingLevel > 0 &&
15767          "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
15768   --DeclareTargetNestingLevel;
15769 }
15770 
15771 NamedDecl *
15772 Sema::lookupOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec,
15773                                     const DeclarationNameInfo &Id,
15774                                     NamedDeclSetType &SameDirectiveDecls) {
15775   LookupResult Lookup(*this, Id, LookupOrdinaryName);
15776   LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
15777 
15778   if (Lookup.isAmbiguous())
15779     return nullptr;
15780   Lookup.suppressDiagnostics();
15781 
15782   if (!Lookup.isSingleResult()) {
15783     VarOrFuncDeclFilterCCC CCC(*this);
15784     if (TypoCorrection Corrected =
15785             CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC,
15786                         CTK_ErrorRecovery)) {
15787       diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
15788                                   << Id.getName());
15789       checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
15790       return nullptr;
15791     }
15792 
15793     Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
15794     return nullptr;
15795   }
15796 
15797   NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
15798   if (!isa<VarDecl>(ND) && !isa<FunctionDecl>(ND) &&
15799       !isa<FunctionTemplateDecl>(ND)) {
15800     Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
15801     return nullptr;
15802   }
15803   if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
15804     Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
15805   return ND;
15806 }
15807 
15808 void Sema::ActOnOpenMPDeclareTargetName(
15809     NamedDecl *ND, SourceLocation Loc, OMPDeclareTargetDeclAttr::MapTypeTy MT,
15810     OMPDeclareTargetDeclAttr::DevTypeTy DT) {
15811   assert((isa<VarDecl>(ND) || isa<FunctionDecl>(ND) ||
15812           isa<FunctionTemplateDecl>(ND)) &&
15813          "Expected variable, function or function template.");
15814 
15815   // Diagnose marking after use as it may lead to incorrect diagnosis and
15816   // codegen.
15817   if (LangOpts.OpenMP >= 50 &&
15818       (ND->isUsed(/*CheckUsedAttr=*/false) || ND->isReferenced()))
15819     Diag(Loc, diag::warn_omp_declare_target_after_first_use);
15820 
15821   Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
15822       OMPDeclareTargetDeclAttr::getDeviceType(cast<ValueDecl>(ND));
15823   if (DevTy.hasValue() && *DevTy != DT) {
15824     Diag(Loc, diag::err_omp_device_type_mismatch)
15825         << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(DT)
15826         << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(*DevTy);
15827     return;
15828   }
15829   Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
15830       OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(cast<ValueDecl>(ND));
15831   if (!Res) {
15832     auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT, DT,
15833                                                        SourceRange(Loc, Loc));
15834     ND->addAttr(A);
15835     if (ASTMutationListener *ML = Context.getASTMutationListener())
15836       ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
15837     checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Loc);
15838   } else if (*Res != MT) {
15839     Diag(Loc, diag::err_omp_declare_target_to_and_link) << ND;
15840   }
15841 }
15842 
15843 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
15844                                      Sema &SemaRef, Decl *D) {
15845   if (!D || !isa<VarDecl>(D))
15846     return;
15847   auto *VD = cast<VarDecl>(D);
15848   Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy =
15849       OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
15850   if (SemaRef.LangOpts.OpenMP >= 50 &&
15851       (SemaRef.getCurLambda(/*IgnoreNonLambdaCapturingScope=*/true) ||
15852        SemaRef.getCurBlock() || SemaRef.getCurCapturedRegion()) &&
15853       VD->hasGlobalStorage()) {
15854     llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy =
15855         OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
15856     if (!MapTy || *MapTy != OMPDeclareTargetDeclAttr::MT_To) {
15857       // OpenMP 5.0, 2.12.7 declare target Directive, Restrictions
15858       // If a lambda declaration and definition appears between a
15859       // declare target directive and the matching end declare target
15860       // directive, all variables that are captured by the lambda
15861       // expression must also appear in a to clause.
15862       SemaRef.Diag(VD->getLocation(),
15863                    diag::err_omp_lambda_capture_in_declare_target_not_to);
15864       SemaRef.Diag(SL, diag::note_var_explicitly_captured_here)
15865           << VD << 0 << SR;
15866       return;
15867     }
15868   }
15869   if (MapTy.hasValue())
15870     return;
15871   SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context);
15872   SemaRef.Diag(SL, diag::note_used_here) << SR;
15873 }
15874 
15875 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
15876                                    Sema &SemaRef, DSAStackTy *Stack,
15877                                    ValueDecl *VD) {
15878   return OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) ||
15879          checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(),
15880                            /*FullCheck=*/false);
15881 }
15882 
15883 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
15884                                             SourceLocation IdLoc) {
15885   if (!D || D->isInvalidDecl())
15886     return;
15887   SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
15888   SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
15889   if (auto *VD = dyn_cast<VarDecl>(D)) {
15890     // Only global variables can be marked as declare target.
15891     if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
15892         !VD->isStaticDataMember())
15893       return;
15894     // 2.10.6: threadprivate variable cannot appear in a declare target
15895     // directive.
15896     if (DSAStack->isThreadPrivate(VD)) {
15897       Diag(SL, diag::err_omp_threadprivate_in_target);
15898       reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
15899       return;
15900     }
15901   }
15902   if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
15903     D = FTD->getTemplatedDecl();
15904   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
15905     llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
15906         OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD);
15907     if (IdLoc.isValid() && Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) {
15908       Diag(IdLoc, diag::err_omp_function_in_link_clause);
15909       Diag(FD->getLocation(), diag::note_defined_here) << FD;
15910       return;
15911     }
15912     // Mark the function as must be emitted for the device.
15913     Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
15914         OMPDeclareTargetDeclAttr::getDeviceType(FD);
15915     if (LangOpts.OpenMPIsDevice && Res.hasValue() && IdLoc.isValid() &&
15916         *DevTy != OMPDeclareTargetDeclAttr::DT_Host)
15917       checkOpenMPDeviceFunction(IdLoc, FD, /*CheckForDelayedContext=*/false);
15918     if (!LangOpts.OpenMPIsDevice && Res.hasValue() && IdLoc.isValid() &&
15919         *DevTy != OMPDeclareTargetDeclAttr::DT_NoHost)
15920       checkOpenMPHostFunction(IdLoc, FD, /*CheckCaller=*/false);
15921   }
15922   if (auto *VD = dyn_cast<ValueDecl>(D)) {
15923     // Problem if any with var declared with incomplete type will be reported
15924     // as normal, so no need to check it here.
15925     if ((E || !VD->getType()->isIncompleteType()) &&
15926         !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD))
15927       return;
15928     if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
15929       // Checking declaration inside declare target region.
15930       if (isa<VarDecl>(D) || isa<FunctionDecl>(D) ||
15931           isa<FunctionTemplateDecl>(D)) {
15932         auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
15933             Context, OMPDeclareTargetDeclAttr::MT_To,
15934             OMPDeclareTargetDeclAttr::DT_Any, SourceRange(IdLoc, IdLoc));
15935         D->addAttr(A);
15936         if (ASTMutationListener *ML = Context.getASTMutationListener())
15937           ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
15938       }
15939       return;
15940     }
15941   }
15942   if (!E)
15943     return;
15944   checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
15945 }
15946 
15947 OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
15948                                      CXXScopeSpec &MapperIdScopeSpec,
15949                                      DeclarationNameInfo &MapperId,
15950                                      const OMPVarListLocTy &Locs,
15951                                      ArrayRef<Expr *> UnresolvedMappers) {
15952   MappableVarListInfo MVLI(VarList);
15953   checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, Locs.StartLoc,
15954                               MapperIdScopeSpec, MapperId, UnresolvedMappers);
15955   if (MVLI.ProcessedVarList.empty())
15956     return nullptr;
15957 
15958   return OMPToClause::Create(
15959       Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
15960       MVLI.VarComponents, MVLI.UDMapperList,
15961       MapperIdScopeSpec.getWithLocInContext(Context), MapperId);
15962 }
15963 
15964 OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
15965                                        CXXScopeSpec &MapperIdScopeSpec,
15966                                        DeclarationNameInfo &MapperId,
15967                                        const OMPVarListLocTy &Locs,
15968                                        ArrayRef<Expr *> UnresolvedMappers) {
15969   MappableVarListInfo MVLI(VarList);
15970   checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, Locs.StartLoc,
15971                               MapperIdScopeSpec, MapperId, UnresolvedMappers);
15972   if (MVLI.ProcessedVarList.empty())
15973     return nullptr;
15974 
15975   return OMPFromClause::Create(
15976       Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
15977       MVLI.VarComponents, MVLI.UDMapperList,
15978       MapperIdScopeSpec.getWithLocInContext(Context), MapperId);
15979 }
15980 
15981 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
15982                                                const OMPVarListLocTy &Locs) {
15983   MappableVarListInfo MVLI(VarList);
15984   SmallVector<Expr *, 8> PrivateCopies;
15985   SmallVector<Expr *, 8> Inits;
15986 
15987   for (Expr *RefExpr : VarList) {
15988     assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
15989     SourceLocation ELoc;
15990     SourceRange ERange;
15991     Expr *SimpleRefExpr = RefExpr;
15992     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
15993     if (Res.second) {
15994       // It will be analyzed later.
15995       MVLI.ProcessedVarList.push_back(RefExpr);
15996       PrivateCopies.push_back(nullptr);
15997       Inits.push_back(nullptr);
15998     }
15999     ValueDecl *D = Res.first;
16000     if (!D)
16001       continue;
16002 
16003     QualType Type = D->getType();
16004     Type = Type.getNonReferenceType().getUnqualifiedType();
16005 
16006     auto *VD = dyn_cast<VarDecl>(D);
16007 
16008     // Item should be a pointer or reference to pointer.
16009     if (!Type->isPointerType()) {
16010       Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
16011           << 0 << RefExpr->getSourceRange();
16012       continue;
16013     }
16014 
16015     // Build the private variable and the expression that refers to it.
16016     auto VDPrivate =
16017         buildVarDecl(*this, ELoc, Type, D->getName(),
16018                      D->hasAttrs() ? &D->getAttrs() : nullptr,
16019                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
16020     if (VDPrivate->isInvalidDecl())
16021       continue;
16022 
16023     CurContext->addDecl(VDPrivate);
16024     DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
16025         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
16026 
16027     // Add temporary variable to initialize the private copy of the pointer.
16028     VarDecl *VDInit =
16029         buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
16030     DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
16031         *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc());
16032     AddInitializerToDecl(VDPrivate,
16033                          DefaultLvalueConversion(VDInitRefExpr).get(),
16034                          /*DirectInit=*/false);
16035 
16036     // If required, build a capture to implement the privatization initialized
16037     // with the current list item value.
16038     DeclRefExpr *Ref = nullptr;
16039     if (!VD)
16040       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
16041     MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
16042     PrivateCopies.push_back(VDPrivateRefExpr);
16043     Inits.push_back(VDInitRefExpr);
16044 
16045     // We need to add a data sharing attribute for this variable to make sure it
16046     // is correctly captured. A variable that shows up in a use_device_ptr has
16047     // similar properties of a first private variable.
16048     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
16049 
16050     // Create a mappable component for the list item. List items in this clause
16051     // only need a component.
16052     MVLI.VarBaseDeclarations.push_back(D);
16053     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
16054     MVLI.VarComponents.back().push_back(
16055         OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
16056   }
16057 
16058   if (MVLI.ProcessedVarList.empty())
16059     return nullptr;
16060 
16061   return OMPUseDevicePtrClause::Create(
16062       Context, Locs, MVLI.ProcessedVarList, PrivateCopies, Inits,
16063       MVLI.VarBaseDeclarations, MVLI.VarComponents);
16064 }
16065 
16066 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
16067                                               const OMPVarListLocTy &Locs) {
16068   MappableVarListInfo MVLI(VarList);
16069   for (Expr *RefExpr : VarList) {
16070     assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
16071     SourceLocation ELoc;
16072     SourceRange ERange;
16073     Expr *SimpleRefExpr = RefExpr;
16074     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
16075     if (Res.second) {
16076       // It will be analyzed later.
16077       MVLI.ProcessedVarList.push_back(RefExpr);
16078     }
16079     ValueDecl *D = Res.first;
16080     if (!D)
16081       continue;
16082 
16083     QualType Type = D->getType();
16084     // item should be a pointer or array or reference to pointer or array
16085     if (!Type.getNonReferenceType()->isPointerType() &&
16086         !Type.getNonReferenceType()->isArrayType()) {
16087       Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
16088           << 0 << RefExpr->getSourceRange();
16089       continue;
16090     }
16091 
16092     // Check if the declaration in the clause does not show up in any data
16093     // sharing attribute.
16094     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
16095     if (isOpenMPPrivate(DVar.CKind)) {
16096       Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
16097           << getOpenMPClauseName(DVar.CKind)
16098           << getOpenMPClauseName(OMPC_is_device_ptr)
16099           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
16100       reportOriginalDsa(*this, DSAStack, D, DVar);
16101       continue;
16102     }
16103 
16104     const Expr *ConflictExpr;
16105     if (DSAStack->checkMappableExprComponentListsForDecl(
16106             D, /*CurrentRegionOnly=*/true,
16107             [&ConflictExpr](
16108                 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
16109                 OpenMPClauseKind) -> bool {
16110               ConflictExpr = R.front().getAssociatedExpression();
16111               return true;
16112             })) {
16113       Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
16114       Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
16115           << ConflictExpr->getSourceRange();
16116       continue;
16117     }
16118 
16119     // Store the components in the stack so that they can be used to check
16120     // against other clauses later on.
16121     OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
16122     DSAStack->addMappableExpressionComponents(
16123         D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
16124 
16125     // Record the expression we've just processed.
16126     MVLI.ProcessedVarList.push_back(SimpleRefExpr);
16127 
16128     // Create a mappable component for the list item. List items in this clause
16129     // only need a component. We use a null declaration to signal fields in
16130     // 'this'.
16131     assert((isa<DeclRefExpr>(SimpleRefExpr) ||
16132             isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
16133            "Unexpected device pointer expression!");
16134     MVLI.VarBaseDeclarations.push_back(
16135         isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
16136     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
16137     MVLI.VarComponents.back().push_back(MC);
16138   }
16139 
16140   if (MVLI.ProcessedVarList.empty())
16141     return nullptr;
16142 
16143   return OMPIsDevicePtrClause::Create(Context, Locs, MVLI.ProcessedVarList,
16144                                       MVLI.VarBaseDeclarations,
16145                                       MVLI.VarComponents);
16146 }
16147 
16148 OMPClause *Sema::ActOnOpenMPAllocateClause(
16149     Expr *Allocator, ArrayRef<Expr *> VarList, SourceLocation StartLoc,
16150     SourceLocation ColonLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
16151   if (Allocator) {
16152     // OpenMP [2.11.4 allocate Clause, Description]
16153     // allocator is an expression of omp_allocator_handle_t type.
16154     if (!findOMPAllocatorHandleT(*this, Allocator->getExprLoc(), DSAStack))
16155       return nullptr;
16156 
16157     ExprResult AllocatorRes = DefaultLvalueConversion(Allocator);
16158     if (AllocatorRes.isInvalid())
16159       return nullptr;
16160     AllocatorRes = PerformImplicitConversion(AllocatorRes.get(),
16161                                              DSAStack->getOMPAllocatorHandleT(),
16162                                              Sema::AA_Initializing,
16163                                              /*AllowExplicit=*/true);
16164     if (AllocatorRes.isInvalid())
16165       return nullptr;
16166     Allocator = AllocatorRes.get();
16167   } else {
16168     // OpenMP 5.0, 2.11.4 allocate Clause, Restrictions.
16169     // allocate clauses that appear on a target construct or on constructs in a
16170     // target region must specify an allocator expression unless a requires
16171     // directive with the dynamic_allocators clause is present in the same
16172     // compilation unit.
16173     if (LangOpts.OpenMPIsDevice &&
16174         !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
16175       targetDiag(StartLoc, diag::err_expected_allocator_expression);
16176   }
16177   // Analyze and build list of variables.
16178   SmallVector<Expr *, 8> Vars;
16179   for (Expr *RefExpr : VarList) {
16180     assert(RefExpr && "NULL expr in OpenMP private clause.");
16181     SourceLocation ELoc;
16182     SourceRange ERange;
16183     Expr *SimpleRefExpr = RefExpr;
16184     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
16185     if (Res.second) {
16186       // It will be analyzed later.
16187       Vars.push_back(RefExpr);
16188     }
16189     ValueDecl *D = Res.first;
16190     if (!D)
16191       continue;
16192 
16193     auto *VD = dyn_cast<VarDecl>(D);
16194     DeclRefExpr *Ref = nullptr;
16195     if (!VD && !CurContext->isDependentContext())
16196       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
16197     Vars.push_back((VD || CurContext->isDependentContext())
16198                        ? RefExpr->IgnoreParens()
16199                        : Ref);
16200   }
16201 
16202   if (Vars.empty())
16203     return nullptr;
16204 
16205   return OMPAllocateClause::Create(Context, StartLoc, LParenLoc, Allocator,
16206                                    ColonLoc, EndLoc, Vars);
16207 }
16208