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/DiagnosticSema.h"
26 #include "clang/Basic/OpenMPKinds.h"
27 #include "clang/Basic/PartialDiagnostic.h"
28 #include "clang/Basic/TargetInfo.h"
29 #include "clang/Sema/Initialization.h"
30 #include "clang/Sema/Lookup.h"
31 #include "clang/Sema/Scope.h"
32 #include "clang/Sema/ScopeInfo.h"
33 #include "clang/Sema/SemaInternal.h"
34 #include "llvm/ADT/IndexedMap.h"
35 #include "llvm/ADT/PointerEmbeddedInt.h"
36 #include "llvm/ADT/STLExtras.h"
37 #include "llvm/Frontend/OpenMP/OMPConstants.h"
38 #include <set>
39 
40 using namespace clang;
41 using namespace llvm::omp;
42 
43 //===----------------------------------------------------------------------===//
44 // Stack of data-sharing attributes for variables
45 //===----------------------------------------------------------------------===//
46 
47 static const Expr *checkMapClauseExpressionBase(
48     Sema &SemaRef, Expr *E,
49     OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
50     OpenMPClauseKind CKind, bool NoDiagnose);
51 
52 namespace {
53 /// Default data sharing attributes, which can be applied to directive.
54 enum DefaultDataSharingAttributes {
55   DSA_unspecified = 0, /// Data sharing attribute not specified.
56   DSA_none = 1 << 0,   /// Default data sharing attribute 'none'.
57   DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'.
58 };
59 
60 /// Stack for tracking declarations used in OpenMP directives and
61 /// clauses and their data-sharing attributes.
62 class DSAStackTy {
63 public:
64   struct DSAVarData {
65     OpenMPDirectiveKind DKind = OMPD_unknown;
66     OpenMPClauseKind CKind = OMPC_unknown;
67     unsigned Modifier = 0;
68     const Expr *RefExpr = nullptr;
69     DeclRefExpr *PrivateCopy = nullptr;
70     SourceLocation ImplicitDSALoc;
71     DSAVarData() = default;
72     DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
73                const Expr *RefExpr, DeclRefExpr *PrivateCopy,
74                SourceLocation ImplicitDSALoc, unsigned Modifier)
75         : DKind(DKind), CKind(CKind), Modifier(Modifier), RefExpr(RefExpr),
76           PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc) {}
77   };
78   using OperatorOffsetTy =
79       llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>;
80   using DoacrossDependMapTy =
81       llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>;
82   /// Kind of the declaration used in the uses_allocators clauses.
83   enum class UsesAllocatorsDeclKind {
84     /// Predefined allocator
85     PredefinedAllocator,
86     /// User-defined allocator
87     UserDefinedAllocator,
88     /// The declaration that represent allocator trait
89     AllocatorTrait,
90   };
91 
92 private:
93   struct DSAInfo {
94     OpenMPClauseKind Attributes = OMPC_unknown;
95     unsigned Modifier = 0;
96     /// Pointer to a reference expression and a flag which shows that the
97     /// variable is marked as lastprivate(true) or not (false).
98     llvm::PointerIntPair<const Expr *, 1, bool> RefExpr;
99     DeclRefExpr *PrivateCopy = nullptr;
100   };
101   using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>;
102   using UsedRefMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>;
103   using LCDeclInfo = std::pair<unsigned, VarDecl *>;
104   using LoopControlVariablesMapTy =
105       llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>;
106   /// Struct that associates a component with the clause kind where they are
107   /// found.
108   struct MappedExprComponentTy {
109     OMPClauseMappableExprCommon::MappableExprComponentLists Components;
110     OpenMPClauseKind Kind = OMPC_unknown;
111   };
112   using MappedExprComponentsTy =
113       llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>;
114   using CriticalsWithHintsTy =
115       llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>;
116   struct ReductionData {
117     using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>;
118     SourceRange ReductionRange;
119     llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp;
120     ReductionData() = default;
121     void set(BinaryOperatorKind BO, SourceRange RR) {
122       ReductionRange = RR;
123       ReductionOp = BO;
124     }
125     void set(const Expr *RefExpr, SourceRange RR) {
126       ReductionRange = RR;
127       ReductionOp = RefExpr;
128     }
129   };
130   using DeclReductionMapTy =
131       llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>;
132   struct DefaultmapInfo {
133     OpenMPDefaultmapClauseModifier ImplicitBehavior =
134         OMPC_DEFAULTMAP_MODIFIER_unknown;
135     SourceLocation SLoc;
136     DefaultmapInfo() = default;
137     DefaultmapInfo(OpenMPDefaultmapClauseModifier M, SourceLocation Loc)
138         : ImplicitBehavior(M), SLoc(Loc) {}
139   };
140 
141   struct SharingMapTy {
142     DeclSAMapTy SharingMap;
143     DeclReductionMapTy ReductionMap;
144     UsedRefMapTy AlignedMap;
145     UsedRefMapTy NontemporalMap;
146     MappedExprComponentsTy MappedExprComponents;
147     LoopControlVariablesMapTy LCVMap;
148     DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
149     SourceLocation DefaultAttrLoc;
150     DefaultmapInfo DefaultmapMap[OMPC_DEFAULTMAP_unknown];
151     OpenMPDirectiveKind Directive = OMPD_unknown;
152     DeclarationNameInfo DirectiveName;
153     Scope *CurScope = nullptr;
154     SourceLocation ConstructLoc;
155     /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
156     /// get the data (loop counters etc.) about enclosing loop-based construct.
157     /// This data is required during codegen.
158     DoacrossDependMapTy DoacrossDepends;
159     /// First argument (Expr *) contains optional argument of the
160     /// 'ordered' clause, the second one is true if the regions has 'ordered'
161     /// clause, false otherwise.
162     llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion;
163     unsigned AssociatedLoops = 1;
164     bool HasMutipleLoops = false;
165     const Decl *PossiblyLoopCounter = nullptr;
166     bool NowaitRegion = false;
167     bool CancelRegion = false;
168     bool LoopStart = false;
169     bool BodyComplete = false;
170     SourceLocation PrevScanLocation;
171     SourceLocation InnerTeamsRegionLoc;
172     /// Reference to the taskgroup task_reduction reference expression.
173     Expr *TaskgroupReductionRef = nullptr;
174     llvm::DenseSet<QualType> MappedClassesQualTypes;
175     SmallVector<Expr *, 4> InnerUsedAllocators;
176     llvm::DenseSet<CanonicalDeclPtr<Decl>> ImplicitTaskFirstprivates;
177     /// List of globals marked as declare target link in this target region
178     /// (isOpenMPTargetExecutionDirective(Directive) == true).
179     llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls;
180     /// List of decls used in inclusive/exclusive clauses of the scan directive.
181     llvm::DenseSet<CanonicalDeclPtr<Decl>> UsedInScanDirective;
182     llvm::DenseMap<CanonicalDeclPtr<const Decl>, UsesAllocatorsDeclKind>
183         UsesAllocatorsDecls;
184     SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
185                  Scope *CurScope, SourceLocation Loc)
186         : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
187           ConstructLoc(Loc) {}
188     SharingMapTy() = default;
189   };
190 
191   using StackTy = SmallVector<SharingMapTy, 4>;
192 
193   /// Stack of used declaration and their data-sharing attributes.
194   DeclSAMapTy Threadprivates;
195   const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
196   SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack;
197   /// true, if check for DSA must be from parent directive, false, if
198   /// from current directive.
199   OpenMPClauseKind ClauseKindMode = OMPC_unknown;
200   Sema &SemaRef;
201   bool ForceCapturing = false;
202   /// true if all the variables in the target executable directives must be
203   /// captured by reference.
204   bool ForceCaptureByReferenceInTargetExecutable = false;
205   CriticalsWithHintsTy Criticals;
206   unsigned IgnoredStackElements = 0;
207 
208   /// Iterators over the stack iterate in order from innermost to outermost
209   /// directive.
210   using const_iterator = StackTy::const_reverse_iterator;
211   const_iterator begin() const {
212     return Stack.empty() ? const_iterator()
213                          : Stack.back().first.rbegin() + IgnoredStackElements;
214   }
215   const_iterator end() const {
216     return Stack.empty() ? const_iterator() : Stack.back().first.rend();
217   }
218   using iterator = StackTy::reverse_iterator;
219   iterator begin() {
220     return Stack.empty() ? iterator()
221                          : Stack.back().first.rbegin() + IgnoredStackElements;
222   }
223   iterator end() {
224     return Stack.empty() ? iterator() : Stack.back().first.rend();
225   }
226 
227   // Convenience operations to get at the elements of the stack.
228 
229   bool isStackEmpty() const {
230     return Stack.empty() ||
231            Stack.back().second != CurrentNonCapturingFunctionScope ||
232            Stack.back().first.size() <= IgnoredStackElements;
233   }
234   size_t getStackSize() const {
235     return isStackEmpty() ? 0
236                           : Stack.back().first.size() - IgnoredStackElements;
237   }
238 
239   SharingMapTy *getTopOfStackOrNull() {
240     size_t Size = getStackSize();
241     if (Size == 0)
242       return nullptr;
243     return &Stack.back().first[Size - 1];
244   }
245   const SharingMapTy *getTopOfStackOrNull() const {
246     return const_cast<DSAStackTy&>(*this).getTopOfStackOrNull();
247   }
248   SharingMapTy &getTopOfStack() {
249     assert(!isStackEmpty() && "no current directive");
250     return *getTopOfStackOrNull();
251   }
252   const SharingMapTy &getTopOfStack() const {
253     return const_cast<DSAStackTy&>(*this).getTopOfStack();
254   }
255 
256   SharingMapTy *getSecondOnStackOrNull() {
257     size_t Size = getStackSize();
258     if (Size <= 1)
259       return nullptr;
260     return &Stack.back().first[Size - 2];
261   }
262   const SharingMapTy *getSecondOnStackOrNull() const {
263     return const_cast<DSAStackTy&>(*this).getSecondOnStackOrNull();
264   }
265 
266   /// Get the stack element at a certain level (previously returned by
267   /// \c getNestingLevel).
268   ///
269   /// Note that nesting levels count from outermost to innermost, and this is
270   /// the reverse of our iteration order where new inner levels are pushed at
271   /// the front of the stack.
272   SharingMapTy &getStackElemAtLevel(unsigned Level) {
273     assert(Level < getStackSize() && "no such stack element");
274     return Stack.back().first[Level];
275   }
276   const SharingMapTy &getStackElemAtLevel(unsigned Level) const {
277     return const_cast<DSAStackTy&>(*this).getStackElemAtLevel(Level);
278   }
279 
280   DSAVarData getDSA(const_iterator &Iter, ValueDecl *D) const;
281 
282   /// Checks if the variable is a local for OpenMP region.
283   bool isOpenMPLocal(VarDecl *D, const_iterator Iter) const;
284 
285   /// Vector of previously declared requires directives
286   SmallVector<const OMPRequiresDecl *, 2> RequiresDecls;
287   /// omp_allocator_handle_t type.
288   QualType OMPAllocatorHandleT;
289   /// omp_depend_t type.
290   QualType OMPDependT;
291   /// omp_event_handle_t type.
292   QualType OMPEventHandleT;
293   /// omp_alloctrait_t type.
294   QualType OMPAlloctraitT;
295   /// Expression for the predefined allocators.
296   Expr *OMPPredefinedAllocators[OMPAllocateDeclAttr::OMPUserDefinedMemAlloc] = {
297       nullptr};
298   /// Vector of previously encountered target directives
299   SmallVector<SourceLocation, 2> TargetLocations;
300   SourceLocation AtomicLocation;
301 
302 public:
303   explicit DSAStackTy(Sema &S) : SemaRef(S) {}
304 
305   /// Sets omp_allocator_handle_t type.
306   void setOMPAllocatorHandleT(QualType Ty) { OMPAllocatorHandleT = Ty; }
307   /// Gets omp_allocator_handle_t type.
308   QualType getOMPAllocatorHandleT() const { return OMPAllocatorHandleT; }
309   /// Sets omp_alloctrait_t type.
310   void setOMPAlloctraitT(QualType Ty) { OMPAlloctraitT = Ty; }
311   /// Gets omp_alloctrait_t type.
312   QualType getOMPAlloctraitT() const { return OMPAlloctraitT; }
313   /// Sets the given default allocator.
314   void setAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
315                     Expr *Allocator) {
316     OMPPredefinedAllocators[AllocatorKind] = Allocator;
317   }
318   /// Returns the specified default allocator.
319   Expr *getAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind) const {
320     return OMPPredefinedAllocators[AllocatorKind];
321   }
322   /// Sets omp_depend_t type.
323   void setOMPDependT(QualType Ty) { OMPDependT = Ty; }
324   /// Gets omp_depend_t type.
325   QualType getOMPDependT() const { return OMPDependT; }
326 
327   /// Sets omp_event_handle_t type.
328   void setOMPEventHandleT(QualType Ty) { OMPEventHandleT = Ty; }
329   /// Gets omp_event_handle_t type.
330   QualType getOMPEventHandleT() const { return OMPEventHandleT; }
331 
332   bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
333   OpenMPClauseKind getClauseParsingMode() const {
334     assert(isClauseParsingMode() && "Must be in clause parsing mode.");
335     return ClauseKindMode;
336   }
337   void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
338 
339   bool isBodyComplete() const {
340     const SharingMapTy *Top = getTopOfStackOrNull();
341     return Top && Top->BodyComplete;
342   }
343   void setBodyComplete() {
344     getTopOfStack().BodyComplete = true;
345   }
346 
347   bool isForceVarCapturing() const { return ForceCapturing; }
348   void setForceVarCapturing(bool V) { ForceCapturing = V; }
349 
350   void setForceCaptureByReferenceInTargetExecutable(bool V) {
351     ForceCaptureByReferenceInTargetExecutable = V;
352   }
353   bool isForceCaptureByReferenceInTargetExecutable() const {
354     return ForceCaptureByReferenceInTargetExecutable;
355   }
356 
357   void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
358             Scope *CurScope, SourceLocation Loc) {
359     assert(!IgnoredStackElements &&
360            "cannot change stack while ignoring elements");
361     if (Stack.empty() ||
362         Stack.back().second != CurrentNonCapturingFunctionScope)
363       Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope);
364     Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc);
365     Stack.back().first.back().DefaultAttrLoc = Loc;
366   }
367 
368   void pop() {
369     assert(!IgnoredStackElements &&
370            "cannot change stack while ignoring elements");
371     assert(!Stack.back().first.empty() &&
372            "Data-sharing attributes stack is empty!");
373     Stack.back().first.pop_back();
374   }
375 
376   /// RAII object to temporarily leave the scope of a directive when we want to
377   /// logically operate in its parent.
378   class ParentDirectiveScope {
379     DSAStackTy &Self;
380     bool Active;
381   public:
382     ParentDirectiveScope(DSAStackTy &Self, bool Activate)
383         : Self(Self), Active(false) {
384       if (Activate)
385         enable();
386     }
387     ~ParentDirectiveScope() { disable(); }
388     void disable() {
389       if (Active) {
390         --Self.IgnoredStackElements;
391         Active = false;
392       }
393     }
394     void enable() {
395       if (!Active) {
396         ++Self.IgnoredStackElements;
397         Active = true;
398       }
399     }
400   };
401 
402   /// Marks that we're started loop parsing.
403   void loopInit() {
404     assert(isOpenMPLoopDirective(getCurrentDirective()) &&
405            "Expected loop-based directive.");
406     getTopOfStack().LoopStart = true;
407   }
408   /// Start capturing of the variables in the loop context.
409   void loopStart() {
410     assert(isOpenMPLoopDirective(getCurrentDirective()) &&
411            "Expected loop-based directive.");
412     getTopOfStack().LoopStart = false;
413   }
414   /// true, if variables are captured, false otherwise.
415   bool isLoopStarted() const {
416     assert(isOpenMPLoopDirective(getCurrentDirective()) &&
417            "Expected loop-based directive.");
418     return !getTopOfStack().LoopStart;
419   }
420   /// Marks (or clears) declaration as possibly loop counter.
421   void resetPossibleLoopCounter(const Decl *D = nullptr) {
422     getTopOfStack().PossiblyLoopCounter =
423         D ? D->getCanonicalDecl() : D;
424   }
425   /// Gets the possible loop counter decl.
426   const Decl *getPossiblyLoopCunter() const {
427     return getTopOfStack().PossiblyLoopCounter;
428   }
429   /// Start new OpenMP region stack in new non-capturing function.
430   void pushFunction() {
431     assert(!IgnoredStackElements &&
432            "cannot change stack while ignoring elements");
433     const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
434     assert(!isa<CapturingScopeInfo>(CurFnScope));
435     CurrentNonCapturingFunctionScope = CurFnScope;
436   }
437   /// Pop region stack for non-capturing function.
438   void popFunction(const FunctionScopeInfo *OldFSI) {
439     assert(!IgnoredStackElements &&
440            "cannot change stack while ignoring elements");
441     if (!Stack.empty() && Stack.back().second == OldFSI) {
442       assert(Stack.back().first.empty());
443       Stack.pop_back();
444     }
445     CurrentNonCapturingFunctionScope = nullptr;
446     for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) {
447       if (!isa<CapturingScopeInfo>(FSI)) {
448         CurrentNonCapturingFunctionScope = FSI;
449         break;
450       }
451     }
452   }
453 
454   void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) {
455     Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint);
456   }
457   const std::pair<const OMPCriticalDirective *, llvm::APSInt>
458   getCriticalWithHint(const DeclarationNameInfo &Name) const {
459     auto I = Criticals.find(Name.getAsString());
460     if (I != Criticals.end())
461       return I->second;
462     return std::make_pair(nullptr, llvm::APSInt());
463   }
464   /// If 'aligned' declaration for given variable \a D was not seen yet,
465   /// add it and return NULL; otherwise return previous occurrence's expression
466   /// for diagnostics.
467   const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE);
468   /// If 'nontemporal' declaration for given variable \a D was not seen yet,
469   /// add it and return NULL; otherwise return previous occurrence's expression
470   /// for diagnostics.
471   const Expr *addUniqueNontemporal(const ValueDecl *D, const Expr *NewDE);
472 
473   /// Register specified variable as loop control variable.
474   void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture);
475   /// Check if the specified variable is a loop control variable for
476   /// current region.
477   /// \return The index of the loop control variable in the list of associated
478   /// for-loops (from outer to inner).
479   const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const;
480   /// Check if the specified variable is a loop control variable for
481   /// parent region.
482   /// \return The index of the loop control variable in the list of associated
483   /// for-loops (from outer to inner).
484   const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const;
485   /// Check if the specified variable is a loop control variable for
486   /// current region.
487   /// \return The index of the loop control variable in the list of associated
488   /// for-loops (from outer to inner).
489   const LCDeclInfo isLoopControlVariable(const ValueDecl *D,
490                                          unsigned Level) const;
491   /// Get the loop control variable for the I-th loop (or nullptr) in
492   /// parent directive.
493   const ValueDecl *getParentLoopControlVariable(unsigned I) const;
494 
495   /// Marks the specified decl \p D as used in scan directive.
496   void markDeclAsUsedInScanDirective(ValueDecl *D) {
497     if (SharingMapTy *Stack = getSecondOnStackOrNull())
498       Stack->UsedInScanDirective.insert(D);
499   }
500 
501   /// Checks if the specified declaration was used in the inner scan directive.
502   bool isUsedInScanDirective(ValueDecl *D) const {
503     if (const SharingMapTy *Stack = getTopOfStackOrNull())
504       return Stack->UsedInScanDirective.count(D) > 0;
505     return false;
506   }
507 
508   /// Adds explicit data sharing attribute to the specified declaration.
509   void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
510               DeclRefExpr *PrivateCopy = nullptr, unsigned Modifier = 0);
511 
512   /// Adds additional information for the reduction items with the reduction id
513   /// represented as an operator.
514   void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
515                                  BinaryOperatorKind BOK);
516   /// Adds additional information for the reduction items with the reduction id
517   /// represented as reduction identifier.
518   void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
519                                  const Expr *ReductionRef);
520   /// Returns the location and reduction operation from the innermost parent
521   /// region for the given \p D.
522   const DSAVarData
523   getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
524                                    BinaryOperatorKind &BOK,
525                                    Expr *&TaskgroupDescriptor) const;
526   /// Returns the location and reduction operation from the innermost parent
527   /// region for the given \p D.
528   const DSAVarData
529   getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
530                                    const Expr *&ReductionRef,
531                                    Expr *&TaskgroupDescriptor) const;
532   /// Return reduction reference expression for the current taskgroup or
533   /// parallel/worksharing directives with task reductions.
534   Expr *getTaskgroupReductionRef() const {
535     assert((getTopOfStack().Directive == OMPD_taskgroup ||
536             ((isOpenMPParallelDirective(getTopOfStack().Directive) ||
537               isOpenMPWorksharingDirective(getTopOfStack().Directive)) &&
538              !isOpenMPSimdDirective(getTopOfStack().Directive))) &&
539            "taskgroup reference expression requested for non taskgroup or "
540            "parallel/worksharing directive.");
541     return getTopOfStack().TaskgroupReductionRef;
542   }
543   /// Checks if the given \p VD declaration is actually a taskgroup reduction
544   /// descriptor variable at the \p Level of OpenMP regions.
545   bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const {
546     return getStackElemAtLevel(Level).TaskgroupReductionRef &&
547            cast<DeclRefExpr>(getStackElemAtLevel(Level).TaskgroupReductionRef)
548                    ->getDecl() == VD;
549   }
550 
551   /// Returns data sharing attributes from top of the stack for the
552   /// specified declaration.
553   const DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
554   /// Returns data-sharing attributes for the specified declaration.
555   const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const;
556   /// Returns data-sharing attributes for the specified declaration.
557   const DSAVarData getImplicitDSA(ValueDecl *D, unsigned Level) const;
558   /// Checks if the specified variables has data-sharing attributes which
559   /// match specified \a CPred predicate in any directive which matches \a DPred
560   /// predicate.
561   const DSAVarData
562   hasDSA(ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
563          const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
564          bool FromParent) const;
565   /// Checks if the specified variables has data-sharing attributes which
566   /// match specified \a CPred predicate in any innermost directive which
567   /// matches \a DPred predicate.
568   const DSAVarData
569   hasInnermostDSA(ValueDecl *D,
570                   const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
571                   const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
572                   bool FromParent) const;
573   /// Checks if the specified variables has explicit data-sharing
574   /// attributes which match specified \a CPred predicate at the specified
575   /// OpenMP region.
576   bool hasExplicitDSA(const ValueDecl *D,
577                       const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
578                       unsigned Level, bool NotLastprivate = false) const;
579 
580   /// Returns true if the directive at level \Level matches in the
581   /// specified \a DPred predicate.
582   bool hasExplicitDirective(
583       const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
584       unsigned Level) const;
585 
586   /// Finds a directive which matches specified \a DPred predicate.
587   bool hasDirective(
588       const llvm::function_ref<bool(
589           OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)>
590           DPred,
591       bool FromParent) const;
592 
593   /// Returns currently analyzed directive.
594   OpenMPDirectiveKind getCurrentDirective() const {
595     const SharingMapTy *Top = getTopOfStackOrNull();
596     return Top ? Top->Directive : OMPD_unknown;
597   }
598   /// Returns directive kind at specified level.
599   OpenMPDirectiveKind getDirective(unsigned Level) const {
600     assert(!isStackEmpty() && "No directive at specified level.");
601     return getStackElemAtLevel(Level).Directive;
602   }
603   /// Returns the capture region at the specified level.
604   OpenMPDirectiveKind getCaptureRegion(unsigned Level,
605                                        unsigned OpenMPCaptureLevel) const {
606     SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
607     getOpenMPCaptureRegions(CaptureRegions, getDirective(Level));
608     return CaptureRegions[OpenMPCaptureLevel];
609   }
610   /// Returns parent directive.
611   OpenMPDirectiveKind getParentDirective() const {
612     const SharingMapTy *Parent = getSecondOnStackOrNull();
613     return Parent ? Parent->Directive : OMPD_unknown;
614   }
615 
616   /// Add requires decl to internal vector
617   void addRequiresDecl(OMPRequiresDecl *RD) {
618     RequiresDecls.push_back(RD);
619   }
620 
621   /// Checks if the defined 'requires' directive has specified type of clause.
622   template <typename ClauseType>
623   bool hasRequiresDeclWithClause() const {
624     return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
625       return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
626         return isa<ClauseType>(C);
627       });
628     });
629   }
630 
631   /// Checks for a duplicate clause amongst previously declared requires
632   /// directives
633   bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const {
634     bool IsDuplicate = false;
635     for (OMPClause *CNew : ClauseList) {
636       for (const OMPRequiresDecl *D : RequiresDecls) {
637         for (const OMPClause *CPrev : D->clauselists()) {
638           if (CNew->getClauseKind() == CPrev->getClauseKind()) {
639             SemaRef.Diag(CNew->getBeginLoc(),
640                          diag::err_omp_requires_clause_redeclaration)
641                 << getOpenMPClauseName(CNew->getClauseKind());
642             SemaRef.Diag(CPrev->getBeginLoc(),
643                          diag::note_omp_requires_previous_clause)
644                 << getOpenMPClauseName(CPrev->getClauseKind());
645             IsDuplicate = true;
646           }
647         }
648       }
649     }
650     return IsDuplicate;
651   }
652 
653   /// Add location of previously encountered target to internal vector
654   void addTargetDirLocation(SourceLocation LocStart) {
655     TargetLocations.push_back(LocStart);
656   }
657 
658   /// Add location for the first encountered atomicc directive.
659   void addAtomicDirectiveLoc(SourceLocation Loc) {
660     if (AtomicLocation.isInvalid())
661       AtomicLocation = Loc;
662   }
663 
664   /// Returns the location of the first encountered atomic directive in the
665   /// module.
666   SourceLocation getAtomicDirectiveLoc() const {
667     return AtomicLocation;
668   }
669 
670   // Return previously encountered target region locations.
671   ArrayRef<SourceLocation> getEncounteredTargetLocs() const {
672     return TargetLocations;
673   }
674 
675   /// Set default data sharing attribute to none.
676   void setDefaultDSANone(SourceLocation Loc) {
677     getTopOfStack().DefaultAttr = DSA_none;
678     getTopOfStack().DefaultAttrLoc = Loc;
679   }
680   /// Set default data sharing attribute to shared.
681   void setDefaultDSAShared(SourceLocation Loc) {
682     getTopOfStack().DefaultAttr = DSA_shared;
683     getTopOfStack().DefaultAttrLoc = Loc;
684   }
685   /// Set default data mapping attribute to Modifier:Kind
686   void setDefaultDMAAttr(OpenMPDefaultmapClauseModifier M,
687                          OpenMPDefaultmapClauseKind Kind,
688                          SourceLocation Loc) {
689     DefaultmapInfo &DMI = getTopOfStack().DefaultmapMap[Kind];
690     DMI.ImplicitBehavior = M;
691     DMI.SLoc = Loc;
692   }
693   /// Check whether the implicit-behavior has been set in defaultmap
694   bool checkDefaultmapCategory(OpenMPDefaultmapClauseKind VariableCategory) {
695     if (VariableCategory == OMPC_DEFAULTMAP_unknown)
696       return getTopOfStack()
697                      .DefaultmapMap[OMPC_DEFAULTMAP_aggregate]
698                      .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown ||
699              getTopOfStack()
700                      .DefaultmapMap[OMPC_DEFAULTMAP_scalar]
701                      .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown ||
702              getTopOfStack()
703                      .DefaultmapMap[OMPC_DEFAULTMAP_pointer]
704                      .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown;
705     return getTopOfStack().DefaultmapMap[VariableCategory].ImplicitBehavior !=
706            OMPC_DEFAULTMAP_MODIFIER_unknown;
707   }
708 
709   DefaultDataSharingAttributes getDefaultDSA(unsigned Level) const {
710     return getStackSize() <= Level ? DSA_unspecified
711                                    : getStackElemAtLevel(Level).DefaultAttr;
712   }
713   DefaultDataSharingAttributes getDefaultDSA() const {
714     return isStackEmpty() ? DSA_unspecified
715                           : getTopOfStack().DefaultAttr;
716   }
717   SourceLocation getDefaultDSALocation() const {
718     return isStackEmpty() ? SourceLocation()
719                           : getTopOfStack().DefaultAttrLoc;
720   }
721   OpenMPDefaultmapClauseModifier
722   getDefaultmapModifier(OpenMPDefaultmapClauseKind Kind) const {
723     return isStackEmpty()
724                ? OMPC_DEFAULTMAP_MODIFIER_unknown
725                : getTopOfStack().DefaultmapMap[Kind].ImplicitBehavior;
726   }
727   OpenMPDefaultmapClauseModifier
728   getDefaultmapModifierAtLevel(unsigned Level,
729                                OpenMPDefaultmapClauseKind Kind) const {
730     return getStackElemAtLevel(Level).DefaultmapMap[Kind].ImplicitBehavior;
731   }
732   bool isDefaultmapCapturedByRef(unsigned Level,
733                                  OpenMPDefaultmapClauseKind Kind) const {
734     OpenMPDefaultmapClauseModifier M =
735         getDefaultmapModifierAtLevel(Level, Kind);
736     if (Kind == OMPC_DEFAULTMAP_scalar || Kind == OMPC_DEFAULTMAP_pointer) {
737       return (M == OMPC_DEFAULTMAP_MODIFIER_alloc) ||
738              (M == OMPC_DEFAULTMAP_MODIFIER_to) ||
739              (M == OMPC_DEFAULTMAP_MODIFIER_from) ||
740              (M == OMPC_DEFAULTMAP_MODIFIER_tofrom);
741     }
742     return true;
743   }
744   static bool mustBeFirstprivateBase(OpenMPDefaultmapClauseModifier M,
745                                      OpenMPDefaultmapClauseKind Kind) {
746     switch (Kind) {
747     case OMPC_DEFAULTMAP_scalar:
748     case OMPC_DEFAULTMAP_pointer:
749       return (M == OMPC_DEFAULTMAP_MODIFIER_unknown) ||
750              (M == OMPC_DEFAULTMAP_MODIFIER_firstprivate) ||
751              (M == OMPC_DEFAULTMAP_MODIFIER_default);
752     case OMPC_DEFAULTMAP_aggregate:
753       return M == OMPC_DEFAULTMAP_MODIFIER_firstprivate;
754     default:
755       break;
756     }
757     llvm_unreachable("Unexpected OpenMPDefaultmapClauseKind enum");
758   }
759   bool mustBeFirstprivateAtLevel(unsigned Level,
760                                  OpenMPDefaultmapClauseKind Kind) const {
761     OpenMPDefaultmapClauseModifier M =
762         getDefaultmapModifierAtLevel(Level, Kind);
763     return mustBeFirstprivateBase(M, Kind);
764   }
765   bool mustBeFirstprivate(OpenMPDefaultmapClauseKind Kind) const {
766     OpenMPDefaultmapClauseModifier M = getDefaultmapModifier(Kind);
767     return mustBeFirstprivateBase(M, Kind);
768   }
769 
770   /// Checks if the specified variable is a threadprivate.
771   bool isThreadPrivate(VarDecl *D) {
772     const DSAVarData DVar = getTopDSA(D, false);
773     return isOpenMPThreadPrivate(DVar.CKind);
774   }
775 
776   /// Marks current region as ordered (it has an 'ordered' clause).
777   void setOrderedRegion(bool IsOrdered, const Expr *Param,
778                         OMPOrderedClause *Clause) {
779     if (IsOrdered)
780       getTopOfStack().OrderedRegion.emplace(Param, Clause);
781     else
782       getTopOfStack().OrderedRegion.reset();
783   }
784   /// Returns true, if region is ordered (has associated 'ordered' clause),
785   /// false - otherwise.
786   bool isOrderedRegion() const {
787     if (const SharingMapTy *Top = getTopOfStackOrNull())
788       return Top->OrderedRegion.hasValue();
789     return false;
790   }
791   /// Returns optional parameter for the ordered region.
792   std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const {
793     if (const SharingMapTy *Top = getTopOfStackOrNull())
794       if (Top->OrderedRegion.hasValue())
795         return Top->OrderedRegion.getValue();
796     return std::make_pair(nullptr, nullptr);
797   }
798   /// Returns true, if parent region is ordered (has associated
799   /// 'ordered' clause), false - otherwise.
800   bool isParentOrderedRegion() const {
801     if (const SharingMapTy *Parent = getSecondOnStackOrNull())
802       return Parent->OrderedRegion.hasValue();
803     return false;
804   }
805   /// Returns optional parameter for the ordered region.
806   std::pair<const Expr *, OMPOrderedClause *>
807   getParentOrderedRegionParam() const {
808     if (const SharingMapTy *Parent = getSecondOnStackOrNull())
809       if (Parent->OrderedRegion.hasValue())
810         return Parent->OrderedRegion.getValue();
811     return std::make_pair(nullptr, nullptr);
812   }
813   /// Marks current region as nowait (it has a 'nowait' clause).
814   void setNowaitRegion(bool IsNowait = true) {
815     getTopOfStack().NowaitRegion = IsNowait;
816   }
817   /// Returns true, if parent region is nowait (has associated
818   /// 'nowait' clause), false - otherwise.
819   bool isParentNowaitRegion() const {
820     if (const SharingMapTy *Parent = getSecondOnStackOrNull())
821       return Parent->NowaitRegion;
822     return false;
823   }
824   /// Marks parent region as cancel region.
825   void setParentCancelRegion(bool Cancel = true) {
826     if (SharingMapTy *Parent = getSecondOnStackOrNull())
827       Parent->CancelRegion |= Cancel;
828   }
829   /// Return true if current region has inner cancel construct.
830   bool isCancelRegion() const {
831     const SharingMapTy *Top = getTopOfStackOrNull();
832     return Top ? Top->CancelRegion : false;
833   }
834 
835   /// Mark that parent region already has scan directive.
836   void setParentHasScanDirective(SourceLocation Loc) {
837     if (SharingMapTy *Parent = getSecondOnStackOrNull())
838       Parent->PrevScanLocation = Loc;
839   }
840   /// Return true if current region has inner cancel construct.
841   bool doesParentHasScanDirective() const {
842     const SharingMapTy *Top = getSecondOnStackOrNull();
843     return Top ? Top->PrevScanLocation.isValid() : false;
844   }
845   /// Return true if current region has inner cancel construct.
846   SourceLocation getParentScanDirectiveLoc() const {
847     const SharingMapTy *Top = getSecondOnStackOrNull();
848     return Top ? Top->PrevScanLocation : SourceLocation();
849   }
850 
851   /// Set collapse value for the region.
852   void setAssociatedLoops(unsigned Val) {
853     getTopOfStack().AssociatedLoops = Val;
854     if (Val > 1)
855       getTopOfStack().HasMutipleLoops = true;
856   }
857   /// Return collapse value for region.
858   unsigned getAssociatedLoops() const {
859     const SharingMapTy *Top = getTopOfStackOrNull();
860     return Top ? Top->AssociatedLoops : 0;
861   }
862   /// Returns true if the construct is associated with multiple loops.
863   bool hasMutipleLoops() const {
864     const SharingMapTy *Top = getTopOfStackOrNull();
865     return Top ? Top->HasMutipleLoops : false;
866   }
867 
868   /// Marks current target region as one with closely nested teams
869   /// region.
870   void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
871     if (SharingMapTy *Parent = getSecondOnStackOrNull())
872       Parent->InnerTeamsRegionLoc = TeamsRegionLoc;
873   }
874   /// Returns true, if current region has closely nested teams region.
875   bool hasInnerTeamsRegion() const {
876     return getInnerTeamsRegionLoc().isValid();
877   }
878   /// Returns location of the nested teams region (if any).
879   SourceLocation getInnerTeamsRegionLoc() const {
880     const SharingMapTy *Top = getTopOfStackOrNull();
881     return Top ? Top->InnerTeamsRegionLoc : SourceLocation();
882   }
883 
884   Scope *getCurScope() const {
885     const SharingMapTy *Top = getTopOfStackOrNull();
886     return Top ? Top->CurScope : nullptr;
887   }
888   SourceLocation getConstructLoc() const {
889     const SharingMapTy *Top = getTopOfStackOrNull();
890     return Top ? Top->ConstructLoc : SourceLocation();
891   }
892 
893   /// Do the check specified in \a Check to all component lists and return true
894   /// if any issue is found.
895   bool checkMappableExprComponentListsForDecl(
896       const ValueDecl *VD, bool CurrentRegionOnly,
897       const llvm::function_ref<
898           bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
899                OpenMPClauseKind)>
900           Check) const {
901     if (isStackEmpty())
902       return false;
903     auto SI = begin();
904     auto SE = end();
905 
906     if (SI == SE)
907       return false;
908 
909     if (CurrentRegionOnly)
910       SE = std::next(SI);
911     else
912       std::advance(SI, 1);
913 
914     for (; SI != SE; ++SI) {
915       auto MI = SI->MappedExprComponents.find(VD);
916       if (MI != SI->MappedExprComponents.end())
917         for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
918              MI->second.Components)
919           if (Check(L, MI->second.Kind))
920             return true;
921     }
922     return false;
923   }
924 
925   /// Do the check specified in \a Check to all component lists at a given level
926   /// and return true if any issue is found.
927   bool checkMappableExprComponentListsForDeclAtLevel(
928       const ValueDecl *VD, unsigned Level,
929       const llvm::function_ref<
930           bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
931                OpenMPClauseKind)>
932           Check) const {
933     if (getStackSize() <= Level)
934       return false;
935 
936     const SharingMapTy &StackElem = getStackElemAtLevel(Level);
937     auto MI = StackElem.MappedExprComponents.find(VD);
938     if (MI != StackElem.MappedExprComponents.end())
939       for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
940            MI->second.Components)
941         if (Check(L, MI->second.Kind))
942           return true;
943     return false;
944   }
945 
946   /// Create a new mappable expression component list associated with a given
947   /// declaration and initialize it with the provided list of components.
948   void addMappableExpressionComponents(
949       const ValueDecl *VD,
950       OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
951       OpenMPClauseKind WhereFoundClauseKind) {
952     MappedExprComponentTy &MEC = getTopOfStack().MappedExprComponents[VD];
953     // Create new entry and append the new components there.
954     MEC.Components.resize(MEC.Components.size() + 1);
955     MEC.Components.back().append(Components.begin(), Components.end());
956     MEC.Kind = WhereFoundClauseKind;
957   }
958 
959   unsigned getNestingLevel() const {
960     assert(!isStackEmpty());
961     return getStackSize() - 1;
962   }
963   void addDoacrossDependClause(OMPDependClause *C,
964                                const OperatorOffsetTy &OpsOffs) {
965     SharingMapTy *Parent = getSecondOnStackOrNull();
966     assert(Parent && isOpenMPWorksharingDirective(Parent->Directive));
967     Parent->DoacrossDepends.try_emplace(C, OpsOffs);
968   }
969   llvm::iterator_range<DoacrossDependMapTy::const_iterator>
970   getDoacrossDependClauses() const {
971     const SharingMapTy &StackElem = getTopOfStack();
972     if (isOpenMPWorksharingDirective(StackElem.Directive)) {
973       const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends;
974       return llvm::make_range(Ref.begin(), Ref.end());
975     }
976     return llvm::make_range(StackElem.DoacrossDepends.end(),
977                             StackElem.DoacrossDepends.end());
978   }
979 
980   // Store types of classes which have been explicitly mapped
981   void addMappedClassesQualTypes(QualType QT) {
982     SharingMapTy &StackElem = getTopOfStack();
983     StackElem.MappedClassesQualTypes.insert(QT);
984   }
985 
986   // Return set of mapped classes types
987   bool isClassPreviouslyMapped(QualType QT) const {
988     const SharingMapTy &StackElem = getTopOfStack();
989     return StackElem.MappedClassesQualTypes.count(QT) != 0;
990   }
991 
992   /// Adds global declare target to the parent target region.
993   void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) {
994     assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
995                E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link &&
996            "Expected declare target link global.");
997     for (auto &Elem : *this) {
998       if (isOpenMPTargetExecutionDirective(Elem.Directive)) {
999         Elem.DeclareTargetLinkVarDecls.push_back(E);
1000         return;
1001       }
1002     }
1003   }
1004 
1005   /// Returns the list of globals with declare target link if current directive
1006   /// is target.
1007   ArrayRef<DeclRefExpr *> getLinkGlobals() const {
1008     assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) &&
1009            "Expected target executable directive.");
1010     return getTopOfStack().DeclareTargetLinkVarDecls;
1011   }
1012 
1013   /// Adds list of allocators expressions.
1014   void addInnerAllocatorExpr(Expr *E) {
1015     getTopOfStack().InnerUsedAllocators.push_back(E);
1016   }
1017   /// Return list of used allocators.
1018   ArrayRef<Expr *> getInnerAllocators() const {
1019     return getTopOfStack().InnerUsedAllocators;
1020   }
1021   /// Marks the declaration as implicitly firstprivate nin the task-based
1022   /// regions.
1023   void addImplicitTaskFirstprivate(unsigned Level, Decl *D) {
1024     getStackElemAtLevel(Level).ImplicitTaskFirstprivates.insert(D);
1025   }
1026   /// Checks if the decl is implicitly firstprivate in the task-based region.
1027   bool isImplicitTaskFirstprivate(Decl *D) const {
1028     return getTopOfStack().ImplicitTaskFirstprivates.count(D) > 0;
1029   }
1030 
1031   /// Marks decl as used in uses_allocators clause as the allocator.
1032   void addUsesAllocatorsDecl(const Decl *D, UsesAllocatorsDeclKind Kind) {
1033     getTopOfStack().UsesAllocatorsDecls.try_emplace(D, Kind);
1034   }
1035   /// Checks if specified decl is used in uses allocator clause as the
1036   /// allocator.
1037   Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(unsigned Level,
1038                                                         const Decl *D) const {
1039     const SharingMapTy &StackElem = getTopOfStack();
1040     auto I = StackElem.UsesAllocatorsDecls.find(D);
1041     if (I == StackElem.UsesAllocatorsDecls.end())
1042       return None;
1043     return I->getSecond();
1044   }
1045   Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(const Decl *D) const {
1046     const SharingMapTy &StackElem = getTopOfStack();
1047     auto I = StackElem.UsesAllocatorsDecls.find(D);
1048     if (I == StackElem.UsesAllocatorsDecls.end())
1049       return None;
1050     return I->getSecond();
1051   }
1052 };
1053 
1054 bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) {
1055   return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind);
1056 }
1057 
1058 bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) {
1059   return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(DKind) ||
1060          DKind == OMPD_unknown;
1061 }
1062 
1063 } // namespace
1064 
1065 static const Expr *getExprAsWritten(const Expr *E) {
1066   if (const auto *FE = dyn_cast<FullExpr>(E))
1067     E = FE->getSubExpr();
1068 
1069   if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
1070     E = MTE->getSubExpr();
1071 
1072   while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
1073     E = Binder->getSubExpr();
1074 
1075   if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
1076     E = ICE->getSubExprAsWritten();
1077   return E->IgnoreParens();
1078 }
1079 
1080 static Expr *getExprAsWritten(Expr *E) {
1081   return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E)));
1082 }
1083 
1084 static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
1085   if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
1086     if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
1087       D = ME->getMemberDecl();
1088   const auto *VD = dyn_cast<VarDecl>(D);
1089   const auto *FD = dyn_cast<FieldDecl>(D);
1090   if (VD != nullptr) {
1091     VD = VD->getCanonicalDecl();
1092     D = VD;
1093   } else {
1094     assert(FD);
1095     FD = FD->getCanonicalDecl();
1096     D = FD;
1097   }
1098   return D;
1099 }
1100 
1101 static ValueDecl *getCanonicalDecl(ValueDecl *D) {
1102   return const_cast<ValueDecl *>(
1103       getCanonicalDecl(const_cast<const ValueDecl *>(D)));
1104 }
1105 
1106 DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter,
1107                                           ValueDecl *D) const {
1108   D = getCanonicalDecl(D);
1109   auto *VD = dyn_cast<VarDecl>(D);
1110   const auto *FD = dyn_cast<FieldDecl>(D);
1111   DSAVarData DVar;
1112   if (Iter == end()) {
1113     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1114     // in a region but not in construct]
1115     //  File-scope or namespace-scope variables referenced in called routines
1116     //  in the region are shared unless they appear in a threadprivate
1117     //  directive.
1118     if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD))
1119       DVar.CKind = OMPC_shared;
1120 
1121     // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
1122     // in a region but not in construct]
1123     //  Variables with static storage duration that are declared in called
1124     //  routines in the region are shared.
1125     if (VD && VD->hasGlobalStorage())
1126       DVar.CKind = OMPC_shared;
1127 
1128     // Non-static data members are shared by default.
1129     if (FD)
1130       DVar.CKind = OMPC_shared;
1131 
1132     return DVar;
1133   }
1134 
1135   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1136   // in a Construct, C/C++, predetermined, p.1]
1137   // Variables with automatic storage duration that are declared in a scope
1138   // inside the construct are private.
1139   if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
1140       (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
1141     DVar.CKind = OMPC_private;
1142     return DVar;
1143   }
1144 
1145   DVar.DKind = Iter->Directive;
1146   // Explicitly specified attributes and local variables with predetermined
1147   // attributes.
1148   if (Iter->SharingMap.count(D)) {
1149     const DSAInfo &Data = Iter->SharingMap.lookup(D);
1150     DVar.RefExpr = Data.RefExpr.getPointer();
1151     DVar.PrivateCopy = Data.PrivateCopy;
1152     DVar.CKind = Data.Attributes;
1153     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1154     DVar.Modifier = Data.Modifier;
1155     return DVar;
1156   }
1157 
1158   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1159   // in a Construct, C/C++, implicitly determined, p.1]
1160   //  In a parallel or task construct, the data-sharing attributes of these
1161   //  variables are determined by the default clause, if present.
1162   switch (Iter->DefaultAttr) {
1163   case DSA_shared:
1164     DVar.CKind = OMPC_shared;
1165     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1166     return DVar;
1167   case DSA_none:
1168     return DVar;
1169   case DSA_unspecified:
1170     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1171     // in a Construct, implicitly determined, p.2]
1172     //  In a parallel construct, if no default clause is present, these
1173     //  variables are shared.
1174     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1175     if ((isOpenMPParallelDirective(DVar.DKind) &&
1176          !isOpenMPTaskLoopDirective(DVar.DKind)) ||
1177         isOpenMPTeamsDirective(DVar.DKind)) {
1178       DVar.CKind = OMPC_shared;
1179       return DVar;
1180     }
1181 
1182     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1183     // in a Construct, implicitly determined, p.4]
1184     //  In a task construct, if no default clause is present, a variable that in
1185     //  the enclosing context is determined to be shared by all implicit tasks
1186     //  bound to the current team is shared.
1187     if (isOpenMPTaskingDirective(DVar.DKind)) {
1188       DSAVarData DVarTemp;
1189       const_iterator I = Iter, E = end();
1190       do {
1191         ++I;
1192         // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
1193         // Referenced in a Construct, implicitly determined, p.6]
1194         //  In a task construct, if no default clause is present, a variable
1195         //  whose data-sharing attribute is not determined by the rules above is
1196         //  firstprivate.
1197         DVarTemp = getDSA(I, D);
1198         if (DVarTemp.CKind != OMPC_shared) {
1199           DVar.RefExpr = nullptr;
1200           DVar.CKind = OMPC_firstprivate;
1201           return DVar;
1202         }
1203       } while (I != E && !isImplicitTaskingRegion(I->Directive));
1204       DVar.CKind =
1205           (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
1206       return DVar;
1207     }
1208   }
1209   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1210   // in a Construct, implicitly determined, p.3]
1211   //  For constructs other than task, if no default clause is present, these
1212   //  variables inherit their data-sharing attributes from the enclosing
1213   //  context.
1214   return getDSA(++Iter, D);
1215 }
1216 
1217 const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
1218                                          const Expr *NewDE) {
1219   assert(!isStackEmpty() && "Data sharing attributes stack is empty");
1220   D = getCanonicalDecl(D);
1221   SharingMapTy &StackElem = getTopOfStack();
1222   auto It = StackElem.AlignedMap.find(D);
1223   if (It == StackElem.AlignedMap.end()) {
1224     assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
1225     StackElem.AlignedMap[D] = NewDE;
1226     return nullptr;
1227   }
1228   assert(It->second && "Unexpected nullptr expr in the aligned map");
1229   return It->second;
1230 }
1231 
1232 const Expr *DSAStackTy::addUniqueNontemporal(const ValueDecl *D,
1233                                              const Expr *NewDE) {
1234   assert(!isStackEmpty() && "Data sharing attributes stack is empty");
1235   D = getCanonicalDecl(D);
1236   SharingMapTy &StackElem = getTopOfStack();
1237   auto It = StackElem.NontemporalMap.find(D);
1238   if (It == StackElem.NontemporalMap.end()) {
1239     assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
1240     StackElem.NontemporalMap[D] = NewDE;
1241     return nullptr;
1242   }
1243   assert(It->second && "Unexpected nullptr expr in the aligned map");
1244   return It->second;
1245 }
1246 
1247 void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
1248   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1249   D = getCanonicalDecl(D);
1250   SharingMapTy &StackElem = getTopOfStack();
1251   StackElem.LCVMap.try_emplace(
1252       D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
1253 }
1254 
1255 const DSAStackTy::LCDeclInfo
1256 DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
1257   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1258   D = getCanonicalDecl(D);
1259   const SharingMapTy &StackElem = getTopOfStack();
1260   auto It = StackElem.LCVMap.find(D);
1261   if (It != StackElem.LCVMap.end())
1262     return It->second;
1263   return {0, nullptr};
1264 }
1265 
1266 const DSAStackTy::LCDeclInfo
1267 DSAStackTy::isLoopControlVariable(const ValueDecl *D, unsigned Level) const {
1268   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1269   D = getCanonicalDecl(D);
1270   for (unsigned I = Level + 1; I > 0; --I) {
1271     const SharingMapTy &StackElem = getStackElemAtLevel(I - 1);
1272     auto It = StackElem.LCVMap.find(D);
1273     if (It != StackElem.LCVMap.end())
1274       return It->second;
1275   }
1276   return {0, nullptr};
1277 }
1278 
1279 const DSAStackTy::LCDeclInfo
1280 DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
1281   const SharingMapTy *Parent = getSecondOnStackOrNull();
1282   assert(Parent && "Data-sharing attributes stack is empty");
1283   D = getCanonicalDecl(D);
1284   auto It = Parent->LCVMap.find(D);
1285   if (It != Parent->LCVMap.end())
1286     return It->second;
1287   return {0, nullptr};
1288 }
1289 
1290 const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
1291   const SharingMapTy *Parent = getSecondOnStackOrNull();
1292   assert(Parent && "Data-sharing attributes stack is empty");
1293   if (Parent->LCVMap.size() < I)
1294     return nullptr;
1295   for (const auto &Pair : Parent->LCVMap)
1296     if (Pair.second.first == I)
1297       return Pair.first;
1298   return nullptr;
1299 }
1300 
1301 void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
1302                         DeclRefExpr *PrivateCopy, unsigned Modifier) {
1303   D = getCanonicalDecl(D);
1304   if (A == OMPC_threadprivate) {
1305     DSAInfo &Data = Threadprivates[D];
1306     Data.Attributes = A;
1307     Data.RefExpr.setPointer(E);
1308     Data.PrivateCopy = nullptr;
1309     Data.Modifier = Modifier;
1310   } else {
1311     DSAInfo &Data = getTopOfStack().SharingMap[D];
1312     assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
1313            (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
1314            (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
1315            (isLoopControlVariable(D).first && A == OMPC_private));
1316     Data.Modifier = Modifier;
1317     if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
1318       Data.RefExpr.setInt(/*IntVal=*/true);
1319       return;
1320     }
1321     const bool IsLastprivate =
1322         A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
1323     Data.Attributes = A;
1324     Data.RefExpr.setPointerAndInt(E, IsLastprivate);
1325     Data.PrivateCopy = PrivateCopy;
1326     if (PrivateCopy) {
1327       DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()];
1328       Data.Modifier = Modifier;
1329       Data.Attributes = A;
1330       Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
1331       Data.PrivateCopy = nullptr;
1332     }
1333   }
1334 }
1335 
1336 /// Build a variable declaration for OpenMP loop iteration variable.
1337 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
1338                              StringRef Name, const AttrVec *Attrs = nullptr,
1339                              DeclRefExpr *OrigRef = nullptr) {
1340   DeclContext *DC = SemaRef.CurContext;
1341   IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
1342   TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
1343   auto *Decl =
1344       VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
1345   if (Attrs) {
1346     for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
1347          I != E; ++I)
1348       Decl->addAttr(*I);
1349   }
1350   Decl->setImplicit();
1351   if (OrigRef) {
1352     Decl->addAttr(
1353         OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef));
1354   }
1355   return Decl;
1356 }
1357 
1358 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
1359                                      SourceLocation Loc,
1360                                      bool RefersToCapture = false) {
1361   D->setReferenced();
1362   D->markUsed(S.Context);
1363   return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
1364                              SourceLocation(), D, RefersToCapture, Loc, Ty,
1365                              VK_LValue);
1366 }
1367 
1368 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1369                                            BinaryOperatorKind BOK) {
1370   D = getCanonicalDecl(D);
1371   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1372   assert(
1373       getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
1374       "Additional reduction info may be specified only for reduction items.");
1375   ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
1376   assert(ReductionData.ReductionRange.isInvalid() &&
1377          (getTopOfStack().Directive == OMPD_taskgroup ||
1378           ((isOpenMPParallelDirective(getTopOfStack().Directive) ||
1379             isOpenMPWorksharingDirective(getTopOfStack().Directive)) &&
1380            !isOpenMPSimdDirective(getTopOfStack().Directive))) &&
1381          "Additional reduction info may be specified only once for reduction "
1382          "items.");
1383   ReductionData.set(BOK, SR);
1384   Expr *&TaskgroupReductionRef =
1385       getTopOfStack().TaskgroupReductionRef;
1386   if (!TaskgroupReductionRef) {
1387     VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1388                                SemaRef.Context.VoidPtrTy, ".task_red.");
1389     TaskgroupReductionRef =
1390         buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
1391   }
1392 }
1393 
1394 void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1395                                            const Expr *ReductionRef) {
1396   D = getCanonicalDecl(D);
1397   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1398   assert(
1399       getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
1400       "Additional reduction info may be specified only for reduction items.");
1401   ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
1402   assert(ReductionData.ReductionRange.isInvalid() &&
1403          (getTopOfStack().Directive == OMPD_taskgroup ||
1404           ((isOpenMPParallelDirective(getTopOfStack().Directive) ||
1405             isOpenMPWorksharingDirective(getTopOfStack().Directive)) &&
1406            !isOpenMPSimdDirective(getTopOfStack().Directive))) &&
1407          "Additional reduction info may be specified only once for reduction "
1408          "items.");
1409   ReductionData.set(ReductionRef, SR);
1410   Expr *&TaskgroupReductionRef =
1411       getTopOfStack().TaskgroupReductionRef;
1412   if (!TaskgroupReductionRef) {
1413     VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1414                                SemaRef.Context.VoidPtrTy, ".task_red.");
1415     TaskgroupReductionRef =
1416         buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
1417   }
1418 }
1419 
1420 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1421     const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
1422     Expr *&TaskgroupDescriptor) const {
1423   D = getCanonicalDecl(D);
1424   assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1425   for (const_iterator I = begin() + 1, E = end(); I != E; ++I) {
1426     const DSAInfo &Data = I->SharingMap.lookup(D);
1427     if (Data.Attributes != OMPC_reduction ||
1428         Data.Modifier != OMPC_REDUCTION_task)
1429       continue;
1430     const ReductionData &ReductionData = I->ReductionMap.lookup(D);
1431     if (!ReductionData.ReductionOp ||
1432         ReductionData.ReductionOp.is<const Expr *>())
1433       return DSAVarData();
1434     SR = ReductionData.ReductionRange;
1435     BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
1436     assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1437                                        "expression for the descriptor is not "
1438                                        "set.");
1439     TaskgroupDescriptor = I->TaskgroupReductionRef;
1440     return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(),
1441                       Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task);
1442   }
1443   return DSAVarData();
1444 }
1445 
1446 const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1447     const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
1448     Expr *&TaskgroupDescriptor) const {
1449   D = getCanonicalDecl(D);
1450   assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1451   for (const_iterator I = begin() + 1, E = end(); I != E; ++I) {
1452     const DSAInfo &Data = I->SharingMap.lookup(D);
1453     if (Data.Attributes != OMPC_reduction ||
1454         Data.Modifier != OMPC_REDUCTION_task)
1455       continue;
1456     const ReductionData &ReductionData = I->ReductionMap.lookup(D);
1457     if (!ReductionData.ReductionOp ||
1458         !ReductionData.ReductionOp.is<const Expr *>())
1459       return DSAVarData();
1460     SR = ReductionData.ReductionRange;
1461     ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
1462     assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1463                                        "expression for the descriptor is not "
1464                                        "set.");
1465     TaskgroupDescriptor = I->TaskgroupReductionRef;
1466     return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(),
1467                       Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task);
1468   }
1469   return DSAVarData();
1470 }
1471 
1472 bool DSAStackTy::isOpenMPLocal(VarDecl *D, const_iterator I) const {
1473   D = D->getCanonicalDecl();
1474   for (const_iterator E = end(); I != E; ++I) {
1475     if (isImplicitOrExplicitTaskingRegion(I->Directive) ||
1476         isOpenMPTargetExecutionDirective(I->Directive)) {
1477       Scope *TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
1478       Scope *CurScope = getCurScope();
1479       while (CurScope && CurScope != TopScope && !CurScope->isDeclScope(D))
1480         CurScope = CurScope->getParent();
1481       return CurScope != TopScope;
1482     }
1483   }
1484   return false;
1485 }
1486 
1487 static bool isConstNotMutableType(Sema &SemaRef, QualType Type,
1488                                   bool AcceptIfMutable = true,
1489                                   bool *IsClassType = nullptr) {
1490   ASTContext &Context = SemaRef.getASTContext();
1491   Type = Type.getNonReferenceType().getCanonicalType();
1492   bool IsConstant = Type.isConstant(Context);
1493   Type = Context.getBaseElementType(Type);
1494   const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus
1495                                 ? Type->getAsCXXRecordDecl()
1496                                 : nullptr;
1497   if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
1498     if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
1499       RD = CTD->getTemplatedDecl();
1500   if (IsClassType)
1501     *IsClassType = RD;
1502   return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD &&
1503                          RD->hasDefinition() && RD->hasMutableFields());
1504 }
1505 
1506 static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D,
1507                                       QualType Type, OpenMPClauseKind CKind,
1508                                       SourceLocation ELoc,
1509                                       bool AcceptIfMutable = true,
1510                                       bool ListItemNotVar = false) {
1511   ASTContext &Context = SemaRef.getASTContext();
1512   bool IsClassType;
1513   if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) {
1514     unsigned Diag = ListItemNotVar
1515                         ? diag::err_omp_const_list_item
1516                         : IsClassType ? diag::err_omp_const_not_mutable_variable
1517                                       : diag::err_omp_const_variable;
1518     SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind);
1519     if (!ListItemNotVar && D) {
1520       const VarDecl *VD = dyn_cast<VarDecl>(D);
1521       bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
1522                                VarDecl::DeclarationOnly;
1523       SemaRef.Diag(D->getLocation(),
1524                    IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1525           << D;
1526     }
1527     return true;
1528   }
1529   return false;
1530 }
1531 
1532 const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1533                                                    bool FromParent) {
1534   D = getCanonicalDecl(D);
1535   DSAVarData DVar;
1536 
1537   auto *VD = dyn_cast<VarDecl>(D);
1538   auto TI = Threadprivates.find(D);
1539   if (TI != Threadprivates.end()) {
1540     DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
1541     DVar.CKind = OMPC_threadprivate;
1542     DVar.Modifier = TI->getSecond().Modifier;
1543     return DVar;
1544   }
1545   if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
1546     DVar.RefExpr = buildDeclRefExpr(
1547         SemaRef, VD, D->getType().getNonReferenceType(),
1548         VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
1549     DVar.CKind = OMPC_threadprivate;
1550     addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1551     return DVar;
1552   }
1553   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1554   // in a Construct, C/C++, predetermined, p.1]
1555   //  Variables appearing in threadprivate directives are threadprivate.
1556   if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
1557        !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1558          SemaRef.getLangOpts().OpenMPUseTLS &&
1559          SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
1560       (VD && VD->getStorageClass() == SC_Register &&
1561        VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
1562     DVar.RefExpr = buildDeclRefExpr(
1563         SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation());
1564     DVar.CKind = OMPC_threadprivate;
1565     addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1566     return DVar;
1567   }
1568   if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
1569       VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
1570       !isLoopControlVariable(D).first) {
1571     const_iterator IterTarget =
1572         std::find_if(begin(), end(), [](const SharingMapTy &Data) {
1573           return isOpenMPTargetExecutionDirective(Data.Directive);
1574         });
1575     if (IterTarget != end()) {
1576       const_iterator ParentIterTarget = IterTarget + 1;
1577       for (const_iterator Iter = begin();
1578            Iter != ParentIterTarget; ++Iter) {
1579         if (isOpenMPLocal(VD, Iter)) {
1580           DVar.RefExpr =
1581               buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1582                                D->getLocation());
1583           DVar.CKind = OMPC_threadprivate;
1584           return DVar;
1585         }
1586       }
1587       if (!isClauseParsingMode() || IterTarget != begin()) {
1588         auto DSAIter = IterTarget->SharingMap.find(D);
1589         if (DSAIter != IterTarget->SharingMap.end() &&
1590             isOpenMPPrivate(DSAIter->getSecond().Attributes)) {
1591           DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1592           DVar.CKind = OMPC_threadprivate;
1593           return DVar;
1594         }
1595         const_iterator End = end();
1596         if (!SemaRef.isOpenMPCapturedByRef(
1597                 D, std::distance(ParentIterTarget, End),
1598                 /*OpenMPCaptureLevel=*/0)) {
1599           DVar.RefExpr =
1600               buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1601                                IterTarget->ConstructLoc);
1602           DVar.CKind = OMPC_threadprivate;
1603           return DVar;
1604         }
1605       }
1606     }
1607   }
1608 
1609   if (isStackEmpty())
1610     // Not in OpenMP execution region and top scope was already checked.
1611     return DVar;
1612 
1613   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1614   // in a Construct, C/C++, predetermined, p.4]
1615   //  Static data members are shared.
1616   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1617   // in a Construct, C/C++, predetermined, p.7]
1618   //  Variables with static storage duration that are declared in a scope
1619   //  inside the construct are shared.
1620   if (VD && VD->isStaticDataMember()) {
1621     // Check for explicitly specified attributes.
1622     const_iterator I = begin();
1623     const_iterator EndI = end();
1624     if (FromParent && I != EndI)
1625       ++I;
1626     if (I != EndI) {
1627       auto It = I->SharingMap.find(D);
1628       if (It != I->SharingMap.end()) {
1629         const DSAInfo &Data = It->getSecond();
1630         DVar.RefExpr = Data.RefExpr.getPointer();
1631         DVar.PrivateCopy = Data.PrivateCopy;
1632         DVar.CKind = Data.Attributes;
1633         DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1634         DVar.DKind = I->Directive;
1635         DVar.Modifier = Data.Modifier;
1636         return DVar;
1637       }
1638     }
1639 
1640     DVar.CKind = OMPC_shared;
1641     return DVar;
1642   }
1643 
1644   auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; };
1645   // The predetermined shared attribute for const-qualified types having no
1646   // mutable members was removed after OpenMP 3.1.
1647   if (SemaRef.LangOpts.OpenMP <= 31) {
1648     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1649     // in a Construct, C/C++, predetermined, p.6]
1650     //  Variables with const qualified type having no mutable member are
1651     //  shared.
1652     if (isConstNotMutableType(SemaRef, D->getType())) {
1653       // Variables with const-qualified type having no mutable member may be
1654       // listed in a firstprivate clause, even if they are static data members.
1655       DSAVarData DVarTemp = hasInnermostDSA(
1656           D,
1657           [](OpenMPClauseKind C) {
1658             return C == OMPC_firstprivate || C == OMPC_shared;
1659           },
1660           MatchesAlways, FromParent);
1661       if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
1662         return DVarTemp;
1663 
1664       DVar.CKind = OMPC_shared;
1665       return DVar;
1666     }
1667   }
1668 
1669   // Explicitly specified attributes and local variables with predetermined
1670   // attributes.
1671   const_iterator I = begin();
1672   const_iterator EndI = end();
1673   if (FromParent && I != EndI)
1674     ++I;
1675   if (I == EndI)
1676     return DVar;
1677   auto It = I->SharingMap.find(D);
1678   if (It != I->SharingMap.end()) {
1679     const DSAInfo &Data = It->getSecond();
1680     DVar.RefExpr = Data.RefExpr.getPointer();
1681     DVar.PrivateCopy = Data.PrivateCopy;
1682     DVar.CKind = Data.Attributes;
1683     DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1684     DVar.DKind = I->Directive;
1685     DVar.Modifier = Data.Modifier;
1686   }
1687 
1688   return DVar;
1689 }
1690 
1691 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1692                                                         bool FromParent) const {
1693   if (isStackEmpty()) {
1694     const_iterator I;
1695     return getDSA(I, D);
1696   }
1697   D = getCanonicalDecl(D);
1698   const_iterator StartI = begin();
1699   const_iterator EndI = end();
1700   if (FromParent && StartI != EndI)
1701     ++StartI;
1702   return getDSA(StartI, D);
1703 }
1704 
1705 const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1706                                                         unsigned Level) const {
1707   if (getStackSize() <= Level)
1708     return DSAVarData();
1709   D = getCanonicalDecl(D);
1710   const_iterator StartI = std::next(begin(), getStackSize() - 1 - Level);
1711   return getDSA(StartI, D);
1712 }
1713 
1714 const DSAStackTy::DSAVarData
1715 DSAStackTy::hasDSA(ValueDecl *D,
1716                    const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1717                    const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1718                    bool FromParent) const {
1719   if (isStackEmpty())
1720     return {};
1721   D = getCanonicalDecl(D);
1722   const_iterator I = begin();
1723   const_iterator EndI = end();
1724   if (FromParent && I != EndI)
1725     ++I;
1726   for (; I != EndI; ++I) {
1727     if (!DPred(I->Directive) &&
1728         !isImplicitOrExplicitTaskingRegion(I->Directive))
1729       continue;
1730     const_iterator NewI = I;
1731     DSAVarData DVar = getDSA(NewI, D);
1732     if (I == NewI && CPred(DVar.CKind))
1733       return DVar;
1734   }
1735   return {};
1736 }
1737 
1738 const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
1739     ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1740     const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1741     bool FromParent) const {
1742   if (isStackEmpty())
1743     return {};
1744   D = getCanonicalDecl(D);
1745   const_iterator StartI = begin();
1746   const_iterator EndI = end();
1747   if (FromParent && StartI != EndI)
1748     ++StartI;
1749   if (StartI == EndI || !DPred(StartI->Directive))
1750     return {};
1751   const_iterator NewI = StartI;
1752   DSAVarData DVar = getDSA(NewI, D);
1753   return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData();
1754 }
1755 
1756 bool DSAStackTy::hasExplicitDSA(
1757     const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1758     unsigned Level, bool NotLastprivate) const {
1759   if (getStackSize() <= Level)
1760     return false;
1761   D = getCanonicalDecl(D);
1762   const SharingMapTy &StackElem = getStackElemAtLevel(Level);
1763   auto I = StackElem.SharingMap.find(D);
1764   if (I != StackElem.SharingMap.end() &&
1765       I->getSecond().RefExpr.getPointer() &&
1766       CPred(I->getSecond().Attributes) &&
1767       (!NotLastprivate || !I->getSecond().RefExpr.getInt()))
1768     return true;
1769   // Check predetermined rules for the loop control variables.
1770   auto LI = StackElem.LCVMap.find(D);
1771   if (LI != StackElem.LCVMap.end())
1772     return CPred(OMPC_private);
1773   return false;
1774 }
1775 
1776 bool DSAStackTy::hasExplicitDirective(
1777     const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1778     unsigned Level) const {
1779   if (getStackSize() <= Level)
1780     return false;
1781   const SharingMapTy &StackElem = getStackElemAtLevel(Level);
1782   return DPred(StackElem.Directive);
1783 }
1784 
1785 bool DSAStackTy::hasDirective(
1786     const llvm::function_ref<bool(OpenMPDirectiveKind,
1787                                   const DeclarationNameInfo &, SourceLocation)>
1788         DPred,
1789     bool FromParent) const {
1790   // We look only in the enclosing region.
1791   size_t Skip = FromParent ? 2 : 1;
1792   for (const_iterator I = begin() + std::min(Skip, getStackSize()), E = end();
1793        I != E; ++I) {
1794     if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1795       return true;
1796   }
1797   return false;
1798 }
1799 
1800 void Sema::InitDataSharingAttributesStack() {
1801   VarDataSharingAttributesStack = new DSAStackTy(*this);
1802 }
1803 
1804 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1805 
1806 void Sema::pushOpenMPFunctionRegion() {
1807   DSAStack->pushFunction();
1808 }
1809 
1810 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1811   DSAStack->popFunction(OldFSI);
1812 }
1813 
1814 static bool isOpenMPDeviceDelayedContext(Sema &S) {
1815   assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice &&
1816          "Expected OpenMP device compilation.");
1817   return !S.isInOpenMPTargetExecutionDirective() &&
1818          !S.isInOpenMPDeclareTargetContext();
1819 }
1820 
1821 namespace {
1822 /// Status of the function emission on the host/device.
1823 enum class FunctionEmissionStatus {
1824   Emitted,
1825   Discarded,
1826   Unknown,
1827 };
1828 } // anonymous namespace
1829 
1830 Sema::DeviceDiagBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc,
1831                                                      unsigned DiagID) {
1832   assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice &&
1833          "Expected OpenMP device compilation.");
1834   FunctionEmissionStatus FES = getEmissionStatus(getCurFunctionDecl());
1835   DeviceDiagBuilder::Kind Kind = DeviceDiagBuilder::K_Nop;
1836   switch (FES) {
1837   case FunctionEmissionStatus::Emitted:
1838     Kind = DeviceDiagBuilder::K_Immediate;
1839     break;
1840   case FunctionEmissionStatus::Unknown:
1841     Kind = isOpenMPDeviceDelayedContext(*this) ? DeviceDiagBuilder::K_Deferred
1842                                                : DeviceDiagBuilder::K_Immediate;
1843     break;
1844   case FunctionEmissionStatus::TemplateDiscarded:
1845   case FunctionEmissionStatus::OMPDiscarded:
1846     Kind = DeviceDiagBuilder::K_Nop;
1847     break;
1848   case FunctionEmissionStatus::CUDADiscarded:
1849     llvm_unreachable("CUDADiscarded unexpected in OpenMP device compilation");
1850     break;
1851   }
1852 
1853   return DeviceDiagBuilder(Kind, Loc, DiagID, getCurFunctionDecl(), *this);
1854 }
1855 
1856 Sema::DeviceDiagBuilder Sema::diagIfOpenMPHostCode(SourceLocation Loc,
1857                                                    unsigned DiagID) {
1858   assert(LangOpts.OpenMP && !LangOpts.OpenMPIsDevice &&
1859          "Expected OpenMP host compilation.");
1860   FunctionEmissionStatus FES = getEmissionStatus(getCurFunctionDecl());
1861   DeviceDiagBuilder::Kind Kind = DeviceDiagBuilder::K_Nop;
1862   switch (FES) {
1863   case FunctionEmissionStatus::Emitted:
1864     Kind = DeviceDiagBuilder::K_Immediate;
1865     break;
1866   case FunctionEmissionStatus::Unknown:
1867     Kind = DeviceDiagBuilder::K_Deferred;
1868     break;
1869   case FunctionEmissionStatus::TemplateDiscarded:
1870   case FunctionEmissionStatus::OMPDiscarded:
1871   case FunctionEmissionStatus::CUDADiscarded:
1872     Kind = DeviceDiagBuilder::K_Nop;
1873     break;
1874   }
1875 
1876   return DeviceDiagBuilder(Kind, Loc, DiagID, getCurFunctionDecl(), *this);
1877 }
1878 
1879 void Sema::checkOpenMPDeviceExpr(const Expr *E) {
1880   assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice &&
1881          "OpenMP device compilation mode is expected.");
1882   QualType Ty = E->getType();
1883   if ((Ty->isFloat16Type() && !Context.getTargetInfo().hasFloat16Type()) ||
1884       ((Ty->isFloat128Type() ||
1885         (Ty->isRealFloatingType() && Context.getTypeSize(Ty) == 128)) &&
1886        !Context.getTargetInfo().hasFloat128Type()) ||
1887       (Ty->isIntegerType() && Context.getTypeSize(Ty) == 128 &&
1888        !Context.getTargetInfo().hasInt128Type()))
1889     targetDiag(E->getExprLoc(), diag::err_omp_unsupported_type)
1890         << static_cast<unsigned>(Context.getTypeSize(Ty)) << Ty
1891         << Context.getTargetInfo().getTriple().str() << E->getSourceRange();
1892 }
1893 
1894 static OpenMPDefaultmapClauseKind
1895 getVariableCategoryFromDecl(const LangOptions &LO, const ValueDecl *VD) {
1896   if (LO.OpenMP <= 45) {
1897     if (VD->getType().getNonReferenceType()->isScalarType())
1898       return OMPC_DEFAULTMAP_scalar;
1899     return OMPC_DEFAULTMAP_aggregate;
1900   }
1901   if (VD->getType().getNonReferenceType()->isAnyPointerType())
1902     return OMPC_DEFAULTMAP_pointer;
1903   if (VD->getType().getNonReferenceType()->isScalarType())
1904     return OMPC_DEFAULTMAP_scalar;
1905   return OMPC_DEFAULTMAP_aggregate;
1906 }
1907 
1908 bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level,
1909                                  unsigned OpenMPCaptureLevel) const {
1910   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1911 
1912   ASTContext &Ctx = getASTContext();
1913   bool IsByRef = true;
1914 
1915   // Find the directive that is associated with the provided scope.
1916   D = cast<ValueDecl>(D->getCanonicalDecl());
1917   QualType Ty = D->getType();
1918 
1919   bool IsVariableUsedInMapClause = false;
1920   if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
1921     // This table summarizes how a given variable should be passed to the device
1922     // given its type and the clauses where it appears. This table is based on
1923     // the description in OpenMP 4.5 [2.10.4, target Construct] and
1924     // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
1925     //
1926     // =========================================================================
1927     // | type |  defaultmap   | pvt | first | is_device_ptr |    map   | res.  |
1928     // |      |(tofrom:scalar)|     |  pvt  |               |          |       |
1929     // =========================================================================
1930     // | scl  |               |     |       |       -       |          | bycopy|
1931     // | scl  |               |  -  |   x   |       -       |     -    | bycopy|
1932     // | scl  |               |  x  |   -   |       -       |     -    | null  |
1933     // | scl  |       x       |     |       |       -       |          | byref |
1934     // | scl  |       x       |  -  |   x   |       -       |     -    | bycopy|
1935     // | scl  |       x       |  x  |   -   |       -       |     -    | null  |
1936     // | scl  |               |  -  |   -   |       -       |     x    | byref |
1937     // | scl  |       x       |  -  |   -   |       -       |     x    | byref |
1938     //
1939     // | agg  |      n.a.     |     |       |       -       |          | byref |
1940     // | agg  |      n.a.     |  -  |   x   |       -       |     -    | byref |
1941     // | agg  |      n.a.     |  x  |   -   |       -       |     -    | null  |
1942     // | agg  |      n.a.     |  -  |   -   |       -       |     x    | byref |
1943     // | agg  |      n.a.     |  -  |   -   |       -       |    x[]   | byref |
1944     //
1945     // | ptr  |      n.a.     |     |       |       -       |          | bycopy|
1946     // | ptr  |      n.a.     |  -  |   x   |       -       |     -    | bycopy|
1947     // | ptr  |      n.a.     |  x  |   -   |       -       |     -    | null  |
1948     // | ptr  |      n.a.     |  -  |   -   |       -       |     x    | byref |
1949     // | ptr  |      n.a.     |  -  |   -   |       -       |    x[]   | bycopy|
1950     // | ptr  |      n.a.     |  -  |   -   |       x       |          | bycopy|
1951     // | ptr  |      n.a.     |  -  |   -   |       x       |     x    | bycopy|
1952     // | ptr  |      n.a.     |  -  |   -   |       x       |    x[]   | bycopy|
1953     // =========================================================================
1954     // Legend:
1955     //  scl - scalar
1956     //  ptr - pointer
1957     //  agg - aggregate
1958     //  x - applies
1959     //  - - invalid in this combination
1960     //  [] - mapped with an array section
1961     //  byref - should be mapped by reference
1962     //  byval - should be mapped by value
1963     //  null - initialize a local variable to null on the device
1964     //
1965     // Observations:
1966     //  - All scalar declarations that show up in a map clause have to be passed
1967     //    by reference, because they may have been mapped in the enclosing data
1968     //    environment.
1969     //  - If the scalar value does not fit the size of uintptr, it has to be
1970     //    passed by reference, regardless the result in the table above.
1971     //  - For pointers mapped by value that have either an implicit map or an
1972     //    array section, the runtime library may pass the NULL value to the
1973     //    device instead of the value passed to it by the compiler.
1974 
1975     if (Ty->isReferenceType())
1976       Ty = Ty->castAs<ReferenceType>()->getPointeeType();
1977 
1978     // Locate map clauses and see if the variable being captured is referred to
1979     // in any of those clauses. Here we only care about variables, not fields,
1980     // because fields are part of aggregates.
1981     bool IsVariableAssociatedWithSection = false;
1982 
1983     DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1984         D, Level,
1985         [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D](
1986             OMPClauseMappableExprCommon::MappableExprComponentListRef
1987                 MapExprComponents,
1988             OpenMPClauseKind WhereFoundClauseKind) {
1989           // Only the map clause information influences how a variable is
1990           // captured. E.g. is_device_ptr does not require changing the default
1991           // behavior.
1992           if (WhereFoundClauseKind != OMPC_map)
1993             return false;
1994 
1995           auto EI = MapExprComponents.rbegin();
1996           auto EE = MapExprComponents.rend();
1997 
1998           assert(EI != EE && "Invalid map expression!");
1999 
2000           if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
2001             IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
2002 
2003           ++EI;
2004           if (EI == EE)
2005             return false;
2006 
2007           if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
2008               isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
2009               isa<MemberExpr>(EI->getAssociatedExpression()) ||
2010               isa<OMPArrayShapingExpr>(EI->getAssociatedExpression())) {
2011             IsVariableAssociatedWithSection = true;
2012             // There is nothing more we need to know about this variable.
2013             return true;
2014           }
2015 
2016           // Keep looking for more map info.
2017           return false;
2018         });
2019 
2020     if (IsVariableUsedInMapClause) {
2021       // If variable is identified in a map clause it is always captured by
2022       // reference except if it is a pointer that is dereferenced somehow.
2023       IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
2024     } else {
2025       // By default, all the data that has a scalar type is mapped by copy
2026       // (except for reduction variables).
2027       // Defaultmap scalar is mutual exclusive to defaultmap pointer
2028       IsByRef =
2029           (DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
2030            !Ty->isAnyPointerType()) ||
2031           !Ty->isScalarType() ||
2032           DSAStack->isDefaultmapCapturedByRef(
2033               Level, getVariableCategoryFromDecl(LangOpts, D)) ||
2034           DSAStack->hasExplicitDSA(
2035               D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level);
2036     }
2037   }
2038 
2039   if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
2040     IsByRef =
2041         ((IsVariableUsedInMapClause &&
2042           DSAStack->getCaptureRegion(Level, OpenMPCaptureLevel) ==
2043               OMPD_target) ||
2044          !(DSAStack->hasExplicitDSA(
2045                D,
2046                [](OpenMPClauseKind K) -> bool {
2047                  return K == OMPC_firstprivate;
2048                },
2049                Level, /*NotLastprivate=*/true) ||
2050            DSAStack->isUsesAllocatorsDecl(Level, D))) &&
2051         // If the variable is artificial and must be captured by value - try to
2052         // capture by value.
2053         !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
2054           !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue());
2055   }
2056 
2057   // When passing data by copy, we need to make sure it fits the uintptr size
2058   // and alignment, because the runtime library only deals with uintptr types.
2059   // If it does not fit the uintptr size, we need to pass the data by reference
2060   // instead.
2061   if (!IsByRef &&
2062       (Ctx.getTypeSizeInChars(Ty) >
2063            Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
2064        Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
2065     IsByRef = true;
2066   }
2067 
2068   return IsByRef;
2069 }
2070 
2071 unsigned Sema::getOpenMPNestingLevel() const {
2072   assert(getLangOpts().OpenMP);
2073   return DSAStack->getNestingLevel();
2074 }
2075 
2076 bool Sema::isInOpenMPTargetExecutionDirective() const {
2077   return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
2078           !DSAStack->isClauseParsingMode()) ||
2079          DSAStack->hasDirective(
2080              [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
2081                 SourceLocation) -> bool {
2082                return isOpenMPTargetExecutionDirective(K);
2083              },
2084              false);
2085 }
2086 
2087 VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo,
2088                                     unsigned StopAt) {
2089   assert(LangOpts.OpenMP && "OpenMP is not allowed");
2090   D = getCanonicalDecl(D);
2091 
2092   auto *VD = dyn_cast<VarDecl>(D);
2093   // Do not capture constexpr variables.
2094   if (VD && VD->isConstexpr())
2095     return nullptr;
2096 
2097   // If we want to determine whether the variable should be captured from the
2098   // perspective of the current capturing scope, and we've already left all the
2099   // capturing scopes of the top directive on the stack, check from the
2100   // perspective of its parent directive (if any) instead.
2101   DSAStackTy::ParentDirectiveScope InParentDirectiveRAII(
2102       *DSAStack, CheckScopeInfo && DSAStack->isBodyComplete());
2103 
2104   // If we are attempting to capture a global variable in a directive with
2105   // 'target' we return true so that this global is also mapped to the device.
2106   //
2107   if (VD && !VD->hasLocalStorage() &&
2108       (getCurCapturedRegion() || getCurBlock() || getCurLambda())) {
2109     if (isInOpenMPDeclareTargetContext()) {
2110       // Try to mark variable as declare target if it is used in capturing
2111       // regions.
2112       if (LangOpts.OpenMP <= 45 &&
2113           !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
2114         checkDeclIsAllowedInOpenMPTarget(nullptr, VD);
2115       return nullptr;
2116     } else if (isInOpenMPTargetExecutionDirective()) {
2117       // If the declaration is enclosed in a 'declare target' directive,
2118       // then it should not be captured.
2119       //
2120       if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
2121         return nullptr;
2122       CapturedRegionScopeInfo *CSI = nullptr;
2123       for (FunctionScopeInfo *FSI : llvm::drop_begin(
2124                llvm::reverse(FunctionScopes),
2125                CheckScopeInfo ? (FunctionScopes.size() - (StopAt + 1)) : 0)) {
2126         if (!isa<CapturingScopeInfo>(FSI))
2127           return nullptr;
2128         if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI))
2129           if (RSI->CapRegionKind == CR_OpenMP) {
2130             CSI = RSI;
2131             break;
2132           }
2133       }
2134       SmallVector<OpenMPDirectiveKind, 4> Regions;
2135       getOpenMPCaptureRegions(Regions,
2136                               DSAStack->getDirective(CSI->OpenMPLevel));
2137       if (Regions[CSI->OpenMPCaptureLevel] != OMPD_task)
2138         return VD;
2139     }
2140   }
2141 
2142   if (CheckScopeInfo) {
2143     bool OpenMPFound = false;
2144     for (unsigned I = StopAt + 1; I > 0; --I) {
2145       FunctionScopeInfo *FSI = FunctionScopes[I - 1];
2146       if(!isa<CapturingScopeInfo>(FSI))
2147         return nullptr;
2148       if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI))
2149         if (RSI->CapRegionKind == CR_OpenMP) {
2150           OpenMPFound = true;
2151           break;
2152         }
2153     }
2154     if (!OpenMPFound)
2155       return nullptr;
2156   }
2157 
2158   if (DSAStack->getCurrentDirective() != OMPD_unknown &&
2159       (!DSAStack->isClauseParsingMode() ||
2160        DSAStack->getParentDirective() != OMPD_unknown)) {
2161     auto &&Info = DSAStack->isLoopControlVariable(D);
2162     if (Info.first ||
2163         (VD && VD->hasLocalStorage() &&
2164          isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) ||
2165         (VD && DSAStack->isForceVarCapturing()))
2166       return VD ? VD : Info.second;
2167     DSAStackTy::DSAVarData DVarTop =
2168         DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
2169     if (DVarTop.CKind != OMPC_unknown && isOpenMPPrivate(DVarTop.CKind))
2170       return VD ? VD : cast<VarDecl>(DVarTop.PrivateCopy->getDecl());
2171     // Threadprivate variables must not be captured.
2172     if (isOpenMPThreadPrivate(DVarTop.CKind))
2173       return nullptr;
2174     // The variable is not private or it is the variable in the directive with
2175     // default(none) clause and not used in any clause.
2176     DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA(
2177         D, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; },
2178         DSAStack->isClauseParsingMode());
2179     // Global shared must not be captured.
2180     if (VD && !VD->hasLocalStorage() && DVarPrivate.CKind == OMPC_unknown &&
2181         (DSAStack->getDefaultDSA() != DSA_none || DVarTop.CKind == OMPC_shared))
2182       return nullptr;
2183     if (DVarPrivate.CKind != OMPC_unknown ||
2184         (VD && DSAStack->getDefaultDSA() == DSA_none))
2185       return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
2186   }
2187   return nullptr;
2188 }
2189 
2190 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
2191                                         unsigned Level) const {
2192   FunctionScopesIndex -= getOpenMPCaptureLevels(DSAStack->getDirective(Level));
2193 }
2194 
2195 void Sema::startOpenMPLoop() {
2196   assert(LangOpts.OpenMP && "OpenMP must be enabled.");
2197   if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()))
2198     DSAStack->loopInit();
2199 }
2200 
2201 void Sema::startOpenMPCXXRangeFor() {
2202   assert(LangOpts.OpenMP && "OpenMP must be enabled.");
2203   if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2204     DSAStack->resetPossibleLoopCounter();
2205     DSAStack->loopStart();
2206   }
2207 }
2208 
2209 OpenMPClauseKind Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level,
2210                                            unsigned CapLevel) const {
2211   assert(LangOpts.OpenMP && "OpenMP is not allowed");
2212   if (DSAStack->hasExplicitDirective(
2213           [](OpenMPDirectiveKind K) { return isOpenMPTaskingDirective(K); },
2214           Level)) {
2215     bool IsTriviallyCopyable =
2216         D->getType().getNonReferenceType().isTriviallyCopyableType(Context);
2217     OpenMPDirectiveKind DKind = DSAStack->getDirective(Level);
2218     SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2219     getOpenMPCaptureRegions(CaptureRegions, DKind);
2220     if (isOpenMPTaskingDirective(CaptureRegions[CapLevel]) &&
2221         (IsTriviallyCopyable ||
2222          !isOpenMPTaskLoopDirective(CaptureRegions[CapLevel]))) {
2223       if (DSAStack->hasExplicitDSA(
2224               D, [](OpenMPClauseKind K) { return K == OMPC_firstprivate; },
2225               Level, /*NotLastprivate=*/true))
2226         return OMPC_firstprivate;
2227       DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level);
2228       if (DVar.CKind != OMPC_shared &&
2229           !DSAStack->isLoopControlVariable(D, Level).first && !DVar.RefExpr) {
2230         DSAStack->addImplicitTaskFirstprivate(Level, D);
2231         return OMPC_firstprivate;
2232       }
2233     }
2234   }
2235   if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2236     if (DSAStack->getAssociatedLoops() > 0 &&
2237         !DSAStack->isLoopStarted()) {
2238       DSAStack->resetPossibleLoopCounter(D);
2239       DSAStack->loopStart();
2240       return OMPC_private;
2241     }
2242     if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() ||
2243          DSAStack->isLoopControlVariable(D).first) &&
2244         !DSAStack->hasExplicitDSA(
2245             D, [](OpenMPClauseKind K) { return K != OMPC_private; }, Level) &&
2246         !isOpenMPSimdDirective(DSAStack->getCurrentDirective()))
2247       return OMPC_private;
2248   }
2249   if (const auto *VD = dyn_cast<VarDecl>(D)) {
2250     if (DSAStack->isThreadPrivate(const_cast<VarDecl *>(VD)) &&
2251         DSAStack->isForceVarCapturing() &&
2252         !DSAStack->hasExplicitDSA(
2253             D, [](OpenMPClauseKind K) { return K == OMPC_copyin; }, Level))
2254       return OMPC_private;
2255   }
2256   // User-defined allocators are private since they must be defined in the
2257   // context of target region.
2258   if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level) &&
2259       DSAStack->isUsesAllocatorsDecl(Level, D).getValueOr(
2260           DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) ==
2261           DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator)
2262     return OMPC_private;
2263   return (DSAStack->hasExplicitDSA(
2264               D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) ||
2265           (DSAStack->isClauseParsingMode() &&
2266            DSAStack->getClauseParsingMode() == OMPC_private) ||
2267           // Consider taskgroup reduction descriptor variable a private
2268           // to avoid possible capture in the region.
2269           (DSAStack->hasExplicitDirective(
2270                [](OpenMPDirectiveKind K) {
2271                  return K == OMPD_taskgroup ||
2272                         ((isOpenMPParallelDirective(K) ||
2273                           isOpenMPWorksharingDirective(K)) &&
2274                          !isOpenMPSimdDirective(K));
2275                },
2276                Level) &&
2277            DSAStack->isTaskgroupReductionRef(D, Level)))
2278              ? OMPC_private
2279              : OMPC_unknown;
2280 }
2281 
2282 void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
2283                                 unsigned Level) {
2284   assert(LangOpts.OpenMP && "OpenMP is not allowed");
2285   D = getCanonicalDecl(D);
2286   OpenMPClauseKind OMPC = OMPC_unknown;
2287   for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
2288     const unsigned NewLevel = I - 1;
2289     if (DSAStack->hasExplicitDSA(D,
2290                                  [&OMPC](const OpenMPClauseKind K) {
2291                                    if (isOpenMPPrivate(K)) {
2292                                      OMPC = K;
2293                                      return true;
2294                                    }
2295                                    return false;
2296                                  },
2297                                  NewLevel))
2298       break;
2299     if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
2300             D, NewLevel,
2301             [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
2302                OpenMPClauseKind) { return true; })) {
2303       OMPC = OMPC_map;
2304       break;
2305     }
2306     if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
2307                                        NewLevel)) {
2308       OMPC = OMPC_map;
2309       if (DSAStack->mustBeFirstprivateAtLevel(
2310               NewLevel, getVariableCategoryFromDecl(LangOpts, D)))
2311         OMPC = OMPC_firstprivate;
2312       break;
2313     }
2314   }
2315   if (OMPC != OMPC_unknown)
2316     FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, unsigned(OMPC)));
2317 }
2318 
2319 bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D, unsigned Level,
2320                                       unsigned CaptureLevel) const {
2321   assert(LangOpts.OpenMP && "OpenMP is not allowed");
2322   // Return true if the current level is no longer enclosed in a target region.
2323 
2324   SmallVector<OpenMPDirectiveKind, 4> Regions;
2325   getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
2326   const auto *VD = dyn_cast<VarDecl>(D);
2327   return VD && !VD->hasLocalStorage() &&
2328          DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
2329                                         Level) &&
2330          Regions[CaptureLevel] != OMPD_task;
2331 }
2332 
2333 bool Sema::isOpenMPGlobalCapturedDecl(ValueDecl *D, unsigned Level,
2334                                       unsigned CaptureLevel) const {
2335   assert(LangOpts.OpenMP && "OpenMP is not allowed");
2336   // Return true if the current level is no longer enclosed in a target region.
2337 
2338   if (const auto *VD = dyn_cast<VarDecl>(D)) {
2339     if (!VD->hasLocalStorage()) {
2340       DSAStackTy::DSAVarData TopDVar =
2341           DSAStack->getTopDSA(D, /*FromParent=*/false);
2342       unsigned NumLevels =
2343           getOpenMPCaptureLevels(DSAStack->getDirective(Level));
2344       if (Level == 0)
2345         return (NumLevels == CaptureLevel + 1) && TopDVar.CKind != OMPC_shared;
2346       DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level - 1);
2347       return DVar.CKind != OMPC_shared ||
2348              isOpenMPGlobalCapturedDecl(
2349                  D, Level - 1,
2350                  getOpenMPCaptureLevels(DSAStack->getDirective(Level - 1)) - 1);
2351     }
2352   }
2353   return true;
2354 }
2355 
2356 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
2357 
2358 void Sema::ActOnOpenMPBeginDeclareVariant(SourceLocation Loc,
2359                                           OMPTraitInfo &TI) {
2360   if (!OMPDeclareVariantScopes.empty()) {
2361     Diag(Loc, diag::warn_nested_declare_variant);
2362     return;
2363   }
2364   OMPDeclareVariantScopes.push_back(OMPDeclareVariantScope(TI));
2365 }
2366 
2367 void Sema::ActOnOpenMPEndDeclareVariant() {
2368   assert(isInOpenMPDeclareVariantScope() &&
2369          "Not in OpenMP declare variant scope!");
2370 
2371   OMPDeclareVariantScopes.pop_back();
2372 }
2373 
2374 void Sema::finalizeOpenMPDelayedAnalysis(const FunctionDecl *Caller,
2375                                          const FunctionDecl *Callee,
2376                                          SourceLocation Loc) {
2377   assert(LangOpts.OpenMP && "Expected OpenMP compilation mode.");
2378   Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
2379       OMPDeclareTargetDeclAttr::getDeviceType(Caller->getMostRecentDecl());
2380   // Ignore host functions during device analyzis.
2381   if (LangOpts.OpenMPIsDevice && DevTy &&
2382       *DevTy == OMPDeclareTargetDeclAttr::DT_Host)
2383     return;
2384   // Ignore nohost functions during host analyzis.
2385   if (!LangOpts.OpenMPIsDevice && DevTy &&
2386       *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost)
2387     return;
2388   const FunctionDecl *FD = Callee->getMostRecentDecl();
2389   DevTy = OMPDeclareTargetDeclAttr::getDeviceType(FD);
2390   if (LangOpts.OpenMPIsDevice && DevTy &&
2391       *DevTy == OMPDeclareTargetDeclAttr::DT_Host) {
2392     // Diagnose host function called during device codegen.
2393     StringRef HostDevTy =
2394         getOpenMPSimpleClauseTypeName(OMPC_device_type, OMPC_DEVICE_TYPE_host);
2395     Diag(Loc, diag::err_omp_wrong_device_function_call) << HostDevTy << 0;
2396     Diag(FD->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(),
2397          diag::note_omp_marked_device_type_here)
2398         << HostDevTy;
2399     return;
2400   }
2401       if (!LangOpts.OpenMPIsDevice && DevTy &&
2402           *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) {
2403         // Diagnose nohost function called during host codegen.
2404         StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName(
2405             OMPC_device_type, OMPC_DEVICE_TYPE_nohost);
2406         Diag(Loc, diag::err_omp_wrong_device_function_call) << NoHostDevTy << 1;
2407         Diag(FD->getAttr<OMPDeclareTargetDeclAttr>()->getLocation(),
2408              diag::note_omp_marked_device_type_here)
2409             << NoHostDevTy;
2410       }
2411 }
2412 
2413 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
2414                                const DeclarationNameInfo &DirName,
2415                                Scope *CurScope, SourceLocation Loc) {
2416   DSAStack->push(DKind, DirName, CurScope, Loc);
2417   PushExpressionEvaluationContext(
2418       ExpressionEvaluationContext::PotentiallyEvaluated);
2419 }
2420 
2421 void Sema::StartOpenMPClause(OpenMPClauseKind K) {
2422   DSAStack->setClauseParsingMode(K);
2423 }
2424 
2425 void Sema::EndOpenMPClause() {
2426   DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
2427 }
2428 
2429 static std::pair<ValueDecl *, bool>
2430 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
2431                SourceRange &ERange, bool AllowArraySection = false);
2432 
2433 /// Check consistency of the reduction clauses.
2434 static void checkReductionClauses(Sema &S, DSAStackTy *Stack,
2435                                   ArrayRef<OMPClause *> Clauses) {
2436   bool InscanFound = false;
2437   SourceLocation InscanLoc;
2438   // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions.
2439   // A reduction clause without the inscan reduction-modifier may not appear on
2440   // a construct on which a reduction clause with the inscan reduction-modifier
2441   // appears.
2442   for (OMPClause *C : Clauses) {
2443     if (C->getClauseKind() != OMPC_reduction)
2444       continue;
2445     auto *RC = cast<OMPReductionClause>(C);
2446     if (RC->getModifier() == OMPC_REDUCTION_inscan) {
2447       InscanFound = true;
2448       InscanLoc = RC->getModifierLoc();
2449       continue;
2450     }
2451     if (RC->getModifier() == OMPC_REDUCTION_task) {
2452       // OpenMP 5.0, 2.19.5.4 reduction Clause.
2453       // A reduction clause with the task reduction-modifier may only appear on
2454       // a parallel construct, a worksharing construct or a combined or
2455       // composite construct for which any of the aforementioned constructs is a
2456       // constituent construct and simd or loop are not constituent constructs.
2457       OpenMPDirectiveKind CurDir = Stack->getCurrentDirective();
2458       if (!(isOpenMPParallelDirective(CurDir) ||
2459             isOpenMPWorksharingDirective(CurDir)) ||
2460           isOpenMPSimdDirective(CurDir))
2461         S.Diag(RC->getModifierLoc(),
2462                diag::err_omp_reduction_task_not_parallel_or_worksharing);
2463       continue;
2464     }
2465   }
2466   if (InscanFound) {
2467     for (OMPClause *C : Clauses) {
2468       if (C->getClauseKind() != OMPC_reduction)
2469         continue;
2470       auto *RC = cast<OMPReductionClause>(C);
2471       if (RC->getModifier() != OMPC_REDUCTION_inscan) {
2472         S.Diag(RC->getModifier() == OMPC_REDUCTION_unknown
2473                    ? RC->getBeginLoc()
2474                    : RC->getModifierLoc(),
2475                diag::err_omp_inscan_reduction_expected);
2476         S.Diag(InscanLoc, diag::note_omp_previous_inscan_reduction);
2477         continue;
2478       }
2479       for (Expr *Ref : RC->varlists()) {
2480         assert(Ref && "NULL expr in OpenMP nontemporal clause.");
2481         SourceLocation ELoc;
2482         SourceRange ERange;
2483         Expr *SimpleRefExpr = Ref;
2484         auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
2485                                   /*AllowArraySection=*/true);
2486         ValueDecl *D = Res.first;
2487         if (!D)
2488           continue;
2489         if (!Stack->isUsedInScanDirective(getCanonicalDecl(D))) {
2490           S.Diag(Ref->getExprLoc(),
2491                  diag::err_omp_reduction_not_inclusive_exclusive)
2492               << Ref->getSourceRange();
2493         }
2494       }
2495     }
2496   }
2497 }
2498 
2499 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
2500                                  ArrayRef<OMPClause *> Clauses);
2501 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
2502                                  bool WithInit);
2503 
2504 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
2505                               const ValueDecl *D,
2506                               const DSAStackTy::DSAVarData &DVar,
2507                               bool IsLoopIterVar = false);
2508 
2509 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
2510   // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
2511   //  A variable of class type (or array thereof) that appears in a lastprivate
2512   //  clause requires an accessible, unambiguous default constructor for the
2513   //  class type, unless the list item is also specified in a firstprivate
2514   //  clause.
2515   if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
2516     for (OMPClause *C : D->clauses()) {
2517       if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
2518         SmallVector<Expr *, 8> PrivateCopies;
2519         for (Expr *DE : Clause->varlists()) {
2520           if (DE->isValueDependent() || DE->isTypeDependent()) {
2521             PrivateCopies.push_back(nullptr);
2522             continue;
2523           }
2524           auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
2525           auto *VD = cast<VarDecl>(DRE->getDecl());
2526           QualType Type = VD->getType().getNonReferenceType();
2527           const DSAStackTy::DSAVarData DVar =
2528               DSAStack->getTopDSA(VD, /*FromParent=*/false);
2529           if (DVar.CKind == OMPC_lastprivate) {
2530             // Generate helper private variable and initialize it with the
2531             // default value. The address of the original variable is replaced
2532             // by the address of the new private variable in CodeGen. This new
2533             // variable is not added to IdResolver, so the code in the OpenMP
2534             // region uses original variable for proper diagnostics.
2535             VarDecl *VDPrivate = buildVarDecl(
2536                 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
2537                 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE);
2538             ActOnUninitializedDecl(VDPrivate);
2539             if (VDPrivate->isInvalidDecl()) {
2540               PrivateCopies.push_back(nullptr);
2541               continue;
2542             }
2543             PrivateCopies.push_back(buildDeclRefExpr(
2544                 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
2545           } else {
2546             // The variable is also a firstprivate, so initialization sequence
2547             // for private copy is generated already.
2548             PrivateCopies.push_back(nullptr);
2549           }
2550         }
2551         Clause->setPrivateCopies(PrivateCopies);
2552         continue;
2553       }
2554       // Finalize nontemporal clause by handling private copies, if any.
2555       if (auto *Clause = dyn_cast<OMPNontemporalClause>(C)) {
2556         SmallVector<Expr *, 8> PrivateRefs;
2557         for (Expr *RefExpr : Clause->varlists()) {
2558           assert(RefExpr && "NULL expr in OpenMP nontemporal clause.");
2559           SourceLocation ELoc;
2560           SourceRange ERange;
2561           Expr *SimpleRefExpr = RefExpr;
2562           auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
2563           if (Res.second)
2564             // It will be analyzed later.
2565             PrivateRefs.push_back(RefExpr);
2566           ValueDecl *D = Res.first;
2567           if (!D)
2568             continue;
2569 
2570           const DSAStackTy::DSAVarData DVar =
2571               DSAStack->getTopDSA(D, /*FromParent=*/false);
2572           PrivateRefs.push_back(DVar.PrivateCopy ? DVar.PrivateCopy
2573                                                  : SimpleRefExpr);
2574         }
2575         Clause->setPrivateRefs(PrivateRefs);
2576         continue;
2577       }
2578       if (auto *Clause = dyn_cast<OMPUsesAllocatorsClause>(C)) {
2579         for (unsigned I = 0, E = Clause->getNumberOfAllocators(); I < E; ++I) {
2580           OMPUsesAllocatorsClause::Data D = Clause->getAllocatorData(I);
2581           auto *DRE = dyn_cast<DeclRefExpr>(D.Allocator->IgnoreParenImpCasts());
2582           if (!DRE)
2583             continue;
2584           ValueDecl *VD = DRE->getDecl();
2585           if (!VD || !isa<VarDecl>(VD))
2586             continue;
2587           DSAStackTy::DSAVarData DVar =
2588               DSAStack->getTopDSA(VD, /*FromParent=*/false);
2589           // OpenMP [2.12.5, target Construct]
2590           // Memory allocators that appear in a uses_allocators clause cannot
2591           // appear in other data-sharing attribute clauses or data-mapping
2592           // attribute clauses in the same construct.
2593           Expr *MapExpr = nullptr;
2594           if (DVar.RefExpr ||
2595               DSAStack->checkMappableExprComponentListsForDecl(
2596                   VD, /*CurrentRegionOnly=*/true,
2597                   [VD, &MapExpr](
2598                       OMPClauseMappableExprCommon::MappableExprComponentListRef
2599                           MapExprComponents,
2600                       OpenMPClauseKind C) {
2601                     auto MI = MapExprComponents.rbegin();
2602                     auto ME = MapExprComponents.rend();
2603                     if (MI != ME &&
2604                         MI->getAssociatedDeclaration()->getCanonicalDecl() ==
2605                             VD->getCanonicalDecl()) {
2606                       MapExpr = MI->getAssociatedExpression();
2607                       return true;
2608                     }
2609                     return false;
2610                   })) {
2611             Diag(D.Allocator->getExprLoc(),
2612                  diag::err_omp_allocator_used_in_clauses)
2613                 << D.Allocator->getSourceRange();
2614             if (DVar.RefExpr)
2615               reportOriginalDsa(*this, DSAStack, VD, DVar);
2616             else
2617               Diag(MapExpr->getExprLoc(), diag::note_used_here)
2618                   << MapExpr->getSourceRange();
2619           }
2620         }
2621         continue;
2622       }
2623     }
2624     // Check allocate clauses.
2625     if (!CurContext->isDependentContext())
2626       checkAllocateClauses(*this, DSAStack, D->clauses());
2627     checkReductionClauses(*this, DSAStack, D->clauses());
2628   }
2629 
2630   DSAStack->pop();
2631   DiscardCleanupsInEvaluationContext();
2632   PopExpressionEvaluationContext();
2633 }
2634 
2635 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
2636                                      Expr *NumIterations, Sema &SemaRef,
2637                                      Scope *S, DSAStackTy *Stack);
2638 
2639 namespace {
2640 
2641 class VarDeclFilterCCC final : public CorrectionCandidateCallback {
2642 private:
2643   Sema &SemaRef;
2644 
2645 public:
2646   explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
2647   bool ValidateCandidate(const TypoCorrection &Candidate) override {
2648     NamedDecl *ND = Candidate.getCorrectionDecl();
2649     if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
2650       return VD->hasGlobalStorage() &&
2651              SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
2652                                    SemaRef.getCurScope());
2653     }
2654     return false;
2655   }
2656 
2657   std::unique_ptr<CorrectionCandidateCallback> clone() override {
2658     return std::make_unique<VarDeclFilterCCC>(*this);
2659   }
2660 
2661 };
2662 
2663 class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
2664 private:
2665   Sema &SemaRef;
2666 
2667 public:
2668   explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
2669   bool ValidateCandidate(const TypoCorrection &Candidate) override {
2670     NamedDecl *ND = Candidate.getCorrectionDecl();
2671     if (ND && ((isa<VarDecl>(ND) && ND->getKind() == Decl::Var) ||
2672                isa<FunctionDecl>(ND))) {
2673       return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
2674                                    SemaRef.getCurScope());
2675     }
2676     return false;
2677   }
2678 
2679   std::unique_ptr<CorrectionCandidateCallback> clone() override {
2680     return std::make_unique<VarOrFuncDeclFilterCCC>(*this);
2681   }
2682 };
2683 
2684 } // namespace
2685 
2686 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
2687                                          CXXScopeSpec &ScopeSpec,
2688                                          const DeclarationNameInfo &Id,
2689                                          OpenMPDirectiveKind Kind) {
2690   LookupResult Lookup(*this, Id, LookupOrdinaryName);
2691   LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
2692 
2693   if (Lookup.isAmbiguous())
2694     return ExprError();
2695 
2696   VarDecl *VD;
2697   if (!Lookup.isSingleResult()) {
2698     VarDeclFilterCCC CCC(*this);
2699     if (TypoCorrection Corrected =
2700             CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC,
2701                         CTK_ErrorRecovery)) {
2702       diagnoseTypo(Corrected,
2703                    PDiag(Lookup.empty()
2704                              ? diag::err_undeclared_var_use_suggest
2705                              : diag::err_omp_expected_var_arg_suggest)
2706                        << Id.getName());
2707       VD = Corrected.getCorrectionDeclAs<VarDecl>();
2708     } else {
2709       Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
2710                                        : diag::err_omp_expected_var_arg)
2711           << Id.getName();
2712       return ExprError();
2713     }
2714   } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
2715     Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
2716     Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
2717     return ExprError();
2718   }
2719   Lookup.suppressDiagnostics();
2720 
2721   // OpenMP [2.9.2, Syntax, C/C++]
2722   //   Variables must be file-scope, namespace-scope, or static block-scope.
2723   if (Kind == OMPD_threadprivate && !VD->hasGlobalStorage()) {
2724     Diag(Id.getLoc(), diag::err_omp_global_var_arg)
2725         << getOpenMPDirectiveName(Kind) << !VD->isStaticLocal();
2726     bool IsDecl =
2727         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2728     Diag(VD->getLocation(),
2729          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2730         << VD;
2731     return ExprError();
2732   }
2733 
2734   VarDecl *CanonicalVD = VD->getCanonicalDecl();
2735   NamedDecl *ND = CanonicalVD;
2736   // OpenMP [2.9.2, Restrictions, C/C++, p.2]
2737   //   A threadprivate directive for file-scope variables must appear outside
2738   //   any definition or declaration.
2739   if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
2740       !getCurLexicalContext()->isTranslationUnit()) {
2741     Diag(Id.getLoc(), diag::err_omp_var_scope)
2742         << getOpenMPDirectiveName(Kind) << VD;
2743     bool IsDecl =
2744         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2745     Diag(VD->getLocation(),
2746          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2747         << VD;
2748     return ExprError();
2749   }
2750   // OpenMP [2.9.2, Restrictions, C/C++, p.3]
2751   //   A threadprivate directive for static class member variables must appear
2752   //   in the class definition, in the same scope in which the member
2753   //   variables are declared.
2754   if (CanonicalVD->isStaticDataMember() &&
2755       !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
2756     Diag(Id.getLoc(), diag::err_omp_var_scope)
2757         << getOpenMPDirectiveName(Kind) << VD;
2758     bool IsDecl =
2759         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2760     Diag(VD->getLocation(),
2761          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2762         << VD;
2763     return ExprError();
2764   }
2765   // OpenMP [2.9.2, Restrictions, C/C++, p.4]
2766   //   A threadprivate directive for namespace-scope variables must appear
2767   //   outside any definition or declaration other than the namespace
2768   //   definition itself.
2769   if (CanonicalVD->getDeclContext()->isNamespace() &&
2770       (!getCurLexicalContext()->isFileContext() ||
2771        !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
2772     Diag(Id.getLoc(), diag::err_omp_var_scope)
2773         << getOpenMPDirectiveName(Kind) << VD;
2774     bool IsDecl =
2775         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2776     Diag(VD->getLocation(),
2777          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2778         << VD;
2779     return ExprError();
2780   }
2781   // OpenMP [2.9.2, Restrictions, C/C++, p.6]
2782   //   A threadprivate directive for static block-scope variables must appear
2783   //   in the scope of the variable and not in a nested scope.
2784   if (CanonicalVD->isLocalVarDecl() && CurScope &&
2785       !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
2786     Diag(Id.getLoc(), diag::err_omp_var_scope)
2787         << getOpenMPDirectiveName(Kind) << VD;
2788     bool IsDecl =
2789         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2790     Diag(VD->getLocation(),
2791          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2792         << VD;
2793     return ExprError();
2794   }
2795 
2796   // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
2797   //   A threadprivate directive must lexically precede all references to any
2798   //   of the variables in its list.
2799   if (Kind == OMPD_threadprivate && VD->isUsed() &&
2800       !DSAStack->isThreadPrivate(VD)) {
2801     Diag(Id.getLoc(), diag::err_omp_var_used)
2802         << getOpenMPDirectiveName(Kind) << VD;
2803     return ExprError();
2804   }
2805 
2806   QualType ExprType = VD->getType().getNonReferenceType();
2807   return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
2808                              SourceLocation(), VD,
2809                              /*RefersToEnclosingVariableOrCapture=*/false,
2810                              Id.getLoc(), ExprType, VK_LValue);
2811 }
2812 
2813 Sema::DeclGroupPtrTy
2814 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
2815                                         ArrayRef<Expr *> VarList) {
2816   if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
2817     CurContext->addDecl(D);
2818     return DeclGroupPtrTy::make(DeclGroupRef(D));
2819   }
2820   return nullptr;
2821 }
2822 
2823 namespace {
2824 class LocalVarRefChecker final
2825     : public ConstStmtVisitor<LocalVarRefChecker, bool> {
2826   Sema &SemaRef;
2827 
2828 public:
2829   bool VisitDeclRefExpr(const DeclRefExpr *E) {
2830     if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
2831       if (VD->hasLocalStorage()) {
2832         SemaRef.Diag(E->getBeginLoc(),
2833                      diag::err_omp_local_var_in_threadprivate_init)
2834             << E->getSourceRange();
2835         SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
2836             << VD << VD->getSourceRange();
2837         return true;
2838       }
2839     }
2840     return false;
2841   }
2842   bool VisitStmt(const Stmt *S) {
2843     for (const Stmt *Child : S->children()) {
2844       if (Child && Visit(Child))
2845         return true;
2846     }
2847     return false;
2848   }
2849   explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
2850 };
2851 } // namespace
2852 
2853 OMPThreadPrivateDecl *
2854 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
2855   SmallVector<Expr *, 8> Vars;
2856   for (Expr *RefExpr : VarList) {
2857     auto *DE = cast<DeclRefExpr>(RefExpr);
2858     auto *VD = cast<VarDecl>(DE->getDecl());
2859     SourceLocation ILoc = DE->getExprLoc();
2860 
2861     // Mark variable as used.
2862     VD->setReferenced();
2863     VD->markUsed(Context);
2864 
2865     QualType QType = VD->getType();
2866     if (QType->isDependentType() || QType->isInstantiationDependentType()) {
2867       // It will be analyzed later.
2868       Vars.push_back(DE);
2869       continue;
2870     }
2871 
2872     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2873     //   A threadprivate variable must not have an incomplete type.
2874     if (RequireCompleteType(ILoc, VD->getType(),
2875                             diag::err_omp_threadprivate_incomplete_type)) {
2876       continue;
2877     }
2878 
2879     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2880     //   A threadprivate variable must not have a reference type.
2881     if (VD->getType()->isReferenceType()) {
2882       Diag(ILoc, diag::err_omp_ref_type_arg)
2883           << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
2884       bool IsDecl =
2885           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2886       Diag(VD->getLocation(),
2887            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2888           << VD;
2889       continue;
2890     }
2891 
2892     // Check if this is a TLS variable. If TLS is not being supported, produce
2893     // the corresponding diagnostic.
2894     if ((VD->getTLSKind() != VarDecl::TLS_None &&
2895          !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
2896            getLangOpts().OpenMPUseTLS &&
2897            getASTContext().getTargetInfo().isTLSSupported())) ||
2898         (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
2899          !VD->isLocalVarDecl())) {
2900       Diag(ILoc, diag::err_omp_var_thread_local)
2901           << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
2902       bool IsDecl =
2903           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2904       Diag(VD->getLocation(),
2905            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2906           << VD;
2907       continue;
2908     }
2909 
2910     // Check if initial value of threadprivate variable reference variable with
2911     // local storage (it is not supported by runtime).
2912     if (const Expr *Init = VD->getAnyInitializer()) {
2913       LocalVarRefChecker Checker(*this);
2914       if (Checker.Visit(Init))
2915         continue;
2916     }
2917 
2918     Vars.push_back(RefExpr);
2919     DSAStack->addDSA(VD, DE, OMPC_threadprivate);
2920     VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
2921         Context, SourceRange(Loc, Loc)));
2922     if (ASTMutationListener *ML = Context.getASTMutationListener())
2923       ML->DeclarationMarkedOpenMPThreadPrivate(VD);
2924   }
2925   OMPThreadPrivateDecl *D = nullptr;
2926   if (!Vars.empty()) {
2927     D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
2928                                      Vars);
2929     D->setAccess(AS_public);
2930   }
2931   return D;
2932 }
2933 
2934 static OMPAllocateDeclAttr::AllocatorTypeTy
2935 getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) {
2936   if (!Allocator)
2937     return OMPAllocateDeclAttr::OMPNullMemAlloc;
2938   if (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
2939       Allocator->isInstantiationDependent() ||
2940       Allocator->containsUnexpandedParameterPack())
2941     return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
2942   auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
2943   const Expr *AE = Allocator->IgnoreParenImpCasts();
2944   for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
2945     auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
2946     const Expr *DefAllocator = Stack->getAllocator(AllocatorKind);
2947     llvm::FoldingSetNodeID AEId, DAEId;
2948     AE->Profile(AEId, S.getASTContext(), /*Canonical=*/true);
2949     DefAllocator->Profile(DAEId, S.getASTContext(), /*Canonical=*/true);
2950     if (AEId == DAEId) {
2951       AllocatorKindRes = AllocatorKind;
2952       break;
2953     }
2954   }
2955   return AllocatorKindRes;
2956 }
2957 
2958 static bool checkPreviousOMPAllocateAttribute(
2959     Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD,
2960     OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) {
2961   if (!VD->hasAttr<OMPAllocateDeclAttr>())
2962     return false;
2963   const auto *A = VD->getAttr<OMPAllocateDeclAttr>();
2964   Expr *PrevAllocator = A->getAllocator();
2965   OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind =
2966       getAllocatorKind(S, Stack, PrevAllocator);
2967   bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind;
2968   if (AllocatorsMatch &&
2969       AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc &&
2970       Allocator && PrevAllocator) {
2971     const Expr *AE = Allocator->IgnoreParenImpCasts();
2972     const Expr *PAE = PrevAllocator->IgnoreParenImpCasts();
2973     llvm::FoldingSetNodeID AEId, PAEId;
2974     AE->Profile(AEId, S.Context, /*Canonical=*/true);
2975     PAE->Profile(PAEId, S.Context, /*Canonical=*/true);
2976     AllocatorsMatch = AEId == PAEId;
2977   }
2978   if (!AllocatorsMatch) {
2979     SmallString<256> AllocatorBuffer;
2980     llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer);
2981     if (Allocator)
2982       Allocator->printPretty(AllocatorStream, nullptr, S.getPrintingPolicy());
2983     SmallString<256> PrevAllocatorBuffer;
2984     llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer);
2985     if (PrevAllocator)
2986       PrevAllocator->printPretty(PrevAllocatorStream, nullptr,
2987                                  S.getPrintingPolicy());
2988 
2989     SourceLocation AllocatorLoc =
2990         Allocator ? Allocator->getExprLoc() : RefExpr->getExprLoc();
2991     SourceRange AllocatorRange =
2992         Allocator ? Allocator->getSourceRange() : RefExpr->getSourceRange();
2993     SourceLocation PrevAllocatorLoc =
2994         PrevAllocator ? PrevAllocator->getExprLoc() : A->getLocation();
2995     SourceRange PrevAllocatorRange =
2996         PrevAllocator ? PrevAllocator->getSourceRange() : A->getRange();
2997     S.Diag(AllocatorLoc, diag::warn_omp_used_different_allocator)
2998         << (Allocator ? 1 : 0) << AllocatorStream.str()
2999         << (PrevAllocator ? 1 : 0) << PrevAllocatorStream.str()
3000         << AllocatorRange;
3001     S.Diag(PrevAllocatorLoc, diag::note_omp_previous_allocator)
3002         << PrevAllocatorRange;
3003     return true;
3004   }
3005   return false;
3006 }
3007 
3008 static void
3009 applyOMPAllocateAttribute(Sema &S, VarDecl *VD,
3010                           OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
3011                           Expr *Allocator, SourceRange SR) {
3012   if (VD->hasAttr<OMPAllocateDeclAttr>())
3013     return;
3014   if (Allocator &&
3015       (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
3016        Allocator->isInstantiationDependent() ||
3017        Allocator->containsUnexpandedParameterPack()))
3018     return;
3019   auto *A = OMPAllocateDeclAttr::CreateImplicit(S.Context, AllocatorKind,
3020                                                 Allocator, SR);
3021   VD->addAttr(A);
3022   if (ASTMutationListener *ML = S.Context.getASTMutationListener())
3023     ML->DeclarationMarkedOpenMPAllocate(VD, A);
3024 }
3025 
3026 Sema::DeclGroupPtrTy Sema::ActOnOpenMPAllocateDirective(
3027     SourceLocation Loc, ArrayRef<Expr *> VarList,
3028     ArrayRef<OMPClause *> Clauses, DeclContext *Owner) {
3029   assert(Clauses.size() <= 1 && "Expected at most one clause.");
3030   Expr *Allocator = nullptr;
3031   if (Clauses.empty()) {
3032     // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions.
3033     // allocate directives that appear in a target region must specify an
3034     // allocator clause unless a requires directive with the dynamic_allocators
3035     // clause is present in the same compilation unit.
3036     if (LangOpts.OpenMPIsDevice &&
3037         !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
3038       targetDiag(Loc, diag::err_expected_allocator_clause);
3039   } else {
3040     Allocator = cast<OMPAllocatorClause>(Clauses.back())->getAllocator();
3041   }
3042   OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
3043       getAllocatorKind(*this, DSAStack, Allocator);
3044   SmallVector<Expr *, 8> Vars;
3045   for (Expr *RefExpr : VarList) {
3046     auto *DE = cast<DeclRefExpr>(RefExpr);
3047     auto *VD = cast<VarDecl>(DE->getDecl());
3048 
3049     // Check if this is a TLS variable or global register.
3050     if (VD->getTLSKind() != VarDecl::TLS_None ||
3051         VD->hasAttr<OMPThreadPrivateDeclAttr>() ||
3052         (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
3053          !VD->isLocalVarDecl()))
3054       continue;
3055 
3056     // If the used several times in the allocate directive, the same allocator
3057     // must be used.
3058     if (checkPreviousOMPAllocateAttribute(*this, DSAStack, RefExpr, VD,
3059                                           AllocatorKind, Allocator))
3060       continue;
3061 
3062     // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++
3063     // If a list item has a static storage type, the allocator expression in the
3064     // allocator clause must be a constant expression that evaluates to one of
3065     // the predefined memory allocator values.
3066     if (Allocator && VD->hasGlobalStorage()) {
3067       if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) {
3068         Diag(Allocator->getExprLoc(),
3069              diag::err_omp_expected_predefined_allocator)
3070             << Allocator->getSourceRange();
3071         bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3072                       VarDecl::DeclarationOnly;
3073         Diag(VD->getLocation(),
3074              IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3075             << VD;
3076         continue;
3077       }
3078     }
3079 
3080     Vars.push_back(RefExpr);
3081     applyOMPAllocateAttribute(*this, VD, AllocatorKind, Allocator,
3082                               DE->getSourceRange());
3083   }
3084   if (Vars.empty())
3085     return nullptr;
3086   if (!Owner)
3087     Owner = getCurLexicalContext();
3088   auto *D = OMPAllocateDecl::Create(Context, Owner, Loc, Vars, Clauses);
3089   D->setAccess(AS_public);
3090   Owner->addDecl(D);
3091   return DeclGroupPtrTy::make(DeclGroupRef(D));
3092 }
3093 
3094 Sema::DeclGroupPtrTy
3095 Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc,
3096                                    ArrayRef<OMPClause *> ClauseList) {
3097   OMPRequiresDecl *D = nullptr;
3098   if (!CurContext->isFileContext()) {
3099     Diag(Loc, diag::err_omp_invalid_scope) << "requires";
3100   } else {
3101     D = CheckOMPRequiresDecl(Loc, ClauseList);
3102     if (D) {
3103       CurContext->addDecl(D);
3104       DSAStack->addRequiresDecl(D);
3105     }
3106   }
3107   return DeclGroupPtrTy::make(DeclGroupRef(D));
3108 }
3109 
3110 OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc,
3111                                             ArrayRef<OMPClause *> ClauseList) {
3112   /// For target specific clauses, the requires directive cannot be
3113   /// specified after the handling of any of the target regions in the
3114   /// current compilation unit.
3115   ArrayRef<SourceLocation> TargetLocations =
3116       DSAStack->getEncounteredTargetLocs();
3117   SourceLocation AtomicLoc = DSAStack->getAtomicDirectiveLoc();
3118   if (!TargetLocations.empty() || !AtomicLoc.isInvalid()) {
3119     for (const OMPClause *CNew : ClauseList) {
3120       // Check if any of the requires clauses affect target regions.
3121       if (isa<OMPUnifiedSharedMemoryClause>(CNew) ||
3122           isa<OMPUnifiedAddressClause>(CNew) ||
3123           isa<OMPReverseOffloadClause>(CNew) ||
3124           isa<OMPDynamicAllocatorsClause>(CNew)) {
3125         Diag(Loc, diag::err_omp_directive_before_requires)
3126             << "target" << getOpenMPClauseName(CNew->getClauseKind());
3127         for (SourceLocation TargetLoc : TargetLocations) {
3128           Diag(TargetLoc, diag::note_omp_requires_encountered_directive)
3129               << "target";
3130         }
3131       } else if (!AtomicLoc.isInvalid() &&
3132                  isa<OMPAtomicDefaultMemOrderClause>(CNew)) {
3133         Diag(Loc, diag::err_omp_directive_before_requires)
3134             << "atomic" << getOpenMPClauseName(CNew->getClauseKind());
3135         Diag(AtomicLoc, diag::note_omp_requires_encountered_directive)
3136             << "atomic";
3137       }
3138     }
3139   }
3140 
3141   if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
3142     return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc,
3143                                    ClauseList);
3144   return nullptr;
3145 }
3146 
3147 static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
3148                               const ValueDecl *D,
3149                               const DSAStackTy::DSAVarData &DVar,
3150                               bool IsLoopIterVar) {
3151   if (DVar.RefExpr) {
3152     SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
3153         << getOpenMPClauseName(DVar.CKind);
3154     return;
3155   }
3156   enum {
3157     PDSA_StaticMemberShared,
3158     PDSA_StaticLocalVarShared,
3159     PDSA_LoopIterVarPrivate,
3160     PDSA_LoopIterVarLinear,
3161     PDSA_LoopIterVarLastprivate,
3162     PDSA_ConstVarShared,
3163     PDSA_GlobalVarShared,
3164     PDSA_TaskVarFirstprivate,
3165     PDSA_LocalVarPrivate,
3166     PDSA_Implicit
3167   } Reason = PDSA_Implicit;
3168   bool ReportHint = false;
3169   auto ReportLoc = D->getLocation();
3170   auto *VD = dyn_cast<VarDecl>(D);
3171   if (IsLoopIterVar) {
3172     if (DVar.CKind == OMPC_private)
3173       Reason = PDSA_LoopIterVarPrivate;
3174     else if (DVar.CKind == OMPC_lastprivate)
3175       Reason = PDSA_LoopIterVarLastprivate;
3176     else
3177       Reason = PDSA_LoopIterVarLinear;
3178   } else if (isOpenMPTaskingDirective(DVar.DKind) &&
3179              DVar.CKind == OMPC_firstprivate) {
3180     Reason = PDSA_TaskVarFirstprivate;
3181     ReportLoc = DVar.ImplicitDSALoc;
3182   } else if (VD && VD->isStaticLocal())
3183     Reason = PDSA_StaticLocalVarShared;
3184   else if (VD && VD->isStaticDataMember())
3185     Reason = PDSA_StaticMemberShared;
3186   else if (VD && VD->isFileVarDecl())
3187     Reason = PDSA_GlobalVarShared;
3188   else if (D->getType().isConstant(SemaRef.getASTContext()))
3189     Reason = PDSA_ConstVarShared;
3190   else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
3191     ReportHint = true;
3192     Reason = PDSA_LocalVarPrivate;
3193   }
3194   if (Reason != PDSA_Implicit) {
3195     SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
3196         << Reason << ReportHint
3197         << getOpenMPDirectiveName(Stack->getCurrentDirective());
3198   } else if (DVar.ImplicitDSALoc.isValid()) {
3199     SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
3200         << getOpenMPClauseName(DVar.CKind);
3201   }
3202 }
3203 
3204 static OpenMPMapClauseKind
3205 getMapClauseKindFromModifier(OpenMPDefaultmapClauseModifier M,
3206                              bool IsAggregateOrDeclareTarget) {
3207   OpenMPMapClauseKind Kind = OMPC_MAP_unknown;
3208   switch (M) {
3209   case OMPC_DEFAULTMAP_MODIFIER_alloc:
3210     Kind = OMPC_MAP_alloc;
3211     break;
3212   case OMPC_DEFAULTMAP_MODIFIER_to:
3213     Kind = OMPC_MAP_to;
3214     break;
3215   case OMPC_DEFAULTMAP_MODIFIER_from:
3216     Kind = OMPC_MAP_from;
3217     break;
3218   case OMPC_DEFAULTMAP_MODIFIER_tofrom:
3219     Kind = OMPC_MAP_tofrom;
3220     break;
3221   case OMPC_DEFAULTMAP_MODIFIER_firstprivate:
3222   case OMPC_DEFAULTMAP_MODIFIER_last:
3223     llvm_unreachable("Unexpected defaultmap implicit behavior");
3224   case OMPC_DEFAULTMAP_MODIFIER_none:
3225   case OMPC_DEFAULTMAP_MODIFIER_default:
3226   case OMPC_DEFAULTMAP_MODIFIER_unknown:
3227     // IsAggregateOrDeclareTarget could be true if:
3228     // 1. the implicit behavior for aggregate is tofrom
3229     // 2. it's a declare target link
3230     if (IsAggregateOrDeclareTarget) {
3231       Kind = OMPC_MAP_tofrom;
3232       break;
3233     }
3234     llvm_unreachable("Unexpected defaultmap implicit behavior");
3235   }
3236   assert(Kind != OMPC_MAP_unknown && "Expect map kind to be known");
3237   return Kind;
3238 }
3239 
3240 namespace {
3241 class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
3242   DSAStackTy *Stack;
3243   Sema &SemaRef;
3244   bool ErrorFound = false;
3245   bool TryCaptureCXXThisMembers = false;
3246   CapturedStmt *CS = nullptr;
3247   llvm::SmallVector<Expr *, 4> ImplicitFirstprivate;
3248   llvm::SmallVector<Expr *, 4> ImplicitMap[OMPC_MAP_delete];
3249   Sema::VarsWithInheritedDSAType VarsWithInheritedDSA;
3250   llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
3251 
3252   void VisitSubCaptures(OMPExecutableDirective *S) {
3253     // Check implicitly captured variables.
3254     if (!S->hasAssociatedStmt() || !S->getAssociatedStmt())
3255       return;
3256     visitSubCaptures(S->getInnermostCapturedStmt());
3257     // Try to capture inner this->member references to generate correct mappings
3258     // and diagnostics.
3259     if (TryCaptureCXXThisMembers ||
3260         (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
3261          llvm::any_of(S->getInnermostCapturedStmt()->captures(),
3262                       [](const CapturedStmt::Capture &C) {
3263                         return C.capturesThis();
3264                       }))) {
3265       bool SavedTryCaptureCXXThisMembers = TryCaptureCXXThisMembers;
3266       TryCaptureCXXThisMembers = true;
3267       Visit(S->getInnermostCapturedStmt()->getCapturedStmt());
3268       TryCaptureCXXThisMembers = SavedTryCaptureCXXThisMembers;
3269     }
3270     // In tasks firstprivates are not captured anymore, need to analyze them
3271     // explicitly.
3272     if (isOpenMPTaskingDirective(S->getDirectiveKind()) &&
3273         !isOpenMPTaskLoopDirective(S->getDirectiveKind())) {
3274       for (OMPClause *C : S->clauses())
3275         if (auto *FC = dyn_cast<OMPFirstprivateClause>(C)) {
3276           for (Expr *Ref : FC->varlists())
3277             Visit(Ref);
3278         }
3279     }
3280   }
3281 
3282 public:
3283   void VisitDeclRefExpr(DeclRefExpr *E) {
3284     if (TryCaptureCXXThisMembers || E->isTypeDependent() ||
3285         E->isValueDependent() || E->containsUnexpandedParameterPack() ||
3286         E->isInstantiationDependent())
3287       return;
3288     if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
3289       // Check the datasharing rules for the expressions in the clauses.
3290       if (!CS) {
3291         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
3292           if (!CED->hasAttr<OMPCaptureNoInitAttr>()) {
3293             Visit(CED->getInit());
3294             return;
3295           }
3296       } else if (VD->isImplicit() || isa<OMPCapturedExprDecl>(VD))
3297         // Do not analyze internal variables and do not enclose them into
3298         // implicit clauses.
3299         return;
3300       VD = VD->getCanonicalDecl();
3301       // Skip internally declared variables.
3302       if (VD->hasLocalStorage() && CS && !CS->capturesVariable(VD) &&
3303           !Stack->isImplicitTaskFirstprivate(VD))
3304         return;
3305       // Skip allocators in uses_allocators clauses.
3306       if (Stack->isUsesAllocatorsDecl(VD).hasValue())
3307         return;
3308 
3309       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
3310       // Check if the variable has explicit DSA set and stop analysis if it so.
3311       if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second)
3312         return;
3313 
3314       // Skip internally declared static variables.
3315       llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
3316           OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
3317       if (VD->hasGlobalStorage() && CS && !CS->capturesVariable(VD) &&
3318           (Stack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() ||
3319            !Res || *Res != OMPDeclareTargetDeclAttr::MT_Link) &&
3320           !Stack->isImplicitTaskFirstprivate(VD))
3321         return;
3322 
3323       SourceLocation ELoc = E->getExprLoc();
3324       OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
3325       // The default(none) clause requires that each variable that is referenced
3326       // in the construct, and does not have a predetermined data-sharing
3327       // attribute, must have its data-sharing attribute explicitly determined
3328       // by being listed in a data-sharing attribute clause.
3329       if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
3330           isImplicitOrExplicitTaskingRegion(DKind) &&
3331           VarsWithInheritedDSA.count(VD) == 0) {
3332         VarsWithInheritedDSA[VD] = E;
3333         return;
3334       }
3335 
3336       // OpenMP 5.0 [2.19.7.2, defaultmap clause, Description]
3337       // If implicit-behavior is none, each variable referenced in the
3338       // construct that does not have a predetermined data-sharing attribute
3339       // and does not appear in a to or link clause on a declare target
3340       // directive must be listed in a data-mapping attribute clause, a
3341       // data-haring attribute clause (including a data-sharing attribute
3342       // clause on a combined construct where target. is one of the
3343       // constituent constructs), or an is_device_ptr clause.
3344       OpenMPDefaultmapClauseKind ClauseKind =
3345           getVariableCategoryFromDecl(SemaRef.getLangOpts(), VD);
3346       if (SemaRef.getLangOpts().OpenMP >= 50) {
3347         bool IsModifierNone = Stack->getDefaultmapModifier(ClauseKind) ==
3348                               OMPC_DEFAULTMAP_MODIFIER_none;
3349         if (DVar.CKind == OMPC_unknown && IsModifierNone &&
3350             VarsWithInheritedDSA.count(VD) == 0 && !Res) {
3351           // Only check for data-mapping attribute and is_device_ptr here
3352           // since we have already make sure that the declaration does not
3353           // have a data-sharing attribute above
3354           if (!Stack->checkMappableExprComponentListsForDecl(
3355                   VD, /*CurrentRegionOnly=*/true,
3356                   [VD](OMPClauseMappableExprCommon::MappableExprComponentListRef
3357                            MapExprComponents,
3358                        OpenMPClauseKind) {
3359                     auto MI = MapExprComponents.rbegin();
3360                     auto ME = MapExprComponents.rend();
3361                     return MI != ME && MI->getAssociatedDeclaration() == VD;
3362                   })) {
3363             VarsWithInheritedDSA[VD] = E;
3364             return;
3365           }
3366         }
3367       }
3368 
3369       if (isOpenMPTargetExecutionDirective(DKind) &&
3370           !Stack->isLoopControlVariable(VD).first) {
3371         if (!Stack->checkMappableExprComponentListsForDecl(
3372                 VD, /*CurrentRegionOnly=*/true,
3373                 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
3374                        StackComponents,
3375                    OpenMPClauseKind) {
3376                   // Variable is used if it has been marked as an array, array
3377                   // section, array shaping or the variable iself.
3378                   return StackComponents.size() == 1 ||
3379                          std::all_of(
3380                              std::next(StackComponents.rbegin()),
3381                              StackComponents.rend(),
3382                              [](const OMPClauseMappableExprCommon::
3383                                     MappableComponent &MC) {
3384                                return MC.getAssociatedDeclaration() ==
3385                                           nullptr &&
3386                                       (isa<OMPArraySectionExpr>(
3387                                            MC.getAssociatedExpression()) ||
3388                                        isa<OMPArrayShapingExpr>(
3389                                            MC.getAssociatedExpression()) ||
3390                                        isa<ArraySubscriptExpr>(
3391                                            MC.getAssociatedExpression()));
3392                              });
3393                 })) {
3394           bool IsFirstprivate = false;
3395           // By default lambdas are captured as firstprivates.
3396           if (const auto *RD =
3397                   VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
3398             IsFirstprivate = RD->isLambda();
3399           IsFirstprivate =
3400               IsFirstprivate || (Stack->mustBeFirstprivate(ClauseKind) && !Res);
3401           if (IsFirstprivate) {
3402             ImplicitFirstprivate.emplace_back(E);
3403           } else {
3404             OpenMPDefaultmapClauseModifier M =
3405                 Stack->getDefaultmapModifier(ClauseKind);
3406             OpenMPMapClauseKind Kind = getMapClauseKindFromModifier(
3407                 M, ClauseKind == OMPC_DEFAULTMAP_aggregate || Res);
3408             ImplicitMap[Kind].emplace_back(E);
3409           }
3410           return;
3411         }
3412       }
3413 
3414       // OpenMP [2.9.3.6, Restrictions, p.2]
3415       //  A list item that appears in a reduction clause of the innermost
3416       //  enclosing worksharing or parallel construct may not be accessed in an
3417       //  explicit task.
3418       DVar = Stack->hasInnermostDSA(
3419           VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
3420           [](OpenMPDirectiveKind K) {
3421             return isOpenMPParallelDirective(K) ||
3422                    isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
3423           },
3424           /*FromParent=*/true);
3425       if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
3426         ErrorFound = true;
3427         SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
3428         reportOriginalDsa(SemaRef, Stack, VD, DVar);
3429         return;
3430       }
3431 
3432       // Define implicit data-sharing attributes for task.
3433       DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false);
3434       if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
3435           !Stack->isLoopControlVariable(VD).first) {
3436         ImplicitFirstprivate.push_back(E);
3437         return;
3438       }
3439 
3440       // Store implicitly used globals with declare target link for parent
3441       // target.
3442       if (!isOpenMPTargetExecutionDirective(DKind) && Res &&
3443           *Res == OMPDeclareTargetDeclAttr::MT_Link) {
3444         Stack->addToParentTargetRegionLinkGlobals(E);
3445         return;
3446       }
3447     }
3448   }
3449   void VisitMemberExpr(MemberExpr *E) {
3450     if (E->isTypeDependent() || E->isValueDependent() ||
3451         E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
3452       return;
3453     auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
3454     OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
3455     if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParenCasts())) {
3456       if (!FD)
3457         return;
3458       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false);
3459       // Check if the variable has explicit DSA set and stop analysis if it
3460       // so.
3461       if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second)
3462         return;
3463 
3464       if (isOpenMPTargetExecutionDirective(DKind) &&
3465           !Stack->isLoopControlVariable(FD).first &&
3466           !Stack->checkMappableExprComponentListsForDecl(
3467               FD, /*CurrentRegionOnly=*/true,
3468               [](OMPClauseMappableExprCommon::MappableExprComponentListRef
3469                      StackComponents,
3470                  OpenMPClauseKind) {
3471                 return isa<CXXThisExpr>(
3472                     cast<MemberExpr>(
3473                         StackComponents.back().getAssociatedExpression())
3474                         ->getBase()
3475                         ->IgnoreParens());
3476               })) {
3477         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
3478         //  A bit-field cannot appear in a map clause.
3479         //
3480         if (FD->isBitField())
3481           return;
3482 
3483         // Check to see if the member expression is referencing a class that
3484         // has already been explicitly mapped
3485         if (Stack->isClassPreviouslyMapped(TE->getType()))
3486           return;
3487 
3488         OpenMPDefaultmapClauseModifier Modifier =
3489             Stack->getDefaultmapModifier(OMPC_DEFAULTMAP_aggregate);
3490         OpenMPMapClauseKind Kind = getMapClauseKindFromModifier(
3491             Modifier, /*IsAggregateOrDeclareTarget*/ true);
3492         ImplicitMap[Kind].emplace_back(E);
3493         return;
3494       }
3495 
3496       SourceLocation ELoc = E->getExprLoc();
3497       // OpenMP [2.9.3.6, Restrictions, p.2]
3498       //  A list item that appears in a reduction clause of the innermost
3499       //  enclosing worksharing or parallel construct may not be accessed in
3500       //  an  explicit task.
3501       DVar = Stack->hasInnermostDSA(
3502           FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
3503           [](OpenMPDirectiveKind K) {
3504             return isOpenMPParallelDirective(K) ||
3505                    isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
3506           },
3507           /*FromParent=*/true);
3508       if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
3509         ErrorFound = true;
3510         SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
3511         reportOriginalDsa(SemaRef, Stack, FD, DVar);
3512         return;
3513       }
3514 
3515       // Define implicit data-sharing attributes for task.
3516       DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false);
3517       if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
3518           !Stack->isLoopControlVariable(FD).first) {
3519         // Check if there is a captured expression for the current field in the
3520         // region. Do not mark it as firstprivate unless there is no captured
3521         // expression.
3522         // TODO: try to make it firstprivate.
3523         if (DVar.CKind != OMPC_unknown)
3524           ImplicitFirstprivate.push_back(E);
3525       }
3526       return;
3527     }
3528     if (isOpenMPTargetExecutionDirective(DKind)) {
3529       OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
3530       if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
3531                                         /*NoDiagnose=*/true))
3532         return;
3533       const auto *VD = cast<ValueDecl>(
3534           CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
3535       if (!Stack->checkMappableExprComponentListsForDecl(
3536               VD, /*CurrentRegionOnly=*/true,
3537               [&CurComponents](
3538                   OMPClauseMappableExprCommon::MappableExprComponentListRef
3539                       StackComponents,
3540                   OpenMPClauseKind) {
3541                 auto CCI = CurComponents.rbegin();
3542                 auto CCE = CurComponents.rend();
3543                 for (const auto &SC : llvm::reverse(StackComponents)) {
3544                   // Do both expressions have the same kind?
3545                   if (CCI->getAssociatedExpression()->getStmtClass() !=
3546                       SC.getAssociatedExpression()->getStmtClass())
3547                     if (!((isa<OMPArraySectionExpr>(
3548                                SC.getAssociatedExpression()) ||
3549                            isa<OMPArrayShapingExpr>(
3550                                SC.getAssociatedExpression())) &&
3551                           isa<ArraySubscriptExpr>(
3552                               CCI->getAssociatedExpression())))
3553                       return false;
3554 
3555                   const Decl *CCD = CCI->getAssociatedDeclaration();
3556                   const Decl *SCD = SC.getAssociatedDeclaration();
3557                   CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
3558                   SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
3559                   if (SCD != CCD)
3560                     return false;
3561                   std::advance(CCI, 1);
3562                   if (CCI == CCE)
3563                     break;
3564                 }
3565                 return true;
3566               })) {
3567         Visit(E->getBase());
3568       }
3569     } else if (!TryCaptureCXXThisMembers) {
3570       Visit(E->getBase());
3571     }
3572   }
3573   void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
3574     for (OMPClause *C : S->clauses()) {
3575       // Skip analysis of arguments of implicitly defined firstprivate clause
3576       // for task|target directives.
3577       // Skip analysis of arguments of implicitly defined map clause for target
3578       // directives.
3579       if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
3580                  C->isImplicit())) {
3581         for (Stmt *CC : C->children()) {
3582           if (CC)
3583             Visit(CC);
3584         }
3585       }
3586     }
3587     // Check implicitly captured variables.
3588     VisitSubCaptures(S);
3589   }
3590   void VisitStmt(Stmt *S) {
3591     for (Stmt *C : S->children()) {
3592       if (C) {
3593         // Check implicitly captured variables in the task-based directives to
3594         // check if they must be firstprivatized.
3595         Visit(C);
3596       }
3597     }
3598   }
3599 
3600   void visitSubCaptures(CapturedStmt *S) {
3601     for (const CapturedStmt::Capture &Cap : S->captures()) {
3602       if (!Cap.capturesVariable() && !Cap.capturesVariableByCopy())
3603         continue;
3604       VarDecl *VD = Cap.getCapturedVar();
3605       // Do not try to map the variable if it or its sub-component was mapped
3606       // already.
3607       if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
3608           Stack->checkMappableExprComponentListsForDecl(
3609               VD, /*CurrentRegionOnly=*/true,
3610               [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
3611                  OpenMPClauseKind) { return true; }))
3612         continue;
3613       DeclRefExpr *DRE = buildDeclRefExpr(
3614           SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context),
3615           Cap.getLocation(), /*RefersToCapture=*/true);
3616       Visit(DRE);
3617     }
3618   }
3619   bool isErrorFound() const { return ErrorFound; }
3620   ArrayRef<Expr *> getImplicitFirstprivate() const {
3621     return ImplicitFirstprivate;
3622   }
3623   ArrayRef<Expr *> getImplicitMap(OpenMPDefaultmapClauseKind Kind) const {
3624     return ImplicitMap[Kind];
3625   }
3626   const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
3627     return VarsWithInheritedDSA;
3628   }
3629 
3630   DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
3631       : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {
3632     // Process declare target link variables for the target directives.
3633     if (isOpenMPTargetExecutionDirective(S->getCurrentDirective())) {
3634       for (DeclRefExpr *E : Stack->getLinkGlobals())
3635         Visit(E);
3636     }
3637   }
3638 };
3639 } // namespace
3640 
3641 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
3642   switch (DKind) {
3643   case OMPD_parallel:
3644   case OMPD_parallel_for:
3645   case OMPD_parallel_for_simd:
3646   case OMPD_parallel_sections:
3647   case OMPD_parallel_master:
3648   case OMPD_teams:
3649   case OMPD_teams_distribute:
3650   case OMPD_teams_distribute_simd: {
3651     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3652     QualType KmpInt32PtrTy =
3653         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3654     Sema::CapturedParamNameType Params[] = {
3655         std::make_pair(".global_tid.", KmpInt32PtrTy),
3656         std::make_pair(".bound_tid.", KmpInt32PtrTy),
3657         std::make_pair(StringRef(), QualType()) // __context with shared vars
3658     };
3659     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3660                              Params);
3661     break;
3662   }
3663   case OMPD_target_teams:
3664   case OMPD_target_parallel:
3665   case OMPD_target_parallel_for:
3666   case OMPD_target_parallel_for_simd:
3667   case OMPD_target_teams_distribute:
3668   case OMPD_target_teams_distribute_simd: {
3669     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3670     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3671     QualType KmpInt32PtrTy =
3672         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3673     QualType Args[] = {VoidPtrTy};
3674     FunctionProtoType::ExtProtoInfo EPI;
3675     EPI.Variadic = true;
3676     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3677     Sema::CapturedParamNameType Params[] = {
3678         std::make_pair(".global_tid.", KmpInt32Ty),
3679         std::make_pair(".part_id.", KmpInt32PtrTy),
3680         std::make_pair(".privates.", VoidPtrTy),
3681         std::make_pair(
3682             ".copy_fn.",
3683             Context.getPointerType(CopyFnType).withConst().withRestrict()),
3684         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3685         std::make_pair(StringRef(), QualType()) // __context with shared vars
3686     };
3687     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3688                              Params, /*OpenMPCaptureLevel=*/0);
3689     // Mark this captured region as inlined, because we don't use outlined
3690     // function directly.
3691     getCurCapturedRegion()->TheCapturedDecl->addAttr(
3692         AlwaysInlineAttr::CreateImplicit(
3693             Context, {}, AttributeCommonInfo::AS_Keyword,
3694             AlwaysInlineAttr::Keyword_forceinline));
3695     Sema::CapturedParamNameType ParamsTarget[] = {
3696         std::make_pair(StringRef(), QualType()) // __context with shared vars
3697     };
3698     // Start a captured region for 'target' with no implicit parameters.
3699     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3700                              ParamsTarget, /*OpenMPCaptureLevel=*/1);
3701     Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
3702         std::make_pair(".global_tid.", KmpInt32PtrTy),
3703         std::make_pair(".bound_tid.", KmpInt32PtrTy),
3704         std::make_pair(StringRef(), QualType()) // __context with shared vars
3705     };
3706     // Start a captured region for 'teams' or 'parallel'.  Both regions have
3707     // the same implicit parameters.
3708     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3709                              ParamsTeamsOrParallel, /*OpenMPCaptureLevel=*/2);
3710     break;
3711   }
3712   case OMPD_target:
3713   case OMPD_target_simd: {
3714     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3715     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3716     QualType KmpInt32PtrTy =
3717         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3718     QualType Args[] = {VoidPtrTy};
3719     FunctionProtoType::ExtProtoInfo EPI;
3720     EPI.Variadic = true;
3721     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3722     Sema::CapturedParamNameType Params[] = {
3723         std::make_pair(".global_tid.", KmpInt32Ty),
3724         std::make_pair(".part_id.", KmpInt32PtrTy),
3725         std::make_pair(".privates.", VoidPtrTy),
3726         std::make_pair(
3727             ".copy_fn.",
3728             Context.getPointerType(CopyFnType).withConst().withRestrict()),
3729         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3730         std::make_pair(StringRef(), QualType()) // __context with shared vars
3731     };
3732     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3733                              Params, /*OpenMPCaptureLevel=*/0);
3734     // Mark this captured region as inlined, because we don't use outlined
3735     // function directly.
3736     getCurCapturedRegion()->TheCapturedDecl->addAttr(
3737         AlwaysInlineAttr::CreateImplicit(
3738             Context, {}, AttributeCommonInfo::AS_Keyword,
3739             AlwaysInlineAttr::Keyword_forceinline));
3740     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3741                              std::make_pair(StringRef(), QualType()),
3742                              /*OpenMPCaptureLevel=*/1);
3743     break;
3744   }
3745   case OMPD_simd:
3746   case OMPD_for:
3747   case OMPD_for_simd:
3748   case OMPD_sections:
3749   case OMPD_section:
3750   case OMPD_single:
3751   case OMPD_master:
3752   case OMPD_critical:
3753   case OMPD_taskgroup:
3754   case OMPD_distribute:
3755   case OMPD_distribute_simd:
3756   case OMPD_ordered:
3757   case OMPD_atomic:
3758   case OMPD_target_data: {
3759     Sema::CapturedParamNameType Params[] = {
3760         std::make_pair(StringRef(), QualType()) // __context with shared vars
3761     };
3762     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3763                              Params);
3764     break;
3765   }
3766   case OMPD_task: {
3767     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3768     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3769     QualType KmpInt32PtrTy =
3770         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3771     QualType Args[] = {VoidPtrTy};
3772     FunctionProtoType::ExtProtoInfo EPI;
3773     EPI.Variadic = true;
3774     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3775     Sema::CapturedParamNameType Params[] = {
3776         std::make_pair(".global_tid.", KmpInt32Ty),
3777         std::make_pair(".part_id.", KmpInt32PtrTy),
3778         std::make_pair(".privates.", VoidPtrTy),
3779         std::make_pair(
3780             ".copy_fn.",
3781             Context.getPointerType(CopyFnType).withConst().withRestrict()),
3782         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3783         std::make_pair(StringRef(), QualType()) // __context with shared vars
3784     };
3785     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3786                              Params);
3787     // Mark this captured region as inlined, because we don't use outlined
3788     // function directly.
3789     getCurCapturedRegion()->TheCapturedDecl->addAttr(
3790         AlwaysInlineAttr::CreateImplicit(
3791             Context, {}, AttributeCommonInfo::AS_Keyword,
3792             AlwaysInlineAttr::Keyword_forceinline));
3793     break;
3794   }
3795   case OMPD_taskloop:
3796   case OMPD_taskloop_simd:
3797   case OMPD_master_taskloop:
3798   case OMPD_master_taskloop_simd: {
3799     QualType KmpInt32Ty =
3800         Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
3801             .withConst();
3802     QualType KmpUInt64Ty =
3803         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
3804             .withConst();
3805     QualType KmpInt64Ty =
3806         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
3807             .withConst();
3808     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3809     QualType KmpInt32PtrTy =
3810         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3811     QualType Args[] = {VoidPtrTy};
3812     FunctionProtoType::ExtProtoInfo EPI;
3813     EPI.Variadic = true;
3814     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3815     Sema::CapturedParamNameType Params[] = {
3816         std::make_pair(".global_tid.", KmpInt32Ty),
3817         std::make_pair(".part_id.", KmpInt32PtrTy),
3818         std::make_pair(".privates.", VoidPtrTy),
3819         std::make_pair(
3820             ".copy_fn.",
3821             Context.getPointerType(CopyFnType).withConst().withRestrict()),
3822         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3823         std::make_pair(".lb.", KmpUInt64Ty),
3824         std::make_pair(".ub.", KmpUInt64Ty),
3825         std::make_pair(".st.", KmpInt64Ty),
3826         std::make_pair(".liter.", KmpInt32Ty),
3827         std::make_pair(".reductions.", VoidPtrTy),
3828         std::make_pair(StringRef(), QualType()) // __context with shared vars
3829     };
3830     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3831                              Params);
3832     // Mark this captured region as inlined, because we don't use outlined
3833     // function directly.
3834     getCurCapturedRegion()->TheCapturedDecl->addAttr(
3835         AlwaysInlineAttr::CreateImplicit(
3836             Context, {}, AttributeCommonInfo::AS_Keyword,
3837             AlwaysInlineAttr::Keyword_forceinline));
3838     break;
3839   }
3840   case OMPD_parallel_master_taskloop:
3841   case OMPD_parallel_master_taskloop_simd: {
3842     QualType KmpInt32Ty =
3843         Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
3844             .withConst();
3845     QualType KmpUInt64Ty =
3846         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
3847             .withConst();
3848     QualType KmpInt64Ty =
3849         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
3850             .withConst();
3851     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3852     QualType KmpInt32PtrTy =
3853         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3854     Sema::CapturedParamNameType ParamsParallel[] = {
3855         std::make_pair(".global_tid.", KmpInt32PtrTy),
3856         std::make_pair(".bound_tid.", KmpInt32PtrTy),
3857         std::make_pair(StringRef(), QualType()) // __context with shared vars
3858     };
3859     // Start a captured region for 'parallel'.
3860     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3861                              ParamsParallel, /*OpenMPCaptureLevel=*/0);
3862     QualType Args[] = {VoidPtrTy};
3863     FunctionProtoType::ExtProtoInfo EPI;
3864     EPI.Variadic = true;
3865     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3866     Sema::CapturedParamNameType Params[] = {
3867         std::make_pair(".global_tid.", KmpInt32Ty),
3868         std::make_pair(".part_id.", KmpInt32PtrTy),
3869         std::make_pair(".privates.", VoidPtrTy),
3870         std::make_pair(
3871             ".copy_fn.",
3872             Context.getPointerType(CopyFnType).withConst().withRestrict()),
3873         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3874         std::make_pair(".lb.", KmpUInt64Ty),
3875         std::make_pair(".ub.", KmpUInt64Ty),
3876         std::make_pair(".st.", KmpInt64Ty),
3877         std::make_pair(".liter.", KmpInt32Ty),
3878         std::make_pair(".reductions.", VoidPtrTy),
3879         std::make_pair(StringRef(), QualType()) // __context with shared vars
3880     };
3881     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3882                              Params, /*OpenMPCaptureLevel=*/1);
3883     // Mark this captured region as inlined, because we don't use outlined
3884     // function directly.
3885     getCurCapturedRegion()->TheCapturedDecl->addAttr(
3886         AlwaysInlineAttr::CreateImplicit(
3887             Context, {}, AttributeCommonInfo::AS_Keyword,
3888             AlwaysInlineAttr::Keyword_forceinline));
3889     break;
3890   }
3891   case OMPD_distribute_parallel_for_simd:
3892   case OMPD_distribute_parallel_for: {
3893     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3894     QualType KmpInt32PtrTy =
3895         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3896     Sema::CapturedParamNameType Params[] = {
3897         std::make_pair(".global_tid.", KmpInt32PtrTy),
3898         std::make_pair(".bound_tid.", KmpInt32PtrTy),
3899         std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
3900         std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
3901         std::make_pair(StringRef(), QualType()) // __context with shared vars
3902     };
3903     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3904                              Params);
3905     break;
3906   }
3907   case OMPD_target_teams_distribute_parallel_for:
3908   case OMPD_target_teams_distribute_parallel_for_simd: {
3909     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3910     QualType KmpInt32PtrTy =
3911         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3912     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3913 
3914     QualType Args[] = {VoidPtrTy};
3915     FunctionProtoType::ExtProtoInfo EPI;
3916     EPI.Variadic = true;
3917     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3918     Sema::CapturedParamNameType Params[] = {
3919         std::make_pair(".global_tid.", KmpInt32Ty),
3920         std::make_pair(".part_id.", KmpInt32PtrTy),
3921         std::make_pair(".privates.", VoidPtrTy),
3922         std::make_pair(
3923             ".copy_fn.",
3924             Context.getPointerType(CopyFnType).withConst().withRestrict()),
3925         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3926         std::make_pair(StringRef(), QualType()) // __context with shared vars
3927     };
3928     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3929                              Params, /*OpenMPCaptureLevel=*/0);
3930     // Mark this captured region as inlined, because we don't use outlined
3931     // function directly.
3932     getCurCapturedRegion()->TheCapturedDecl->addAttr(
3933         AlwaysInlineAttr::CreateImplicit(
3934             Context, {}, AttributeCommonInfo::AS_Keyword,
3935             AlwaysInlineAttr::Keyword_forceinline));
3936     Sema::CapturedParamNameType ParamsTarget[] = {
3937         std::make_pair(StringRef(), QualType()) // __context with shared vars
3938     };
3939     // Start a captured region for 'target' with no implicit parameters.
3940     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3941                              ParamsTarget, /*OpenMPCaptureLevel=*/1);
3942 
3943     Sema::CapturedParamNameType ParamsTeams[] = {
3944         std::make_pair(".global_tid.", KmpInt32PtrTy),
3945         std::make_pair(".bound_tid.", KmpInt32PtrTy),
3946         std::make_pair(StringRef(), QualType()) // __context with shared vars
3947     };
3948     // Start a captured region for 'target' with no implicit parameters.
3949     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3950                              ParamsTeams, /*OpenMPCaptureLevel=*/2);
3951 
3952     Sema::CapturedParamNameType ParamsParallel[] = {
3953         std::make_pair(".global_tid.", KmpInt32PtrTy),
3954         std::make_pair(".bound_tid.", KmpInt32PtrTy),
3955         std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
3956         std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
3957         std::make_pair(StringRef(), QualType()) // __context with shared vars
3958     };
3959     // Start a captured region for 'teams' or 'parallel'.  Both regions have
3960     // the same implicit parameters.
3961     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3962                              ParamsParallel, /*OpenMPCaptureLevel=*/3);
3963     break;
3964   }
3965 
3966   case OMPD_teams_distribute_parallel_for:
3967   case OMPD_teams_distribute_parallel_for_simd: {
3968     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3969     QualType KmpInt32PtrTy =
3970         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3971 
3972     Sema::CapturedParamNameType ParamsTeams[] = {
3973         std::make_pair(".global_tid.", KmpInt32PtrTy),
3974         std::make_pair(".bound_tid.", KmpInt32PtrTy),
3975         std::make_pair(StringRef(), QualType()) // __context with shared vars
3976     };
3977     // Start a captured region for 'target' with no implicit parameters.
3978     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3979                              ParamsTeams, /*OpenMPCaptureLevel=*/0);
3980 
3981     Sema::CapturedParamNameType ParamsParallel[] = {
3982         std::make_pair(".global_tid.", KmpInt32PtrTy),
3983         std::make_pair(".bound_tid.", KmpInt32PtrTy),
3984         std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
3985         std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
3986         std::make_pair(StringRef(), QualType()) // __context with shared vars
3987     };
3988     // Start a captured region for 'teams' or 'parallel'.  Both regions have
3989     // the same implicit parameters.
3990     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3991                              ParamsParallel, /*OpenMPCaptureLevel=*/1);
3992     break;
3993   }
3994   case OMPD_target_update:
3995   case OMPD_target_enter_data:
3996   case OMPD_target_exit_data: {
3997     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3998     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3999     QualType KmpInt32PtrTy =
4000         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4001     QualType Args[] = {VoidPtrTy};
4002     FunctionProtoType::ExtProtoInfo EPI;
4003     EPI.Variadic = true;
4004     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
4005     Sema::CapturedParamNameType Params[] = {
4006         std::make_pair(".global_tid.", KmpInt32Ty),
4007         std::make_pair(".part_id.", KmpInt32PtrTy),
4008         std::make_pair(".privates.", VoidPtrTy),
4009         std::make_pair(
4010             ".copy_fn.",
4011             Context.getPointerType(CopyFnType).withConst().withRestrict()),
4012         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
4013         std::make_pair(StringRef(), QualType()) // __context with shared vars
4014     };
4015     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4016                              Params);
4017     // Mark this captured region as inlined, because we don't use outlined
4018     // function directly.
4019     getCurCapturedRegion()->TheCapturedDecl->addAttr(
4020         AlwaysInlineAttr::CreateImplicit(
4021             Context, {}, AttributeCommonInfo::AS_Keyword,
4022             AlwaysInlineAttr::Keyword_forceinline));
4023     break;
4024   }
4025   case OMPD_threadprivate:
4026   case OMPD_allocate:
4027   case OMPD_taskyield:
4028   case OMPD_barrier:
4029   case OMPD_taskwait:
4030   case OMPD_cancellation_point:
4031   case OMPD_cancel:
4032   case OMPD_flush:
4033   case OMPD_depobj:
4034   case OMPD_scan:
4035   case OMPD_declare_reduction:
4036   case OMPD_declare_mapper:
4037   case OMPD_declare_simd:
4038   case OMPD_declare_target:
4039   case OMPD_end_declare_target:
4040   case OMPD_requires:
4041   case OMPD_declare_variant:
4042   case OMPD_begin_declare_variant:
4043   case OMPD_end_declare_variant:
4044     llvm_unreachable("OpenMP Directive is not allowed");
4045   case OMPD_unknown:
4046     llvm_unreachable("Unknown OpenMP directive");
4047   }
4048 }
4049 
4050 int Sema::getNumberOfConstructScopes(unsigned Level) const {
4051   return getOpenMPCaptureLevels(DSAStack->getDirective(Level));
4052 }
4053 
4054 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
4055   SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
4056   getOpenMPCaptureRegions(CaptureRegions, DKind);
4057   return CaptureRegions.size();
4058 }
4059 
4060 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
4061                                              Expr *CaptureExpr, bool WithInit,
4062                                              bool AsExpression) {
4063   assert(CaptureExpr);
4064   ASTContext &C = S.getASTContext();
4065   Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
4066   QualType Ty = Init->getType();
4067   if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
4068     if (S.getLangOpts().CPlusPlus) {
4069       Ty = C.getLValueReferenceType(Ty);
4070     } else {
4071       Ty = C.getPointerType(Ty);
4072       ExprResult Res =
4073           S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
4074       if (!Res.isUsable())
4075         return nullptr;
4076       Init = Res.get();
4077     }
4078     WithInit = true;
4079   }
4080   auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
4081                                           CaptureExpr->getBeginLoc());
4082   if (!WithInit)
4083     CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C));
4084   S.CurContext->addHiddenDecl(CED);
4085   S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
4086   return CED;
4087 }
4088 
4089 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
4090                                  bool WithInit) {
4091   OMPCapturedExprDecl *CD;
4092   if (VarDecl *VD = S.isOpenMPCapturedDecl(D))
4093     CD = cast<OMPCapturedExprDecl>(VD);
4094   else
4095     CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
4096                           /*AsExpression=*/false);
4097   return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
4098                           CaptureExpr->getExprLoc());
4099 }
4100 
4101 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
4102   CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
4103   if (!Ref) {
4104     OMPCapturedExprDecl *CD = buildCaptureDecl(
4105         S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
4106         /*WithInit=*/true, /*AsExpression=*/true);
4107     Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
4108                            CaptureExpr->getExprLoc());
4109   }
4110   ExprResult Res = Ref;
4111   if (!S.getLangOpts().CPlusPlus &&
4112       CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
4113       Ref->getType()->isPointerType()) {
4114     Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
4115     if (!Res.isUsable())
4116       return ExprError();
4117   }
4118   return S.DefaultLvalueConversion(Res.get());
4119 }
4120 
4121 namespace {
4122 // OpenMP directives parsed in this section are represented as a
4123 // CapturedStatement with an associated statement.  If a syntax error
4124 // is detected during the parsing of the associated statement, the
4125 // compiler must abort processing and close the CapturedStatement.
4126 //
4127 // Combined directives such as 'target parallel' have more than one
4128 // nested CapturedStatements.  This RAII ensures that we unwind out
4129 // of all the nested CapturedStatements when an error is found.
4130 class CaptureRegionUnwinderRAII {
4131 private:
4132   Sema &S;
4133   bool &ErrorFound;
4134   OpenMPDirectiveKind DKind = OMPD_unknown;
4135 
4136 public:
4137   CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
4138                             OpenMPDirectiveKind DKind)
4139       : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
4140   ~CaptureRegionUnwinderRAII() {
4141     if (ErrorFound) {
4142       int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
4143       while (--ThisCaptureLevel >= 0)
4144         S.ActOnCapturedRegionError();
4145     }
4146   }
4147 };
4148 } // namespace
4149 
4150 void Sema::tryCaptureOpenMPLambdas(ValueDecl *V) {
4151   // Capture variables captured by reference in lambdas for target-based
4152   // directives.
4153   if (!CurContext->isDependentContext() &&
4154       (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) ||
4155        isOpenMPTargetDataManagementDirective(
4156            DSAStack->getCurrentDirective()))) {
4157     QualType Type = V->getType();
4158     if (const auto *RD = Type.getCanonicalType()
4159                              .getNonReferenceType()
4160                              ->getAsCXXRecordDecl()) {
4161       bool SavedForceCaptureByReferenceInTargetExecutable =
4162           DSAStack->isForceCaptureByReferenceInTargetExecutable();
4163       DSAStack->setForceCaptureByReferenceInTargetExecutable(
4164           /*V=*/true);
4165       if (RD->isLambda()) {
4166         llvm::DenseMap<const VarDecl *, FieldDecl *> Captures;
4167         FieldDecl *ThisCapture;
4168         RD->getCaptureFields(Captures, ThisCapture);
4169         for (const LambdaCapture &LC : RD->captures()) {
4170           if (LC.getCaptureKind() == LCK_ByRef) {
4171             VarDecl *VD = LC.getCapturedVar();
4172             DeclContext *VDC = VD->getDeclContext();
4173             if (!VDC->Encloses(CurContext))
4174               continue;
4175             MarkVariableReferenced(LC.getLocation(), VD);
4176           } else if (LC.getCaptureKind() == LCK_This) {
4177             QualType ThisTy = getCurrentThisType();
4178             if (!ThisTy.isNull() &&
4179                 Context.typesAreCompatible(ThisTy, ThisCapture->getType()))
4180               CheckCXXThisCapture(LC.getLocation());
4181           }
4182         }
4183       }
4184       DSAStack->setForceCaptureByReferenceInTargetExecutable(
4185           SavedForceCaptureByReferenceInTargetExecutable);
4186     }
4187   }
4188 }
4189 
4190 static bool checkOrderedOrderSpecified(Sema &S,
4191                                        const ArrayRef<OMPClause *> Clauses) {
4192   const OMPOrderedClause *Ordered = nullptr;
4193   const OMPOrderClause *Order = nullptr;
4194 
4195   for (const OMPClause *Clause : Clauses) {
4196     if (Clause->getClauseKind() == OMPC_ordered)
4197       Ordered = cast<OMPOrderedClause>(Clause);
4198     else if (Clause->getClauseKind() == OMPC_order) {
4199       Order = cast<OMPOrderClause>(Clause);
4200       if (Order->getKind() != OMPC_ORDER_concurrent)
4201         Order = nullptr;
4202     }
4203     if (Ordered && Order)
4204       break;
4205   }
4206 
4207   if (Ordered && Order) {
4208     S.Diag(Order->getKindKwLoc(),
4209            diag::err_omp_simple_clause_incompatible_with_ordered)
4210         << getOpenMPClauseName(OMPC_order)
4211         << getOpenMPSimpleClauseTypeName(OMPC_order, OMPC_ORDER_concurrent)
4212         << SourceRange(Order->getBeginLoc(), Order->getEndLoc());
4213     S.Diag(Ordered->getBeginLoc(), diag::note_omp_ordered_param)
4214         << 0 << SourceRange(Ordered->getBeginLoc(), Ordered->getEndLoc());
4215     return true;
4216   }
4217   return false;
4218 }
4219 
4220 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
4221                                       ArrayRef<OMPClause *> Clauses) {
4222   bool ErrorFound = false;
4223   CaptureRegionUnwinderRAII CaptureRegionUnwinder(
4224       *this, ErrorFound, DSAStack->getCurrentDirective());
4225   if (!S.isUsable()) {
4226     ErrorFound = true;
4227     return StmtError();
4228   }
4229 
4230   SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
4231   getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
4232   OMPOrderedClause *OC = nullptr;
4233   OMPScheduleClause *SC = nullptr;
4234   SmallVector<const OMPLinearClause *, 4> LCs;
4235   SmallVector<const OMPClauseWithPreInit *, 4> PICs;
4236   // This is required for proper codegen.
4237   for (OMPClause *Clause : Clauses) {
4238     if (!LangOpts.OpenMPSimd &&
4239         isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
4240         Clause->getClauseKind() == OMPC_in_reduction) {
4241       // Capture taskgroup task_reduction descriptors inside the tasking regions
4242       // with the corresponding in_reduction items.
4243       auto *IRC = cast<OMPInReductionClause>(Clause);
4244       for (Expr *E : IRC->taskgroup_descriptors())
4245         if (E)
4246           MarkDeclarationsReferencedInExpr(E);
4247     }
4248     if (isOpenMPPrivate(Clause->getClauseKind()) ||
4249         Clause->getClauseKind() == OMPC_copyprivate ||
4250         (getLangOpts().OpenMPUseTLS &&
4251          getASTContext().getTargetInfo().isTLSSupported() &&
4252          Clause->getClauseKind() == OMPC_copyin)) {
4253       DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
4254       // Mark all variables in private list clauses as used in inner region.
4255       for (Stmt *VarRef : Clause->children()) {
4256         if (auto *E = cast_or_null<Expr>(VarRef)) {
4257           MarkDeclarationsReferencedInExpr(E);
4258         }
4259       }
4260       DSAStack->setForceVarCapturing(/*V=*/false);
4261     } else if (CaptureRegions.size() > 1 ||
4262                CaptureRegions.back() != OMPD_unknown) {
4263       if (auto *C = OMPClauseWithPreInit::get(Clause))
4264         PICs.push_back(C);
4265       if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
4266         if (Expr *E = C->getPostUpdateExpr())
4267           MarkDeclarationsReferencedInExpr(E);
4268       }
4269     }
4270     if (Clause->getClauseKind() == OMPC_schedule)
4271       SC = cast<OMPScheduleClause>(Clause);
4272     else if (Clause->getClauseKind() == OMPC_ordered)
4273       OC = cast<OMPOrderedClause>(Clause);
4274     else if (Clause->getClauseKind() == OMPC_linear)
4275       LCs.push_back(cast<OMPLinearClause>(Clause));
4276   }
4277   // Capture allocator expressions if used.
4278   for (Expr *E : DSAStack->getInnerAllocators())
4279     MarkDeclarationsReferencedInExpr(E);
4280   // OpenMP, 2.7.1 Loop Construct, Restrictions
4281   // The nonmonotonic modifier cannot be specified if an ordered clause is
4282   // specified.
4283   if (SC &&
4284       (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
4285        SC->getSecondScheduleModifier() ==
4286            OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
4287       OC) {
4288     Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
4289              ? SC->getFirstScheduleModifierLoc()
4290              : SC->getSecondScheduleModifierLoc(),
4291          diag::err_omp_simple_clause_incompatible_with_ordered)
4292         << getOpenMPClauseName(OMPC_schedule)
4293         << getOpenMPSimpleClauseTypeName(OMPC_schedule,
4294                                          OMPC_SCHEDULE_MODIFIER_nonmonotonic)
4295         << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
4296     ErrorFound = true;
4297   }
4298   // OpenMP 5.0, 2.9.2 Worksharing-Loop Construct, Restrictions.
4299   // If an order(concurrent) clause is present, an ordered clause may not appear
4300   // on the same directive.
4301   if (checkOrderedOrderSpecified(*this, Clauses))
4302     ErrorFound = true;
4303   if (!LCs.empty() && OC && OC->getNumForLoops()) {
4304     for (const OMPLinearClause *C : LCs) {
4305       Diag(C->getBeginLoc(), diag::err_omp_linear_ordered)
4306           << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
4307     }
4308     ErrorFound = true;
4309   }
4310   if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
4311       isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
4312       OC->getNumForLoops()) {
4313     Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd)
4314         << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
4315     ErrorFound = true;
4316   }
4317   if (ErrorFound) {
4318     return StmtError();
4319   }
4320   StmtResult SR = S;
4321   unsigned CompletedRegions = 0;
4322   for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
4323     // Mark all variables in private list clauses as used in inner region.
4324     // Required for proper codegen of combined directives.
4325     // TODO: add processing for other clauses.
4326     if (ThisCaptureRegion != OMPD_unknown) {
4327       for (const clang::OMPClauseWithPreInit *C : PICs) {
4328         OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
4329         // Find the particular capture region for the clause if the
4330         // directive is a combined one with multiple capture regions.
4331         // If the directive is not a combined one, the capture region
4332         // associated with the clause is OMPD_unknown and is generated
4333         // only once.
4334         if (CaptureRegion == ThisCaptureRegion ||
4335             CaptureRegion == OMPD_unknown) {
4336           if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
4337             for (Decl *D : DS->decls())
4338               MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
4339           }
4340         }
4341       }
4342     }
4343     if (ThisCaptureRegion == OMPD_target) {
4344       // Capture allocator traits in the target region. They are used implicitly
4345       // and, thus, are not captured by default.
4346       for (OMPClause *C : Clauses) {
4347         if (const auto *UAC = dyn_cast<OMPUsesAllocatorsClause>(C)) {
4348           for (unsigned I = 0, End = UAC->getNumberOfAllocators(); I < End;
4349                ++I) {
4350             OMPUsesAllocatorsClause::Data D = UAC->getAllocatorData(I);
4351             if (Expr *E = D.AllocatorTraits)
4352               MarkDeclarationsReferencedInExpr(E);
4353           }
4354           continue;
4355         }
4356       }
4357     }
4358     if (++CompletedRegions == CaptureRegions.size())
4359       DSAStack->setBodyComplete();
4360     SR = ActOnCapturedRegionEnd(SR.get());
4361   }
4362   return SR;
4363 }
4364 
4365 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
4366                               OpenMPDirectiveKind CancelRegion,
4367                               SourceLocation StartLoc) {
4368   // CancelRegion is only needed for cancel and cancellation_point.
4369   if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
4370     return false;
4371 
4372   if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
4373       CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
4374     return false;
4375 
4376   SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4377       << getOpenMPDirectiveName(CancelRegion);
4378   return true;
4379 }
4380 
4381 static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
4382                                   OpenMPDirectiveKind CurrentRegion,
4383                                   const DeclarationNameInfo &CurrentName,
4384                                   OpenMPDirectiveKind CancelRegion,
4385                                   SourceLocation StartLoc) {
4386   if (Stack->getCurScope()) {
4387     OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
4388     OpenMPDirectiveKind OffendingRegion = ParentRegion;
4389     bool NestingProhibited = false;
4390     bool CloseNesting = true;
4391     bool OrphanSeen = false;
4392     enum {
4393       NoRecommend,
4394       ShouldBeInParallelRegion,
4395       ShouldBeInOrderedRegion,
4396       ShouldBeInTargetRegion,
4397       ShouldBeInTeamsRegion,
4398       ShouldBeInLoopSimdRegion,
4399     } Recommend = NoRecommend;
4400     if (isOpenMPSimdDirective(ParentRegion) &&
4401         ((SemaRef.LangOpts.OpenMP <= 45 && CurrentRegion != OMPD_ordered) ||
4402          (SemaRef.LangOpts.OpenMP >= 50 && CurrentRegion != OMPD_ordered &&
4403           CurrentRegion != OMPD_simd && CurrentRegion != OMPD_atomic &&
4404           CurrentRegion != OMPD_scan))) {
4405       // OpenMP [2.16, Nesting of Regions]
4406       // OpenMP constructs may not be nested inside a simd region.
4407       // OpenMP [2.8.1,simd Construct, Restrictions]
4408       // An ordered construct with the simd clause is the only OpenMP
4409       // construct that can appear in the simd region.
4410       // Allowing a SIMD construct nested in another SIMD construct is an
4411       // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
4412       // message.
4413       // OpenMP 5.0 [2.9.3.1, simd Construct, Restrictions]
4414       // The only OpenMP constructs that can be encountered during execution of
4415       // a simd region are the atomic construct, the loop construct, the simd
4416       // construct and the ordered construct with the simd clause.
4417       SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
4418                                  ? diag::err_omp_prohibited_region_simd
4419                                  : diag::warn_omp_nesting_simd)
4420           << (SemaRef.LangOpts.OpenMP >= 50 ? 1 : 0);
4421       return CurrentRegion != OMPD_simd;
4422     }
4423     if (ParentRegion == OMPD_atomic) {
4424       // OpenMP [2.16, Nesting of Regions]
4425       // OpenMP constructs may not be nested inside an atomic region.
4426       SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
4427       return true;
4428     }
4429     if (CurrentRegion == OMPD_section) {
4430       // OpenMP [2.7.2, sections Construct, Restrictions]
4431       // Orphaned section directives are prohibited. That is, the section
4432       // directives must appear within the sections construct and must not be
4433       // encountered elsewhere in the sections region.
4434       if (ParentRegion != OMPD_sections &&
4435           ParentRegion != OMPD_parallel_sections) {
4436         SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
4437             << (ParentRegion != OMPD_unknown)
4438             << getOpenMPDirectiveName(ParentRegion);
4439         return true;
4440       }
4441       return false;
4442     }
4443     // Allow some constructs (except teams and cancellation constructs) to be
4444     // orphaned (they could be used in functions, called from OpenMP regions
4445     // with the required preconditions).
4446     if (ParentRegion == OMPD_unknown &&
4447         !isOpenMPNestingTeamsDirective(CurrentRegion) &&
4448         CurrentRegion != OMPD_cancellation_point &&
4449         CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_scan)
4450       return false;
4451     if (CurrentRegion == OMPD_cancellation_point ||
4452         CurrentRegion == OMPD_cancel) {
4453       // OpenMP [2.16, Nesting of Regions]
4454       // A cancellation point construct for which construct-type-clause is
4455       // taskgroup must be nested inside a task construct. A cancellation
4456       // point construct for which construct-type-clause is not taskgroup must
4457       // be closely nested inside an OpenMP construct that matches the type
4458       // specified in construct-type-clause.
4459       // A cancel construct for which construct-type-clause is taskgroup must be
4460       // nested inside a task construct. A cancel construct for which
4461       // construct-type-clause is not taskgroup must be closely nested inside an
4462       // OpenMP construct that matches the type specified in
4463       // construct-type-clause.
4464       NestingProhibited =
4465           !((CancelRegion == OMPD_parallel &&
4466              (ParentRegion == OMPD_parallel ||
4467               ParentRegion == OMPD_target_parallel)) ||
4468             (CancelRegion == OMPD_for &&
4469              (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
4470               ParentRegion == OMPD_target_parallel_for ||
4471               ParentRegion == OMPD_distribute_parallel_for ||
4472               ParentRegion == OMPD_teams_distribute_parallel_for ||
4473               ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
4474             (CancelRegion == OMPD_taskgroup &&
4475              (ParentRegion == OMPD_task ||
4476               (SemaRef.getLangOpts().OpenMP >= 50 &&
4477                (ParentRegion == OMPD_taskloop ||
4478                 ParentRegion == OMPD_master_taskloop ||
4479                 ParentRegion == OMPD_parallel_master_taskloop)))) ||
4480             (CancelRegion == OMPD_sections &&
4481              (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
4482               ParentRegion == OMPD_parallel_sections)));
4483       OrphanSeen = ParentRegion == OMPD_unknown;
4484     } else if (CurrentRegion == OMPD_master) {
4485       // OpenMP [2.16, Nesting of Regions]
4486       // A master region may not be closely nested inside a worksharing,
4487       // atomic, or explicit task region.
4488       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
4489                           isOpenMPTaskingDirective(ParentRegion);
4490     } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
4491       // OpenMP [2.16, Nesting of Regions]
4492       // A critical region may not be nested (closely or otherwise) inside a
4493       // critical region with the same name. Note that this restriction is not
4494       // sufficient to prevent deadlock.
4495       SourceLocation PreviousCriticalLoc;
4496       bool DeadLock = Stack->hasDirective(
4497           [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
4498                                               const DeclarationNameInfo &DNI,
4499                                               SourceLocation Loc) {
4500             if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
4501               PreviousCriticalLoc = Loc;
4502               return true;
4503             }
4504             return false;
4505           },
4506           false /* skip top directive */);
4507       if (DeadLock) {
4508         SemaRef.Diag(StartLoc,
4509                      diag::err_omp_prohibited_region_critical_same_name)
4510             << CurrentName.getName();
4511         if (PreviousCriticalLoc.isValid())
4512           SemaRef.Diag(PreviousCriticalLoc,
4513                        diag::note_omp_previous_critical_region);
4514         return true;
4515       }
4516     } else if (CurrentRegion == OMPD_barrier) {
4517       // OpenMP [2.16, Nesting of Regions]
4518       // A barrier region may not be closely nested inside a worksharing,
4519       // explicit task, critical, ordered, atomic, or master region.
4520       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
4521                           isOpenMPTaskingDirective(ParentRegion) ||
4522                           ParentRegion == OMPD_master ||
4523                           ParentRegion == OMPD_parallel_master ||
4524                           ParentRegion == OMPD_critical ||
4525                           ParentRegion == OMPD_ordered;
4526     } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
4527                !isOpenMPParallelDirective(CurrentRegion) &&
4528                !isOpenMPTeamsDirective(CurrentRegion)) {
4529       // OpenMP [2.16, Nesting of Regions]
4530       // A worksharing region may not be closely nested inside a worksharing,
4531       // explicit task, critical, ordered, atomic, or master region.
4532       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
4533                           isOpenMPTaskingDirective(ParentRegion) ||
4534                           ParentRegion == OMPD_master ||
4535                           ParentRegion == OMPD_parallel_master ||
4536                           ParentRegion == OMPD_critical ||
4537                           ParentRegion == OMPD_ordered;
4538       Recommend = ShouldBeInParallelRegion;
4539     } else if (CurrentRegion == OMPD_ordered) {
4540       // OpenMP [2.16, Nesting of Regions]
4541       // An ordered region may not be closely nested inside a critical,
4542       // atomic, or explicit task region.
4543       // An ordered region must be closely nested inside a loop region (or
4544       // parallel loop region) with an ordered clause.
4545       // OpenMP [2.8.1,simd Construct, Restrictions]
4546       // An ordered construct with the simd clause is the only OpenMP construct
4547       // that can appear in the simd region.
4548       NestingProhibited = ParentRegion == OMPD_critical ||
4549                           isOpenMPTaskingDirective(ParentRegion) ||
4550                           !(isOpenMPSimdDirective(ParentRegion) ||
4551                             Stack->isParentOrderedRegion());
4552       Recommend = ShouldBeInOrderedRegion;
4553     } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
4554       // OpenMP [2.16, Nesting of Regions]
4555       // If specified, a teams construct must be contained within a target
4556       // construct.
4557       NestingProhibited =
4558           (SemaRef.LangOpts.OpenMP <= 45 && ParentRegion != OMPD_target) ||
4559           (SemaRef.LangOpts.OpenMP >= 50 && ParentRegion != OMPD_unknown &&
4560            ParentRegion != OMPD_target);
4561       OrphanSeen = ParentRegion == OMPD_unknown;
4562       Recommend = ShouldBeInTargetRegion;
4563     } else if (CurrentRegion == OMPD_scan) {
4564       // OpenMP [2.16, Nesting of Regions]
4565       // If specified, a teams construct must be contained within a target
4566       // construct.
4567       NestingProhibited =
4568           SemaRef.LangOpts.OpenMP < 50 ||
4569           (ParentRegion != OMPD_simd && ParentRegion != OMPD_for &&
4570            ParentRegion != OMPD_for_simd && ParentRegion != OMPD_parallel_for &&
4571            ParentRegion != OMPD_parallel_for_simd);
4572       OrphanSeen = ParentRegion == OMPD_unknown;
4573       Recommend = ShouldBeInLoopSimdRegion;
4574     }
4575     if (!NestingProhibited &&
4576         !isOpenMPTargetExecutionDirective(CurrentRegion) &&
4577         !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
4578         (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
4579       // OpenMP [2.16, Nesting of Regions]
4580       // distribute, parallel, parallel sections, parallel workshare, and the
4581       // parallel loop and parallel loop SIMD constructs are the only OpenMP
4582       // constructs that can be closely nested in the teams region.
4583       NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
4584                           !isOpenMPDistributeDirective(CurrentRegion);
4585       Recommend = ShouldBeInParallelRegion;
4586     }
4587     if (!NestingProhibited &&
4588         isOpenMPNestingDistributeDirective(CurrentRegion)) {
4589       // OpenMP 4.5 [2.17 Nesting of Regions]
4590       // The region associated with the distribute construct must be strictly
4591       // nested inside a teams region
4592       NestingProhibited =
4593           (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
4594       Recommend = ShouldBeInTeamsRegion;
4595     }
4596     if (!NestingProhibited &&
4597         (isOpenMPTargetExecutionDirective(CurrentRegion) ||
4598          isOpenMPTargetDataManagementDirective(CurrentRegion))) {
4599       // OpenMP 4.5 [2.17 Nesting of Regions]
4600       // If a target, target update, target data, target enter data, or
4601       // target exit data construct is encountered during execution of a
4602       // target region, the behavior is unspecified.
4603       NestingProhibited = Stack->hasDirective(
4604           [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
4605                              SourceLocation) {
4606             if (isOpenMPTargetExecutionDirective(K)) {
4607               OffendingRegion = K;
4608               return true;
4609             }
4610             return false;
4611           },
4612           false /* don't skip top directive */);
4613       CloseNesting = false;
4614     }
4615     if (NestingProhibited) {
4616       if (OrphanSeen) {
4617         SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
4618             << getOpenMPDirectiveName(CurrentRegion) << Recommend;
4619       } else {
4620         SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
4621             << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
4622             << Recommend << getOpenMPDirectiveName(CurrentRegion);
4623       }
4624       return true;
4625     }
4626   }
4627   return false;
4628 }
4629 
4630 struct Kind2Unsigned {
4631   using argument_type = OpenMPDirectiveKind;
4632   unsigned operator()(argument_type DK) { return unsigned(DK); }
4633 };
4634 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
4635                            ArrayRef<OMPClause *> Clauses,
4636                            ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
4637   bool ErrorFound = false;
4638   unsigned NamedModifiersNumber = 0;
4639   llvm::IndexedMap<const OMPIfClause *, Kind2Unsigned> FoundNameModifiers;
4640   FoundNameModifiers.resize(unsigned(OMPD_unknown) + 1);
4641   SmallVector<SourceLocation, 4> NameModifierLoc;
4642   for (const OMPClause *C : Clauses) {
4643     if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
4644       // At most one if clause without a directive-name-modifier can appear on
4645       // the directive.
4646       OpenMPDirectiveKind CurNM = IC->getNameModifier();
4647       if (FoundNameModifiers[CurNM]) {
4648         S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
4649             << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
4650             << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
4651         ErrorFound = true;
4652       } else if (CurNM != OMPD_unknown) {
4653         NameModifierLoc.push_back(IC->getNameModifierLoc());
4654         ++NamedModifiersNumber;
4655       }
4656       FoundNameModifiers[CurNM] = IC;
4657       if (CurNM == OMPD_unknown)
4658         continue;
4659       // Check if the specified name modifier is allowed for the current
4660       // directive.
4661       // At most one if clause with the particular directive-name-modifier can
4662       // appear on the directive.
4663       bool MatchFound = false;
4664       for (auto NM : AllowedNameModifiers) {
4665         if (CurNM == NM) {
4666           MatchFound = true;
4667           break;
4668         }
4669       }
4670       if (!MatchFound) {
4671         S.Diag(IC->getNameModifierLoc(),
4672                diag::err_omp_wrong_if_directive_name_modifier)
4673             << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
4674         ErrorFound = true;
4675       }
4676     }
4677   }
4678   // If any if clause on the directive includes a directive-name-modifier then
4679   // all if clauses on the directive must include a directive-name-modifier.
4680   if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
4681     if (NamedModifiersNumber == AllowedNameModifiers.size()) {
4682       S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
4683              diag::err_omp_no_more_if_clause);
4684     } else {
4685       std::string Values;
4686       std::string Sep(", ");
4687       unsigned AllowedCnt = 0;
4688       unsigned TotalAllowedNum =
4689           AllowedNameModifiers.size() - NamedModifiersNumber;
4690       for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
4691            ++Cnt) {
4692         OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
4693         if (!FoundNameModifiers[NM]) {
4694           Values += "'";
4695           Values += getOpenMPDirectiveName(NM);
4696           Values += "'";
4697           if (AllowedCnt + 2 == TotalAllowedNum)
4698             Values += " or ";
4699           else if (AllowedCnt + 1 != TotalAllowedNum)
4700             Values += Sep;
4701           ++AllowedCnt;
4702         }
4703       }
4704       S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
4705              diag::err_omp_unnamed_if_clause)
4706           << (TotalAllowedNum > 1) << Values;
4707     }
4708     for (SourceLocation Loc : NameModifierLoc) {
4709       S.Diag(Loc, diag::note_omp_previous_named_if_clause);
4710     }
4711     ErrorFound = true;
4712   }
4713   return ErrorFound;
4714 }
4715 
4716 static std::pair<ValueDecl *, bool> getPrivateItem(Sema &S, Expr *&RefExpr,
4717                                                    SourceLocation &ELoc,
4718                                                    SourceRange &ERange,
4719                                                    bool AllowArraySection) {
4720   if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
4721       RefExpr->containsUnexpandedParameterPack())
4722     return std::make_pair(nullptr, true);
4723 
4724   // OpenMP [3.1, C/C++]
4725   //  A list item is a variable name.
4726   // OpenMP  [2.9.3.3, Restrictions, p.1]
4727   //  A variable that is part of another variable (as an array or
4728   //  structure element) cannot appear in a private clause.
4729   RefExpr = RefExpr->IgnoreParens();
4730   enum {
4731     NoArrayExpr = -1,
4732     ArraySubscript = 0,
4733     OMPArraySection = 1
4734   } IsArrayExpr = NoArrayExpr;
4735   if (AllowArraySection) {
4736     if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
4737       Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
4738       while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
4739         Base = TempASE->getBase()->IgnoreParenImpCasts();
4740       RefExpr = Base;
4741       IsArrayExpr = ArraySubscript;
4742     } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
4743       Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
4744       while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
4745         Base = TempOASE->getBase()->IgnoreParenImpCasts();
4746       while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
4747         Base = TempASE->getBase()->IgnoreParenImpCasts();
4748       RefExpr = Base;
4749       IsArrayExpr = OMPArraySection;
4750     }
4751   }
4752   ELoc = RefExpr->getExprLoc();
4753   ERange = RefExpr->getSourceRange();
4754   RefExpr = RefExpr->IgnoreParenImpCasts();
4755   auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
4756   auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
4757   if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
4758       (S.getCurrentThisType().isNull() || !ME ||
4759        !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
4760        !isa<FieldDecl>(ME->getMemberDecl()))) {
4761     if (IsArrayExpr != NoArrayExpr) {
4762       S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
4763                                                          << ERange;
4764     } else {
4765       S.Diag(ELoc,
4766              AllowArraySection
4767                  ? diag::err_omp_expected_var_name_member_expr_or_array_item
4768                  : diag::err_omp_expected_var_name_member_expr)
4769           << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
4770     }
4771     return std::make_pair(nullptr, false);
4772   }
4773   return std::make_pair(
4774       getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
4775 }
4776 
4777 namespace {
4778 /// Checks if the allocator is used in uses_allocators clause to be allowed in
4779 /// target regions.
4780 class AllocatorChecker final : public ConstStmtVisitor<AllocatorChecker, bool> {
4781   DSAStackTy *S = nullptr;
4782 
4783 public:
4784   bool VisitDeclRefExpr(const DeclRefExpr *E) {
4785     return S->isUsesAllocatorsDecl(E->getDecl())
4786                .getValueOr(
4787                    DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) ==
4788            DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait;
4789   }
4790   bool VisitStmt(const Stmt *S) {
4791     for (const Stmt *Child : S->children()) {
4792       if (Child && Visit(Child))
4793         return true;
4794     }
4795     return false;
4796   }
4797   explicit AllocatorChecker(DSAStackTy *S) : S(S) {}
4798 };
4799 } // namespace
4800 
4801 static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
4802                                  ArrayRef<OMPClause *> Clauses) {
4803   assert(!S.CurContext->isDependentContext() &&
4804          "Expected non-dependent context.");
4805   auto AllocateRange =
4806       llvm::make_filter_range(Clauses, OMPAllocateClause::classof);
4807   llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>>
4808       DeclToCopy;
4809   auto PrivateRange = llvm::make_filter_range(Clauses, [](const OMPClause *C) {
4810     return isOpenMPPrivate(C->getClauseKind());
4811   });
4812   for (OMPClause *Cl : PrivateRange) {
4813     MutableArrayRef<Expr *>::iterator I, It, Et;
4814     if (Cl->getClauseKind() == OMPC_private) {
4815       auto *PC = cast<OMPPrivateClause>(Cl);
4816       I = PC->private_copies().begin();
4817       It = PC->varlist_begin();
4818       Et = PC->varlist_end();
4819     } else if (Cl->getClauseKind() == OMPC_firstprivate) {
4820       auto *PC = cast<OMPFirstprivateClause>(Cl);
4821       I = PC->private_copies().begin();
4822       It = PC->varlist_begin();
4823       Et = PC->varlist_end();
4824     } else if (Cl->getClauseKind() == OMPC_lastprivate) {
4825       auto *PC = cast<OMPLastprivateClause>(Cl);
4826       I = PC->private_copies().begin();
4827       It = PC->varlist_begin();
4828       Et = PC->varlist_end();
4829     } else if (Cl->getClauseKind() == OMPC_linear) {
4830       auto *PC = cast<OMPLinearClause>(Cl);
4831       I = PC->privates().begin();
4832       It = PC->varlist_begin();
4833       Et = PC->varlist_end();
4834     } else if (Cl->getClauseKind() == OMPC_reduction) {
4835       auto *PC = cast<OMPReductionClause>(Cl);
4836       I = PC->privates().begin();
4837       It = PC->varlist_begin();
4838       Et = PC->varlist_end();
4839     } else if (Cl->getClauseKind() == OMPC_task_reduction) {
4840       auto *PC = cast<OMPTaskReductionClause>(Cl);
4841       I = PC->privates().begin();
4842       It = PC->varlist_begin();
4843       Et = PC->varlist_end();
4844     } else if (Cl->getClauseKind() == OMPC_in_reduction) {
4845       auto *PC = cast<OMPInReductionClause>(Cl);
4846       I = PC->privates().begin();
4847       It = PC->varlist_begin();
4848       Et = PC->varlist_end();
4849     } else {
4850       llvm_unreachable("Expected private clause.");
4851     }
4852     for (Expr *E : llvm::make_range(It, Et)) {
4853       if (!*I) {
4854         ++I;
4855         continue;
4856       }
4857       SourceLocation ELoc;
4858       SourceRange ERange;
4859       Expr *SimpleRefExpr = E;
4860       auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
4861                                 /*AllowArraySection=*/true);
4862       DeclToCopy.try_emplace(Res.first,
4863                              cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()));
4864       ++I;
4865     }
4866   }
4867   for (OMPClause *C : AllocateRange) {
4868     auto *AC = cast<OMPAllocateClause>(C);
4869     if (S.getLangOpts().OpenMP >= 50 &&
4870         !Stack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>() &&
4871         isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
4872         AC->getAllocator()) {
4873       Expr *Allocator = AC->getAllocator();
4874       // OpenMP, 2.12.5 target Construct
4875       // Memory allocators that do not appear in a uses_allocators clause cannot
4876       // appear as an allocator in an allocate clause or be used in the target
4877       // region unless a requires directive with the dynamic_allocators clause
4878       // is present in the same compilation unit.
4879       AllocatorChecker Checker(Stack);
4880       if (Checker.Visit(Allocator))
4881         S.Diag(Allocator->getExprLoc(),
4882                diag::err_omp_allocator_not_in_uses_allocators)
4883             << Allocator->getSourceRange();
4884     }
4885     OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
4886         getAllocatorKind(S, Stack, AC->getAllocator());
4887     // OpenMP, 2.11.4 allocate Clause, Restrictions.
4888     // For task, taskloop or target directives, allocation requests to memory
4889     // allocators with the trait access set to thread result in unspecified
4890     // behavior.
4891     if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc &&
4892         (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
4893          isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()))) {
4894       S.Diag(AC->getAllocator()->getExprLoc(),
4895              diag::warn_omp_allocate_thread_on_task_target_directive)
4896           << getOpenMPDirectiveName(Stack->getCurrentDirective());
4897     }
4898     for (Expr *E : AC->varlists()) {
4899       SourceLocation ELoc;
4900       SourceRange ERange;
4901       Expr *SimpleRefExpr = E;
4902       auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange);
4903       ValueDecl *VD = Res.first;
4904       DSAStackTy::DSAVarData Data = Stack->getTopDSA(VD, /*FromParent=*/false);
4905       if (!isOpenMPPrivate(Data.CKind)) {
4906         S.Diag(E->getExprLoc(),
4907                diag::err_omp_expected_private_copy_for_allocate);
4908         continue;
4909       }
4910       VarDecl *PrivateVD = DeclToCopy[VD];
4911       if (checkPreviousOMPAllocateAttribute(S, Stack, E, PrivateVD,
4912                                             AllocatorKind, AC->getAllocator()))
4913         continue;
4914       applyOMPAllocateAttribute(S, PrivateVD, AllocatorKind, AC->getAllocator(),
4915                                 E->getSourceRange());
4916     }
4917   }
4918 }
4919 
4920 StmtResult Sema::ActOnOpenMPExecutableDirective(
4921     OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
4922     OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
4923     Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
4924   StmtResult Res = StmtError();
4925   // First check CancelRegion which is then used in checkNestingOfRegions.
4926   if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
4927       checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
4928                             StartLoc))
4929     return StmtError();
4930 
4931   llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
4932   VarsWithInheritedDSAType VarsWithInheritedDSA;
4933   bool ErrorFound = false;
4934   ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
4935   if (AStmt && !CurContext->isDependentContext()) {
4936     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4937 
4938     // Check default data sharing attributes for referenced variables.
4939     DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
4940     int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
4941     Stmt *S = AStmt;
4942     while (--ThisCaptureLevel >= 0)
4943       S = cast<CapturedStmt>(S)->getCapturedStmt();
4944     DSAChecker.Visit(S);
4945     if (!isOpenMPTargetDataManagementDirective(Kind) &&
4946         !isOpenMPTaskingDirective(Kind)) {
4947       // Visit subcaptures to generate implicit clauses for captured vars.
4948       auto *CS = cast<CapturedStmt>(AStmt);
4949       SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
4950       getOpenMPCaptureRegions(CaptureRegions, Kind);
4951       // Ignore outer tasking regions for target directives.
4952       if (CaptureRegions.size() > 1 && CaptureRegions.front() == OMPD_task)
4953         CS = cast<CapturedStmt>(CS->getCapturedStmt());
4954       DSAChecker.visitSubCaptures(CS);
4955     }
4956     if (DSAChecker.isErrorFound())
4957       return StmtError();
4958     // Generate list of implicitly defined firstprivate variables.
4959     VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
4960 
4961     SmallVector<Expr *, 4> ImplicitFirstprivates(
4962         DSAChecker.getImplicitFirstprivate().begin(),
4963         DSAChecker.getImplicitFirstprivate().end());
4964     SmallVector<Expr *, 4> ImplicitMaps[OMPC_MAP_delete];
4965     for (unsigned I = 0; I < OMPC_MAP_delete; ++I) {
4966       ArrayRef<Expr *> ImplicitMap =
4967           DSAChecker.getImplicitMap(static_cast<OpenMPDefaultmapClauseKind>(I));
4968       ImplicitMaps[I].append(ImplicitMap.begin(), ImplicitMap.end());
4969     }
4970     // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
4971     for (OMPClause *C : Clauses) {
4972       if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
4973         for (Expr *E : IRC->taskgroup_descriptors())
4974           if (E)
4975             ImplicitFirstprivates.emplace_back(E);
4976       }
4977       // OpenMP 5.0, 2.10.1 task Construct
4978       // [detach clause]... The event-handle will be considered as if it was
4979       // specified on a firstprivate clause.
4980       if (auto *DC = dyn_cast<OMPDetachClause>(C))
4981         ImplicitFirstprivates.push_back(DC->getEventHandler());
4982     }
4983     if (!ImplicitFirstprivates.empty()) {
4984       if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
4985               ImplicitFirstprivates, SourceLocation(), SourceLocation(),
4986               SourceLocation())) {
4987         ClausesWithImplicit.push_back(Implicit);
4988         ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
4989                      ImplicitFirstprivates.size();
4990       } else {
4991         ErrorFound = true;
4992       }
4993     }
4994     int ClauseKindCnt = -1;
4995     for (ArrayRef<Expr *> ImplicitMap : ImplicitMaps) {
4996       ++ClauseKindCnt;
4997       if (ImplicitMap.empty())
4998         continue;
4999       CXXScopeSpec MapperIdScopeSpec;
5000       DeclarationNameInfo MapperId;
5001       auto Kind = static_cast<OpenMPMapClauseKind>(ClauseKindCnt);
5002       if (OMPClause *Implicit = ActOnOpenMPMapClause(
5003               llvm::None, llvm::None, MapperIdScopeSpec, MapperId, Kind,
5004               /*IsMapTypeImplicit=*/true, SourceLocation(), SourceLocation(),
5005               ImplicitMap, OMPVarListLocTy())) {
5006         ClausesWithImplicit.emplace_back(Implicit);
5007         ErrorFound |=
5008             cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMap.size();
5009       } else {
5010         ErrorFound = true;
5011       }
5012     }
5013   }
5014 
5015   llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
5016   switch (Kind) {
5017   case OMPD_parallel:
5018     Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
5019                                        EndLoc);
5020     AllowedNameModifiers.push_back(OMPD_parallel);
5021     break;
5022   case OMPD_simd:
5023     Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
5024                                    VarsWithInheritedDSA);
5025     if (LangOpts.OpenMP >= 50)
5026       AllowedNameModifiers.push_back(OMPD_simd);
5027     break;
5028   case OMPD_for:
5029     Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
5030                                   VarsWithInheritedDSA);
5031     break;
5032   case OMPD_for_simd:
5033     Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
5034                                       EndLoc, VarsWithInheritedDSA);
5035     if (LangOpts.OpenMP >= 50)
5036       AllowedNameModifiers.push_back(OMPD_simd);
5037     break;
5038   case OMPD_sections:
5039     Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
5040                                        EndLoc);
5041     break;
5042   case OMPD_section:
5043     assert(ClausesWithImplicit.empty() &&
5044            "No clauses are allowed for 'omp section' directive");
5045     Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
5046     break;
5047   case OMPD_single:
5048     Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
5049                                      EndLoc);
5050     break;
5051   case OMPD_master:
5052     assert(ClausesWithImplicit.empty() &&
5053            "No clauses are allowed for 'omp master' directive");
5054     Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
5055     break;
5056   case OMPD_critical:
5057     Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
5058                                        StartLoc, EndLoc);
5059     break;
5060   case OMPD_parallel_for:
5061     Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
5062                                           EndLoc, VarsWithInheritedDSA);
5063     AllowedNameModifiers.push_back(OMPD_parallel);
5064     break;
5065   case OMPD_parallel_for_simd:
5066     Res = ActOnOpenMPParallelForSimdDirective(
5067         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
5068     AllowedNameModifiers.push_back(OMPD_parallel);
5069     if (LangOpts.OpenMP >= 50)
5070       AllowedNameModifiers.push_back(OMPD_simd);
5071     break;
5072   case OMPD_parallel_master:
5073     Res = ActOnOpenMPParallelMasterDirective(ClausesWithImplicit, AStmt,
5074                                                StartLoc, EndLoc);
5075     AllowedNameModifiers.push_back(OMPD_parallel);
5076     break;
5077   case OMPD_parallel_sections:
5078     Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
5079                                                StartLoc, EndLoc);
5080     AllowedNameModifiers.push_back(OMPD_parallel);
5081     break;
5082   case OMPD_task:
5083     Res =
5084         ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
5085     AllowedNameModifiers.push_back(OMPD_task);
5086     break;
5087   case OMPD_taskyield:
5088     assert(ClausesWithImplicit.empty() &&
5089            "No clauses are allowed for 'omp taskyield' directive");
5090     assert(AStmt == nullptr &&
5091            "No associated statement allowed for 'omp taskyield' directive");
5092     Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
5093     break;
5094   case OMPD_barrier:
5095     assert(ClausesWithImplicit.empty() &&
5096            "No clauses are allowed for 'omp barrier' directive");
5097     assert(AStmt == nullptr &&
5098            "No associated statement allowed for 'omp barrier' directive");
5099     Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
5100     break;
5101   case OMPD_taskwait:
5102     assert(ClausesWithImplicit.empty() &&
5103            "No clauses are allowed for 'omp taskwait' directive");
5104     assert(AStmt == nullptr &&
5105            "No associated statement allowed for 'omp taskwait' directive");
5106     Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
5107     break;
5108   case OMPD_taskgroup:
5109     Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
5110                                         EndLoc);
5111     break;
5112   case OMPD_flush:
5113     assert(AStmt == nullptr &&
5114            "No associated statement allowed for 'omp flush' directive");
5115     Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
5116     break;
5117   case OMPD_depobj:
5118     assert(AStmt == nullptr &&
5119            "No associated statement allowed for 'omp depobj' directive");
5120     Res = ActOnOpenMPDepobjDirective(ClausesWithImplicit, StartLoc, EndLoc);
5121     break;
5122   case OMPD_scan:
5123     assert(AStmt == nullptr &&
5124            "No associated statement allowed for 'omp scan' directive");
5125     Res = ActOnOpenMPScanDirective(ClausesWithImplicit, StartLoc, EndLoc);
5126     break;
5127   case OMPD_ordered:
5128     Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
5129                                       EndLoc);
5130     break;
5131   case OMPD_atomic:
5132     Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
5133                                      EndLoc);
5134     break;
5135   case OMPD_teams:
5136     Res =
5137         ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
5138     break;
5139   case OMPD_target:
5140     Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
5141                                      EndLoc);
5142     AllowedNameModifiers.push_back(OMPD_target);
5143     break;
5144   case OMPD_target_parallel:
5145     Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
5146                                              StartLoc, EndLoc);
5147     AllowedNameModifiers.push_back(OMPD_target);
5148     AllowedNameModifiers.push_back(OMPD_parallel);
5149     break;
5150   case OMPD_target_parallel_for:
5151     Res = ActOnOpenMPTargetParallelForDirective(
5152         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
5153     AllowedNameModifiers.push_back(OMPD_target);
5154     AllowedNameModifiers.push_back(OMPD_parallel);
5155     break;
5156   case OMPD_cancellation_point:
5157     assert(ClausesWithImplicit.empty() &&
5158            "No clauses are allowed for 'omp cancellation point' directive");
5159     assert(AStmt == nullptr && "No associated statement allowed for 'omp "
5160                                "cancellation point' directive");
5161     Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
5162     break;
5163   case OMPD_cancel:
5164     assert(AStmt == nullptr &&
5165            "No associated statement allowed for 'omp cancel' directive");
5166     Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
5167                                      CancelRegion);
5168     AllowedNameModifiers.push_back(OMPD_cancel);
5169     break;
5170   case OMPD_target_data:
5171     Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
5172                                          EndLoc);
5173     AllowedNameModifiers.push_back(OMPD_target_data);
5174     break;
5175   case OMPD_target_enter_data:
5176     Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
5177                                               EndLoc, AStmt);
5178     AllowedNameModifiers.push_back(OMPD_target_enter_data);
5179     break;
5180   case OMPD_target_exit_data:
5181     Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
5182                                              EndLoc, AStmt);
5183     AllowedNameModifiers.push_back(OMPD_target_exit_data);
5184     break;
5185   case OMPD_taskloop:
5186     Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
5187                                        EndLoc, VarsWithInheritedDSA);
5188     AllowedNameModifiers.push_back(OMPD_taskloop);
5189     break;
5190   case OMPD_taskloop_simd:
5191     Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
5192                                            EndLoc, VarsWithInheritedDSA);
5193     AllowedNameModifiers.push_back(OMPD_taskloop);
5194     if (LangOpts.OpenMP >= 50)
5195       AllowedNameModifiers.push_back(OMPD_simd);
5196     break;
5197   case OMPD_master_taskloop:
5198     Res = ActOnOpenMPMasterTaskLoopDirective(
5199         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
5200     AllowedNameModifiers.push_back(OMPD_taskloop);
5201     break;
5202   case OMPD_master_taskloop_simd:
5203     Res = ActOnOpenMPMasterTaskLoopSimdDirective(
5204         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
5205     AllowedNameModifiers.push_back(OMPD_taskloop);
5206     if (LangOpts.OpenMP >= 50)
5207       AllowedNameModifiers.push_back(OMPD_simd);
5208     break;
5209   case OMPD_parallel_master_taskloop:
5210     Res = ActOnOpenMPParallelMasterTaskLoopDirective(
5211         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
5212     AllowedNameModifiers.push_back(OMPD_taskloop);
5213     AllowedNameModifiers.push_back(OMPD_parallel);
5214     break;
5215   case OMPD_parallel_master_taskloop_simd:
5216     Res = ActOnOpenMPParallelMasterTaskLoopSimdDirective(
5217         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
5218     AllowedNameModifiers.push_back(OMPD_taskloop);
5219     AllowedNameModifiers.push_back(OMPD_parallel);
5220     if (LangOpts.OpenMP >= 50)
5221       AllowedNameModifiers.push_back(OMPD_simd);
5222     break;
5223   case OMPD_distribute:
5224     Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
5225                                          EndLoc, VarsWithInheritedDSA);
5226     break;
5227   case OMPD_target_update:
5228     Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
5229                                            EndLoc, AStmt);
5230     AllowedNameModifiers.push_back(OMPD_target_update);
5231     break;
5232   case OMPD_distribute_parallel_for:
5233     Res = ActOnOpenMPDistributeParallelForDirective(
5234         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
5235     AllowedNameModifiers.push_back(OMPD_parallel);
5236     break;
5237   case OMPD_distribute_parallel_for_simd:
5238     Res = ActOnOpenMPDistributeParallelForSimdDirective(
5239         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
5240     AllowedNameModifiers.push_back(OMPD_parallel);
5241     if (LangOpts.OpenMP >= 50)
5242       AllowedNameModifiers.push_back(OMPD_simd);
5243     break;
5244   case OMPD_distribute_simd:
5245     Res = ActOnOpenMPDistributeSimdDirective(
5246         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
5247     if (LangOpts.OpenMP >= 50)
5248       AllowedNameModifiers.push_back(OMPD_simd);
5249     break;
5250   case OMPD_target_parallel_for_simd:
5251     Res = ActOnOpenMPTargetParallelForSimdDirective(
5252         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
5253     AllowedNameModifiers.push_back(OMPD_target);
5254     AllowedNameModifiers.push_back(OMPD_parallel);
5255     if (LangOpts.OpenMP >= 50)
5256       AllowedNameModifiers.push_back(OMPD_simd);
5257     break;
5258   case OMPD_target_simd:
5259     Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
5260                                          EndLoc, VarsWithInheritedDSA);
5261     AllowedNameModifiers.push_back(OMPD_target);
5262     if (LangOpts.OpenMP >= 50)
5263       AllowedNameModifiers.push_back(OMPD_simd);
5264     break;
5265   case OMPD_teams_distribute:
5266     Res = ActOnOpenMPTeamsDistributeDirective(
5267         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
5268     break;
5269   case OMPD_teams_distribute_simd:
5270     Res = ActOnOpenMPTeamsDistributeSimdDirective(
5271         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
5272     if (LangOpts.OpenMP >= 50)
5273       AllowedNameModifiers.push_back(OMPD_simd);
5274     break;
5275   case OMPD_teams_distribute_parallel_for_simd:
5276     Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
5277         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
5278     AllowedNameModifiers.push_back(OMPD_parallel);
5279     if (LangOpts.OpenMP >= 50)
5280       AllowedNameModifiers.push_back(OMPD_simd);
5281     break;
5282   case OMPD_teams_distribute_parallel_for:
5283     Res = ActOnOpenMPTeamsDistributeParallelForDirective(
5284         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
5285     AllowedNameModifiers.push_back(OMPD_parallel);
5286     break;
5287   case OMPD_target_teams:
5288     Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
5289                                           EndLoc);
5290     AllowedNameModifiers.push_back(OMPD_target);
5291     break;
5292   case OMPD_target_teams_distribute:
5293     Res = ActOnOpenMPTargetTeamsDistributeDirective(
5294         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
5295     AllowedNameModifiers.push_back(OMPD_target);
5296     break;
5297   case OMPD_target_teams_distribute_parallel_for:
5298     Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
5299         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
5300     AllowedNameModifiers.push_back(OMPD_target);
5301     AllowedNameModifiers.push_back(OMPD_parallel);
5302     break;
5303   case OMPD_target_teams_distribute_parallel_for_simd:
5304     Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
5305         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
5306     AllowedNameModifiers.push_back(OMPD_target);
5307     AllowedNameModifiers.push_back(OMPD_parallel);
5308     if (LangOpts.OpenMP >= 50)
5309       AllowedNameModifiers.push_back(OMPD_simd);
5310     break;
5311   case OMPD_target_teams_distribute_simd:
5312     Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
5313         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
5314     AllowedNameModifiers.push_back(OMPD_target);
5315     if (LangOpts.OpenMP >= 50)
5316       AllowedNameModifiers.push_back(OMPD_simd);
5317     break;
5318   case OMPD_declare_target:
5319   case OMPD_end_declare_target:
5320   case OMPD_threadprivate:
5321   case OMPD_allocate:
5322   case OMPD_declare_reduction:
5323   case OMPD_declare_mapper:
5324   case OMPD_declare_simd:
5325   case OMPD_requires:
5326   case OMPD_declare_variant:
5327   case OMPD_begin_declare_variant:
5328   case OMPD_end_declare_variant:
5329     llvm_unreachable("OpenMP Directive is not allowed");
5330   case OMPD_unknown:
5331     llvm_unreachable("Unknown OpenMP directive");
5332   }
5333 
5334   ErrorFound = Res.isInvalid() || ErrorFound;
5335 
5336   // Check variables in the clauses if default(none) was specified.
5337   if (DSAStack->getDefaultDSA() == DSA_none) {
5338     DSAAttrChecker DSAChecker(DSAStack, *this, nullptr);
5339     for (OMPClause *C : Clauses) {
5340       switch (C->getClauseKind()) {
5341       case OMPC_num_threads:
5342       case OMPC_dist_schedule:
5343         // Do not analyse if no parent teams directive.
5344         if (isOpenMPTeamsDirective(Kind))
5345           break;
5346         continue;
5347       case OMPC_if:
5348         if (isOpenMPTeamsDirective(Kind) &&
5349             cast<OMPIfClause>(C)->getNameModifier() != OMPD_target)
5350           break;
5351         if (isOpenMPParallelDirective(Kind) &&
5352             isOpenMPTaskLoopDirective(Kind) &&
5353             cast<OMPIfClause>(C)->getNameModifier() != OMPD_parallel)
5354           break;
5355         continue;
5356       case OMPC_schedule:
5357       case OMPC_detach:
5358         break;
5359       case OMPC_grainsize:
5360       case OMPC_num_tasks:
5361       case OMPC_final:
5362       case OMPC_priority:
5363         // Do not analyze if no parent parallel directive.
5364         if (isOpenMPParallelDirective(Kind))
5365           break;
5366         continue;
5367       case OMPC_ordered:
5368       case OMPC_device:
5369       case OMPC_num_teams:
5370       case OMPC_thread_limit:
5371       case OMPC_hint:
5372       case OMPC_collapse:
5373       case OMPC_safelen:
5374       case OMPC_simdlen:
5375       case OMPC_default:
5376       case OMPC_proc_bind:
5377       case OMPC_private:
5378       case OMPC_firstprivate:
5379       case OMPC_lastprivate:
5380       case OMPC_shared:
5381       case OMPC_reduction:
5382       case OMPC_task_reduction:
5383       case OMPC_in_reduction:
5384       case OMPC_linear:
5385       case OMPC_aligned:
5386       case OMPC_copyin:
5387       case OMPC_copyprivate:
5388       case OMPC_nowait:
5389       case OMPC_untied:
5390       case OMPC_mergeable:
5391       case OMPC_allocate:
5392       case OMPC_read:
5393       case OMPC_write:
5394       case OMPC_update:
5395       case OMPC_capture:
5396       case OMPC_seq_cst:
5397       case OMPC_acq_rel:
5398       case OMPC_acquire:
5399       case OMPC_release:
5400       case OMPC_relaxed:
5401       case OMPC_depend:
5402       case OMPC_threads:
5403       case OMPC_simd:
5404       case OMPC_map:
5405       case OMPC_nogroup:
5406       case OMPC_defaultmap:
5407       case OMPC_to:
5408       case OMPC_from:
5409       case OMPC_use_device_ptr:
5410       case OMPC_is_device_ptr:
5411       case OMPC_nontemporal:
5412       case OMPC_order:
5413       case OMPC_destroy:
5414       case OMPC_inclusive:
5415       case OMPC_exclusive:
5416       case OMPC_uses_allocators:
5417         continue;
5418       case OMPC_allocator:
5419       case OMPC_flush:
5420       case OMPC_depobj:
5421       case OMPC_threadprivate:
5422       case OMPC_uniform:
5423       case OMPC_unknown:
5424       case OMPC_unified_address:
5425       case OMPC_unified_shared_memory:
5426       case OMPC_reverse_offload:
5427       case OMPC_dynamic_allocators:
5428       case OMPC_atomic_default_mem_order:
5429       case OMPC_device_type:
5430       case OMPC_match:
5431         llvm_unreachable("Unexpected clause");
5432       }
5433       for (Stmt *CC : C->children()) {
5434         if (CC)
5435           DSAChecker.Visit(CC);
5436       }
5437     }
5438     for (const auto &P : DSAChecker.getVarsWithInheritedDSA())
5439       VarsWithInheritedDSA[P.getFirst()] = P.getSecond();
5440   }
5441   for (const auto &P : VarsWithInheritedDSA) {
5442     if (P.getFirst()->isImplicit() || isa<OMPCapturedExprDecl>(P.getFirst()))
5443       continue;
5444     ErrorFound = true;
5445     if (DSAStack->getDefaultDSA() == DSA_none) {
5446       Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
5447           << P.first << P.second->getSourceRange();
5448       Diag(DSAStack->getDefaultDSALocation(), diag::note_omp_default_dsa_none);
5449     } else if (getLangOpts().OpenMP >= 50) {
5450       Diag(P.second->getExprLoc(),
5451            diag::err_omp_defaultmap_no_attr_for_variable)
5452           << P.first << P.second->getSourceRange();
5453       Diag(DSAStack->getDefaultDSALocation(),
5454            diag::note_omp_defaultmap_attr_none);
5455     }
5456   }
5457 
5458   if (!AllowedNameModifiers.empty())
5459     ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
5460                  ErrorFound;
5461 
5462   if (ErrorFound)
5463     return StmtError();
5464 
5465   if (!CurContext->isDependentContext() &&
5466       isOpenMPTargetExecutionDirective(Kind) &&
5467       !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() ||
5468         DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() ||
5469         DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() ||
5470         DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) {
5471     // Register target to DSA Stack.
5472     DSAStack->addTargetDirLocation(StartLoc);
5473   }
5474 
5475   return Res;
5476 }
5477 
5478 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
5479     DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
5480     ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
5481     ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
5482     ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
5483   assert(Aligneds.size() == Alignments.size());
5484   assert(Linears.size() == LinModifiers.size());
5485   assert(Linears.size() == Steps.size());
5486   if (!DG || DG.get().isNull())
5487     return DeclGroupPtrTy();
5488 
5489   const int SimdId = 0;
5490   if (!DG.get().isSingleDecl()) {
5491     Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant)
5492         << SimdId;
5493     return DG;
5494   }
5495   Decl *ADecl = DG.get().getSingleDecl();
5496   if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
5497     ADecl = FTD->getTemplatedDecl();
5498 
5499   auto *FD = dyn_cast<FunctionDecl>(ADecl);
5500   if (!FD) {
5501     Diag(ADecl->getLocation(), diag::err_omp_function_expected) << SimdId;
5502     return DeclGroupPtrTy();
5503   }
5504 
5505   // OpenMP [2.8.2, declare simd construct, Description]
5506   // The parameter of the simdlen clause must be a constant positive integer
5507   // expression.
5508   ExprResult SL;
5509   if (Simdlen)
5510     SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
5511   // OpenMP [2.8.2, declare simd construct, Description]
5512   // The special this pointer can be used as if was one of the arguments to the
5513   // function in any of the linear, aligned, or uniform clauses.
5514   // The uniform clause declares one or more arguments to have an invariant
5515   // value for all concurrent invocations of the function in the execution of a
5516   // single SIMD loop.
5517   llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
5518   const Expr *UniformedLinearThis = nullptr;
5519   for (const Expr *E : Uniforms) {
5520     E = E->IgnoreParenImpCasts();
5521     if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
5522       if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
5523         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
5524             FD->getParamDecl(PVD->getFunctionScopeIndex())
5525                     ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
5526           UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E);
5527           continue;
5528         }
5529     if (isa<CXXThisExpr>(E)) {
5530       UniformedLinearThis = E;
5531       continue;
5532     }
5533     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
5534         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
5535   }
5536   // OpenMP [2.8.2, declare simd construct, Description]
5537   // The aligned clause declares that the object to which each list item points
5538   // is aligned to the number of bytes expressed in the optional parameter of
5539   // the aligned clause.
5540   // The special this pointer can be used as if was one of the arguments to the
5541   // function in any of the linear, aligned, or uniform clauses.
5542   // The type of list items appearing in the aligned clause must be array,
5543   // pointer, reference to array, or reference to pointer.
5544   llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
5545   const Expr *AlignedThis = nullptr;
5546   for (const Expr *E : Aligneds) {
5547     E = E->IgnoreParenImpCasts();
5548     if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
5549       if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
5550         const VarDecl *CanonPVD = PVD->getCanonicalDecl();
5551         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
5552             FD->getParamDecl(PVD->getFunctionScopeIndex())
5553                     ->getCanonicalDecl() == CanonPVD) {
5554           // OpenMP  [2.8.1, simd construct, Restrictions]
5555           // A list-item cannot appear in more than one aligned clause.
5556           if (AlignedArgs.count(CanonPVD) > 0) {
5557             Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice)
5558                 << 1 << getOpenMPClauseName(OMPC_aligned)
5559                 << E->getSourceRange();
5560             Diag(AlignedArgs[CanonPVD]->getExprLoc(),
5561                  diag::note_omp_explicit_dsa)
5562                 << getOpenMPClauseName(OMPC_aligned);
5563             continue;
5564           }
5565           AlignedArgs[CanonPVD] = E;
5566           QualType QTy = PVD->getType()
5567                              .getNonReferenceType()
5568                              .getUnqualifiedType()
5569                              .getCanonicalType();
5570           const Type *Ty = QTy.getTypePtrOrNull();
5571           if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
5572             Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
5573                 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
5574             Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
5575           }
5576           continue;
5577         }
5578       }
5579     if (isa<CXXThisExpr>(E)) {
5580       if (AlignedThis) {
5581         Diag(E->getExprLoc(), diag::err_omp_used_in_clause_twice)
5582             << 2 << getOpenMPClauseName(OMPC_aligned) << E->getSourceRange();
5583         Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
5584             << getOpenMPClauseName(OMPC_aligned);
5585       }
5586       AlignedThis = E;
5587       continue;
5588     }
5589     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
5590         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
5591   }
5592   // The optional parameter of the aligned clause, alignment, must be a constant
5593   // positive integer expression. If no optional parameter is specified,
5594   // implementation-defined default alignments for SIMD instructions on the
5595   // target platforms are assumed.
5596   SmallVector<const Expr *, 4> NewAligns;
5597   for (Expr *E : Alignments) {
5598     ExprResult Align;
5599     if (E)
5600       Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
5601     NewAligns.push_back(Align.get());
5602   }
5603   // OpenMP [2.8.2, declare simd construct, Description]
5604   // The linear clause declares one or more list items to be private to a SIMD
5605   // lane and to have a linear relationship with respect to the iteration space
5606   // of a loop.
5607   // The special this pointer can be used as if was one of the arguments to the
5608   // function in any of the linear, aligned, or uniform clauses.
5609   // When a linear-step expression is specified in a linear clause it must be
5610   // either a constant integer expression or an integer-typed parameter that is
5611   // specified in a uniform clause on the directive.
5612   llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
5613   const bool IsUniformedThis = UniformedLinearThis != nullptr;
5614   auto MI = LinModifiers.begin();
5615   for (const Expr *E : Linears) {
5616     auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
5617     ++MI;
5618     E = E->IgnoreParenImpCasts();
5619     if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
5620       if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
5621         const VarDecl *CanonPVD = PVD->getCanonicalDecl();
5622         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
5623             FD->getParamDecl(PVD->getFunctionScopeIndex())
5624                     ->getCanonicalDecl() == CanonPVD) {
5625           // OpenMP  [2.15.3.7, linear Clause, Restrictions]
5626           // A list-item cannot appear in more than one linear clause.
5627           if (LinearArgs.count(CanonPVD) > 0) {
5628             Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
5629                 << getOpenMPClauseName(OMPC_linear)
5630                 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
5631             Diag(LinearArgs[CanonPVD]->getExprLoc(),
5632                  diag::note_omp_explicit_dsa)
5633                 << getOpenMPClauseName(OMPC_linear);
5634             continue;
5635           }
5636           // Each argument can appear in at most one uniform or linear clause.
5637           if (UniformedArgs.count(CanonPVD) > 0) {
5638             Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
5639                 << getOpenMPClauseName(OMPC_linear)
5640                 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
5641             Diag(UniformedArgs[CanonPVD]->getExprLoc(),
5642                  diag::note_omp_explicit_dsa)
5643                 << getOpenMPClauseName(OMPC_uniform);
5644             continue;
5645           }
5646           LinearArgs[CanonPVD] = E;
5647           if (E->isValueDependent() || E->isTypeDependent() ||
5648               E->isInstantiationDependent() ||
5649               E->containsUnexpandedParameterPack())
5650             continue;
5651           (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
5652                                       PVD->getOriginalType(),
5653                                       /*IsDeclareSimd=*/true);
5654           continue;
5655         }
5656       }
5657     if (isa<CXXThisExpr>(E)) {
5658       if (UniformedLinearThis) {
5659         Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
5660             << getOpenMPClauseName(OMPC_linear)
5661             << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
5662             << E->getSourceRange();
5663         Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
5664             << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
5665                                                    : OMPC_linear);
5666         continue;
5667       }
5668       UniformedLinearThis = E;
5669       if (E->isValueDependent() || E->isTypeDependent() ||
5670           E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
5671         continue;
5672       (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
5673                                   E->getType(), /*IsDeclareSimd=*/true);
5674       continue;
5675     }
5676     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
5677         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
5678   }
5679   Expr *Step = nullptr;
5680   Expr *NewStep = nullptr;
5681   SmallVector<Expr *, 4> NewSteps;
5682   for (Expr *E : Steps) {
5683     // Skip the same step expression, it was checked already.
5684     if (Step == E || !E) {
5685       NewSteps.push_back(E ? NewStep : nullptr);
5686       continue;
5687     }
5688     Step = E;
5689     if (const auto *DRE = dyn_cast<DeclRefExpr>(Step))
5690       if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
5691         const VarDecl *CanonPVD = PVD->getCanonicalDecl();
5692         if (UniformedArgs.count(CanonPVD) == 0) {
5693           Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
5694               << Step->getSourceRange();
5695         } else if (E->isValueDependent() || E->isTypeDependent() ||
5696                    E->isInstantiationDependent() ||
5697                    E->containsUnexpandedParameterPack() ||
5698                    CanonPVD->getType()->hasIntegerRepresentation()) {
5699           NewSteps.push_back(Step);
5700         } else {
5701           Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
5702               << Step->getSourceRange();
5703         }
5704         continue;
5705       }
5706     NewStep = Step;
5707     if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
5708         !Step->isInstantiationDependent() &&
5709         !Step->containsUnexpandedParameterPack()) {
5710       NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
5711                     .get();
5712       if (NewStep)
5713         NewStep = VerifyIntegerConstantExpression(NewStep).get();
5714     }
5715     NewSteps.push_back(NewStep);
5716   }
5717   auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
5718       Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
5719       Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
5720       const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
5721       const_cast<Expr **>(Linears.data()), Linears.size(),
5722       const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
5723       NewSteps.data(), NewSteps.size(), SR);
5724   ADecl->addAttr(NewAttr);
5725   return DG;
5726 }
5727 
5728 static void setPrototype(Sema &S, FunctionDecl *FD, FunctionDecl *FDWithProto,
5729                          QualType NewType) {
5730   assert(NewType->isFunctionProtoType() &&
5731          "Expected function type with prototype.");
5732   assert(FD->getType()->isFunctionNoProtoType() &&
5733          "Expected function with type with no prototype.");
5734   assert(FDWithProto->getType()->isFunctionProtoType() &&
5735          "Expected function with prototype.");
5736   // Synthesize parameters with the same types.
5737   FD->setType(NewType);
5738   SmallVector<ParmVarDecl *, 16> Params;
5739   for (const ParmVarDecl *P : FDWithProto->parameters()) {
5740     auto *Param = ParmVarDecl::Create(S.getASTContext(), FD, SourceLocation(),
5741                                       SourceLocation(), nullptr, P->getType(),
5742                                       /*TInfo=*/nullptr, SC_None, nullptr);
5743     Param->setScopeInfo(0, Params.size());
5744     Param->setImplicit();
5745     Params.push_back(Param);
5746   }
5747 
5748   FD->setParams(Params);
5749 }
5750 
5751 Sema::OMPDeclareVariantScope::OMPDeclareVariantScope(OMPTraitInfo &TI)
5752     : TI(&TI), NameSuffix(TI.getMangledName()) {}
5753 
5754 FunctionDecl *
5755 Sema::ActOnStartOfFunctionDefinitionInOpenMPDeclareVariantScope(Scope *S,
5756                                                                 Declarator &D) {
5757   IdentifierInfo *BaseII = D.getIdentifier();
5758   LookupResult Lookup(*this, DeclarationName(BaseII), D.getIdentifierLoc(),
5759                       LookupOrdinaryName);
5760   LookupParsedName(Lookup, S, &D.getCXXScopeSpec());
5761 
5762   TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
5763   QualType FType = TInfo->getType();
5764 
5765   bool IsConstexpr = D.getDeclSpec().getConstexprSpecifier() == CSK_constexpr;
5766   bool IsConsteval = D.getDeclSpec().getConstexprSpecifier() == CSK_consteval;
5767 
5768   FunctionDecl *BaseFD = nullptr;
5769   for (auto *Candidate : Lookup) {
5770     auto *UDecl = dyn_cast<FunctionDecl>(Candidate->getUnderlyingDecl());
5771     if (!UDecl)
5772       continue;
5773 
5774     // Don't specialize constexpr/consteval functions with
5775     // non-constexpr/consteval functions.
5776     if (UDecl->isConstexpr() && !IsConstexpr)
5777       continue;
5778     if (UDecl->isConsteval() && !IsConsteval)
5779       continue;
5780 
5781     QualType NewType = Context.mergeFunctionTypes(
5782         FType, UDecl->getType(), /* OfBlockPointer */ false,
5783         /* Unqualified */ false, /* AllowCXX */ true);
5784     if (NewType.isNull())
5785       continue;
5786 
5787     // Found a base!
5788     BaseFD = UDecl;
5789     break;
5790   }
5791   if (!BaseFD) {
5792     BaseFD = cast<FunctionDecl>(ActOnDeclarator(S, D));
5793     BaseFD->setImplicit(true);
5794   }
5795 
5796   OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back();
5797   std::string MangledName;
5798   MangledName += D.getIdentifier()->getName();
5799   MangledName += getOpenMPVariantManglingSeparatorStr();
5800   MangledName += DVScope.NameSuffix;
5801   IdentifierInfo &VariantII = Context.Idents.get(MangledName);
5802 
5803   VariantII.setMangledOpenMPVariantName(true);
5804   D.SetIdentifier(&VariantII, D.getBeginLoc());
5805   return BaseFD;
5806 }
5807 
5808 void Sema::ActOnFinishedFunctionDefinitionInOpenMPDeclareVariantScope(
5809     FunctionDecl *FD, FunctionDecl *BaseFD) {
5810   // Do not mark function as is used to prevent its emission if this is the
5811   // only place where it is used.
5812   EnterExpressionEvaluationContext Unevaluated(
5813       *this, Sema::ExpressionEvaluationContext::Unevaluated);
5814 
5815   Expr *VariantFuncRef = DeclRefExpr::Create(
5816       Context, NestedNameSpecifierLoc(), SourceLocation(), FD,
5817       /* RefersToEnclosingVariableOrCapture */ false,
5818       /* NameLoc */ FD->getLocation(), FD->getType(), ExprValueKind::VK_RValue);
5819 
5820   OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back();
5821   auto *OMPDeclareVariantA = OMPDeclareVariantAttr::CreateImplicit(
5822       Context, VariantFuncRef, DVScope.TI);
5823   BaseFD->addAttr(OMPDeclareVariantA);
5824 }
5825 
5826 ExprResult Sema::ActOnOpenMPCall(ExprResult Call, Scope *Scope,
5827                                  SourceLocation LParenLoc,
5828                                  MultiExprArg ArgExprs,
5829                                  SourceLocation RParenLoc, Expr *ExecConfig) {
5830   // The common case is a regular call we do not want to specialize at all. Try
5831   // to make that case fast by bailing early.
5832   CallExpr *CE = dyn_cast<CallExpr>(Call.get());
5833   if (!CE)
5834     return Call;
5835 
5836   FunctionDecl *CalleeFnDecl = CE->getDirectCallee();
5837   if (!CalleeFnDecl)
5838     return Call;
5839 
5840   if (!CalleeFnDecl->hasAttr<OMPDeclareVariantAttr>())
5841     return Call;
5842 
5843   ASTContext &Context = getASTContext();
5844   OMPContext OMPCtx(getLangOpts().OpenMPIsDevice,
5845                     Context.getTargetInfo().getTriple());
5846 
5847   SmallVector<Expr *, 4> Exprs;
5848   SmallVector<VariantMatchInfo, 4> VMIs;
5849   while (CalleeFnDecl) {
5850     for (OMPDeclareVariantAttr *A :
5851          CalleeFnDecl->specific_attrs<OMPDeclareVariantAttr>()) {
5852       Expr *VariantRef = A->getVariantFuncRef();
5853 
5854       VariantMatchInfo VMI;
5855       OMPTraitInfo &TI = A->getTraitInfo();
5856       TI.getAsVariantMatchInfo(Context, VMI);
5857       if (!isVariantApplicableInContext(VMI, OMPCtx, /* DeviceSetOnly */ false))
5858         continue;
5859 
5860       VMIs.push_back(VMI);
5861       Exprs.push_back(VariantRef);
5862     }
5863 
5864     CalleeFnDecl = CalleeFnDecl->getPreviousDecl();
5865   }
5866 
5867   ExprResult NewCall;
5868   do {
5869     int BestIdx = getBestVariantMatchForContext(VMIs, OMPCtx);
5870     if (BestIdx < 0)
5871       return Call;
5872     Expr *BestExpr = cast<DeclRefExpr>(Exprs[BestIdx]);
5873     Decl *BestDecl = cast<DeclRefExpr>(BestExpr)->getDecl();
5874 
5875     {
5876       // Try to build a (member) call expression for the current best applicable
5877       // variant expression. We allow this to fail in which case we continue
5878       // with the next best variant expression. The fail case is part of the
5879       // implementation defined behavior in the OpenMP standard when it talks
5880       // about what differences in the function prototypes: "Any differences
5881       // that the specific OpenMP context requires in the prototype of the
5882       // variant from the base function prototype are implementation defined."
5883       // This wording is there to allow the specialized variant to have a
5884       // different type than the base function. This is intended and OK but if
5885       // we cannot create a call the difference is not in the "implementation
5886       // defined range" we allow.
5887       Sema::TentativeAnalysisScope Trap(*this);
5888 
5889       if (auto *SpecializedMethod = dyn_cast<CXXMethodDecl>(BestDecl)) {
5890         auto *MemberCall = dyn_cast<CXXMemberCallExpr>(CE);
5891         BestExpr = MemberExpr::CreateImplicit(
5892             Context, MemberCall->getImplicitObjectArgument(),
5893             /* IsArrow */ false, SpecializedMethod, Context.BoundMemberTy,
5894             MemberCall->getValueKind(), MemberCall->getObjectKind());
5895       }
5896       NewCall = BuildCallExpr(Scope, BestExpr, LParenLoc, ArgExprs, RParenLoc,
5897                               ExecConfig);
5898       if (NewCall.isUsable())
5899         break;
5900     }
5901 
5902     VMIs.erase(VMIs.begin() + BestIdx);
5903     Exprs.erase(Exprs.begin() + BestIdx);
5904   } while (!VMIs.empty());
5905 
5906   if (!NewCall.isUsable())
5907     return Call;
5908   return PseudoObjectExpr::Create(Context, CE, {NewCall.get()}, 0);
5909 }
5910 
5911 Optional<std::pair<FunctionDecl *, Expr *>>
5912 Sema::checkOpenMPDeclareVariantFunction(Sema::DeclGroupPtrTy DG,
5913                                         Expr *VariantRef, OMPTraitInfo &TI,
5914                                         SourceRange SR) {
5915   if (!DG || DG.get().isNull())
5916     return None;
5917 
5918   const int VariantId = 1;
5919   // Must be applied only to single decl.
5920   if (!DG.get().isSingleDecl()) {
5921     Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd_variant)
5922         << VariantId << SR;
5923     return None;
5924   }
5925   Decl *ADecl = DG.get().getSingleDecl();
5926   if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
5927     ADecl = FTD->getTemplatedDecl();
5928 
5929   // Decl must be a function.
5930   auto *FD = dyn_cast<FunctionDecl>(ADecl);
5931   if (!FD) {
5932     Diag(ADecl->getLocation(), diag::err_omp_function_expected)
5933         << VariantId << SR;
5934     return None;
5935   }
5936 
5937   auto &&HasMultiVersionAttributes = [](const FunctionDecl *FD) {
5938     return FD->hasAttrs() &&
5939            (FD->hasAttr<CPUDispatchAttr>() || FD->hasAttr<CPUSpecificAttr>() ||
5940             FD->hasAttr<TargetAttr>());
5941   };
5942   // OpenMP is not compatible with CPU-specific attributes.
5943   if (HasMultiVersionAttributes(FD)) {
5944     Diag(FD->getLocation(), diag::err_omp_declare_variant_incompat_attributes)
5945         << SR;
5946     return None;
5947   }
5948 
5949   // Allow #pragma omp declare variant only if the function is not used.
5950   if (FD->isUsed(false))
5951     Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_used)
5952         << FD->getLocation();
5953 
5954   // Check if the function was emitted already.
5955   const FunctionDecl *Definition;
5956   if (!FD->isThisDeclarationADefinition() && FD->isDefined(Definition) &&
5957       (LangOpts.EmitAllDecls || Context.DeclMustBeEmitted(Definition)))
5958     Diag(SR.getBegin(), diag::warn_omp_declare_variant_after_emitted)
5959         << FD->getLocation();
5960 
5961   // The VariantRef must point to function.
5962   if (!VariantRef) {
5963     Diag(SR.getBegin(), diag::err_omp_function_expected) << VariantId;
5964     return None;
5965   }
5966 
5967   auto ShouldDelayChecks = [](Expr *&E, bool) {
5968     return E && (E->isTypeDependent() || E->isValueDependent() ||
5969                  E->containsUnexpandedParameterPack() ||
5970                  E->isInstantiationDependent());
5971   };
5972   // Do not check templates, wait until instantiation.
5973   if (FD->isDependentContext() || ShouldDelayChecks(VariantRef, false) ||
5974       TI.anyScoreOrCondition(ShouldDelayChecks))
5975     return std::make_pair(FD, VariantRef);
5976 
5977   // Deal with non-constant score and user condition expressions.
5978   auto HandleNonConstantScoresAndConditions = [this](Expr *&E,
5979                                                      bool IsScore) -> bool {
5980     llvm::APSInt Result;
5981     if (!E || E->isIntegerConstantExpr(Result, Context))
5982       return false;
5983 
5984     if (IsScore) {
5985       // We warn on non-constant scores and pretend they were not present.
5986       Diag(E->getExprLoc(), diag::warn_omp_declare_variant_score_not_constant)
5987           << E;
5988       E = nullptr;
5989     } else {
5990       // We could replace a non-constant user condition with "false" but we
5991       // will soon need to handle these anyway for the dynamic version of
5992       // OpenMP context selectors.
5993       Diag(E->getExprLoc(),
5994            diag::err_omp_declare_variant_user_condition_not_constant)
5995           << E;
5996     }
5997     return true;
5998   };
5999   if (TI.anyScoreOrCondition(HandleNonConstantScoresAndConditions))
6000     return None;
6001 
6002   // Convert VariantRef expression to the type of the original function to
6003   // resolve possible conflicts.
6004   ExprResult VariantRefCast;
6005   if (LangOpts.CPlusPlus) {
6006     QualType FnPtrType;
6007     auto *Method = dyn_cast<CXXMethodDecl>(FD);
6008     if (Method && !Method->isStatic()) {
6009       const Type *ClassType =
6010           Context.getTypeDeclType(Method->getParent()).getTypePtr();
6011       FnPtrType = Context.getMemberPointerType(FD->getType(), ClassType);
6012       ExprResult ER;
6013       {
6014         // Build adrr_of unary op to correctly handle type checks for member
6015         // functions.
6016         Sema::TentativeAnalysisScope Trap(*this);
6017         ER = CreateBuiltinUnaryOp(VariantRef->getBeginLoc(), UO_AddrOf,
6018                                   VariantRef);
6019       }
6020       if (!ER.isUsable()) {
6021         Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected)
6022             << VariantId << VariantRef->getSourceRange();
6023         return None;
6024       }
6025       VariantRef = ER.get();
6026     } else {
6027       FnPtrType = Context.getPointerType(FD->getType());
6028     }
6029     ImplicitConversionSequence ICS =
6030         TryImplicitConversion(VariantRef, FnPtrType.getUnqualifiedType(),
6031                               /*SuppressUserConversions=*/false,
6032                               AllowedExplicit::None,
6033                               /*InOverloadResolution=*/false,
6034                               /*CStyle=*/false,
6035                               /*AllowObjCWritebackConversion=*/false);
6036     if (ICS.isFailure()) {
6037       Diag(VariantRef->getExprLoc(),
6038            diag::err_omp_declare_variant_incompat_types)
6039           << VariantRef->getType()
6040           << ((Method && !Method->isStatic()) ? FnPtrType : FD->getType())
6041           << VariantRef->getSourceRange();
6042       return None;
6043     }
6044     VariantRefCast = PerformImplicitConversion(
6045         VariantRef, FnPtrType.getUnqualifiedType(), AA_Converting);
6046     if (!VariantRefCast.isUsable())
6047       return None;
6048     // Drop previously built artificial addr_of unary op for member functions.
6049     if (Method && !Method->isStatic()) {
6050       Expr *PossibleAddrOfVariantRef = VariantRefCast.get();
6051       if (auto *UO = dyn_cast<UnaryOperator>(
6052               PossibleAddrOfVariantRef->IgnoreImplicit()))
6053         VariantRefCast = UO->getSubExpr();
6054     }
6055   } else {
6056     VariantRefCast = VariantRef;
6057   }
6058 
6059   ExprResult ER = CheckPlaceholderExpr(VariantRefCast.get());
6060   if (!ER.isUsable() ||
6061       !ER.get()->IgnoreParenImpCasts()->getType()->isFunctionType()) {
6062     Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected)
6063         << VariantId << VariantRef->getSourceRange();
6064     return None;
6065   }
6066 
6067   // The VariantRef must point to function.
6068   auto *DRE = dyn_cast<DeclRefExpr>(ER.get()->IgnoreParenImpCasts());
6069   if (!DRE) {
6070     Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected)
6071         << VariantId << VariantRef->getSourceRange();
6072     return None;
6073   }
6074   auto *NewFD = dyn_cast_or_null<FunctionDecl>(DRE->getDecl());
6075   if (!NewFD) {
6076     Diag(VariantRef->getExprLoc(), diag::err_omp_function_expected)
6077         << VariantId << VariantRef->getSourceRange();
6078     return None;
6079   }
6080 
6081   // Check if function types are compatible in C.
6082   if (!LangOpts.CPlusPlus) {
6083     QualType NewType =
6084         Context.mergeFunctionTypes(FD->getType(), NewFD->getType());
6085     if (NewType.isNull()) {
6086       Diag(VariantRef->getExprLoc(),
6087            diag::err_omp_declare_variant_incompat_types)
6088           << NewFD->getType() << FD->getType() << VariantRef->getSourceRange();
6089       return None;
6090     }
6091     if (NewType->isFunctionProtoType()) {
6092       if (FD->getType()->isFunctionNoProtoType())
6093         setPrototype(*this, FD, NewFD, NewType);
6094       else if (NewFD->getType()->isFunctionNoProtoType())
6095         setPrototype(*this, NewFD, FD, NewType);
6096     }
6097   }
6098 
6099   // Check if variant function is not marked with declare variant directive.
6100   if (NewFD->hasAttrs() && NewFD->hasAttr<OMPDeclareVariantAttr>()) {
6101     Diag(VariantRef->getExprLoc(),
6102          diag::warn_omp_declare_variant_marked_as_declare_variant)
6103         << VariantRef->getSourceRange();
6104     SourceRange SR =
6105         NewFD->specific_attr_begin<OMPDeclareVariantAttr>()->getRange();
6106     Diag(SR.getBegin(), diag::note_omp_marked_declare_variant_here) << SR;
6107     return None;
6108   }
6109 
6110   enum DoesntSupport {
6111     VirtFuncs = 1,
6112     Constructors = 3,
6113     Destructors = 4,
6114     DeletedFuncs = 5,
6115     DefaultedFuncs = 6,
6116     ConstexprFuncs = 7,
6117     ConstevalFuncs = 8,
6118   };
6119   if (const auto *CXXFD = dyn_cast<CXXMethodDecl>(FD)) {
6120     if (CXXFD->isVirtual()) {
6121       Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
6122           << VirtFuncs;
6123       return None;
6124     }
6125 
6126     if (isa<CXXConstructorDecl>(FD)) {
6127       Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
6128           << Constructors;
6129       return None;
6130     }
6131 
6132     if (isa<CXXDestructorDecl>(FD)) {
6133       Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
6134           << Destructors;
6135       return None;
6136     }
6137   }
6138 
6139   if (FD->isDeleted()) {
6140     Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
6141         << DeletedFuncs;
6142     return None;
6143   }
6144 
6145   if (FD->isDefaulted()) {
6146     Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
6147         << DefaultedFuncs;
6148     return None;
6149   }
6150 
6151   if (FD->isConstexpr()) {
6152     Diag(FD->getLocation(), diag::err_omp_declare_variant_doesnt_support)
6153         << (NewFD->isConsteval() ? ConstevalFuncs : ConstexprFuncs);
6154     return None;
6155   }
6156 
6157   // Check general compatibility.
6158   if (areMultiversionVariantFunctionsCompatible(
6159           FD, NewFD, PartialDiagnostic::NullDiagnostic(),
6160           PartialDiagnosticAt(SourceLocation(),
6161                               PartialDiagnostic::NullDiagnostic()),
6162           PartialDiagnosticAt(
6163               VariantRef->getExprLoc(),
6164               PDiag(diag::err_omp_declare_variant_doesnt_support)),
6165           PartialDiagnosticAt(VariantRef->getExprLoc(),
6166                               PDiag(diag::err_omp_declare_variant_diff)
6167                                   << FD->getLocation()),
6168           /*TemplatesSupported=*/true, /*ConstexprSupported=*/false,
6169           /*CLinkageMayDiffer=*/true))
6170     return None;
6171   return std::make_pair(FD, cast<Expr>(DRE));
6172 }
6173 
6174 void Sema::ActOnOpenMPDeclareVariantDirective(FunctionDecl *FD,
6175                                               Expr *VariantRef,
6176                                               OMPTraitInfo &TI,
6177                                               SourceRange SR) {
6178   auto *NewAttr =
6179       OMPDeclareVariantAttr::CreateImplicit(Context, VariantRef, &TI, SR);
6180   FD->addAttr(NewAttr);
6181 }
6182 
6183 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
6184                                               Stmt *AStmt,
6185                                               SourceLocation StartLoc,
6186                                               SourceLocation EndLoc) {
6187   if (!AStmt)
6188     return StmtError();
6189 
6190   auto *CS = cast<CapturedStmt>(AStmt);
6191   // 1.2.2 OpenMP Language Terminology
6192   // Structured block - An executable statement with a single entry at the
6193   // top and a single exit at the bottom.
6194   // The point of exit cannot be a branch out of the structured block.
6195   // longjmp() and throw() must not violate the entry/exit criteria.
6196   CS->getCapturedDecl()->setNothrow();
6197 
6198   setFunctionHasBranchProtectedScope();
6199 
6200   return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
6201                                       DSAStack->getTaskgroupReductionRef(),
6202                                       DSAStack->isCancelRegion());
6203 }
6204 
6205 namespace {
6206 /// Iteration space of a single for loop.
6207 struct LoopIterationSpace final {
6208   /// True if the condition operator is the strict compare operator (<, > or
6209   /// !=).
6210   bool IsStrictCompare = false;
6211   /// Condition of the loop.
6212   Expr *PreCond = nullptr;
6213   /// This expression calculates the number of iterations in the loop.
6214   /// It is always possible to calculate it before starting the loop.
6215   Expr *NumIterations = nullptr;
6216   /// The loop counter variable.
6217   Expr *CounterVar = nullptr;
6218   /// Private loop counter variable.
6219   Expr *PrivateCounterVar = nullptr;
6220   /// This is initializer for the initial value of #CounterVar.
6221   Expr *CounterInit = nullptr;
6222   /// This is step for the #CounterVar used to generate its update:
6223   /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
6224   Expr *CounterStep = nullptr;
6225   /// Should step be subtracted?
6226   bool Subtract = false;
6227   /// Source range of the loop init.
6228   SourceRange InitSrcRange;
6229   /// Source range of the loop condition.
6230   SourceRange CondSrcRange;
6231   /// Source range of the loop increment.
6232   SourceRange IncSrcRange;
6233   /// Minimum value that can have the loop control variable. Used to support
6234   /// non-rectangular loops. Applied only for LCV with the non-iterator types,
6235   /// since only such variables can be used in non-loop invariant expressions.
6236   Expr *MinValue = nullptr;
6237   /// Maximum value that can have the loop control variable. Used to support
6238   /// non-rectangular loops. Applied only for LCV with the non-iterator type,
6239   /// since only such variables can be used in non-loop invariant expressions.
6240   Expr *MaxValue = nullptr;
6241   /// true, if the lower bound depends on the outer loop control var.
6242   bool IsNonRectangularLB = false;
6243   /// true, if the upper bound depends on the outer loop control var.
6244   bool IsNonRectangularUB = false;
6245   /// Index of the loop this loop depends on and forms non-rectangular loop
6246   /// nest.
6247   unsigned LoopDependentIdx = 0;
6248   /// Final condition for the non-rectangular loop nest support. It is used to
6249   /// check that the number of iterations for this particular counter must be
6250   /// finished.
6251   Expr *FinalCondition = nullptr;
6252 };
6253 
6254 /// Helper class for checking canonical form of the OpenMP loops and
6255 /// extracting iteration space of each loop in the loop nest, that will be used
6256 /// for IR generation.
6257 class OpenMPIterationSpaceChecker {
6258   /// Reference to Sema.
6259   Sema &SemaRef;
6260   /// Data-sharing stack.
6261   DSAStackTy &Stack;
6262   /// A location for diagnostics (when there is no some better location).
6263   SourceLocation DefaultLoc;
6264   /// A location for diagnostics (when increment is not compatible).
6265   SourceLocation ConditionLoc;
6266   /// A source location for referring to loop init later.
6267   SourceRange InitSrcRange;
6268   /// A source location for referring to condition later.
6269   SourceRange ConditionSrcRange;
6270   /// A source location for referring to increment later.
6271   SourceRange IncrementSrcRange;
6272   /// Loop variable.
6273   ValueDecl *LCDecl = nullptr;
6274   /// Reference to loop variable.
6275   Expr *LCRef = nullptr;
6276   /// Lower bound (initializer for the var).
6277   Expr *LB = nullptr;
6278   /// Upper bound.
6279   Expr *UB = nullptr;
6280   /// Loop step (increment).
6281   Expr *Step = nullptr;
6282   /// This flag is true when condition is one of:
6283   ///   Var <  UB
6284   ///   Var <= UB
6285   ///   UB  >  Var
6286   ///   UB  >= Var
6287   /// This will have no value when the condition is !=
6288   llvm::Optional<bool> TestIsLessOp;
6289   /// This flag is true when condition is strict ( < or > ).
6290   bool TestIsStrictOp = false;
6291   /// This flag is true when step is subtracted on each iteration.
6292   bool SubtractStep = false;
6293   /// The outer loop counter this loop depends on (if any).
6294   const ValueDecl *DepDecl = nullptr;
6295   /// Contains number of loop (starts from 1) on which loop counter init
6296   /// expression of this loop depends on.
6297   Optional<unsigned> InitDependOnLC;
6298   /// Contains number of loop (starts from 1) on which loop counter condition
6299   /// expression of this loop depends on.
6300   Optional<unsigned> CondDependOnLC;
6301   /// Checks if the provide statement depends on the loop counter.
6302   Optional<unsigned> doesDependOnLoopCounter(const Stmt *S, bool IsInitializer);
6303   /// Original condition required for checking of the exit condition for
6304   /// non-rectangular loop.
6305   Expr *Condition = nullptr;
6306 
6307 public:
6308   OpenMPIterationSpaceChecker(Sema &SemaRef, DSAStackTy &Stack,
6309                               SourceLocation DefaultLoc)
6310       : SemaRef(SemaRef), Stack(Stack), DefaultLoc(DefaultLoc),
6311         ConditionLoc(DefaultLoc) {}
6312   /// Check init-expr for canonical loop form and save loop counter
6313   /// variable - #Var and its initialization value - #LB.
6314   bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
6315   /// Check test-expr for canonical form, save upper-bound (#UB), flags
6316   /// for less/greater and for strict/non-strict comparison.
6317   bool checkAndSetCond(Expr *S);
6318   /// Check incr-expr for canonical loop form and return true if it
6319   /// does not conform, otherwise save loop step (#Step).
6320   bool checkAndSetInc(Expr *S);
6321   /// Return the loop counter variable.
6322   ValueDecl *getLoopDecl() const { return LCDecl; }
6323   /// Return the reference expression to loop counter variable.
6324   Expr *getLoopDeclRefExpr() const { return LCRef; }
6325   /// Source range of the loop init.
6326   SourceRange getInitSrcRange() const { return InitSrcRange; }
6327   /// Source range of the loop condition.
6328   SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
6329   /// Source range of the loop increment.
6330   SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
6331   /// True if the step should be subtracted.
6332   bool shouldSubtractStep() const { return SubtractStep; }
6333   /// True, if the compare operator is strict (<, > or !=).
6334   bool isStrictTestOp() const { return TestIsStrictOp; }
6335   /// Build the expression to calculate the number of iterations.
6336   Expr *buildNumIterations(
6337       Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType,
6338       llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
6339   /// Build the precondition expression for the loops.
6340   Expr *
6341   buildPreCond(Scope *S, Expr *Cond,
6342                llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
6343   /// Build reference expression to the counter be used for codegen.
6344   DeclRefExpr *
6345   buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
6346                   DSAStackTy &DSA) const;
6347   /// Build reference expression to the private counter be used for
6348   /// codegen.
6349   Expr *buildPrivateCounterVar() const;
6350   /// Build initialization of the counter be used for codegen.
6351   Expr *buildCounterInit() const;
6352   /// Build step of the counter be used for codegen.
6353   Expr *buildCounterStep() const;
6354   /// Build loop data with counter value for depend clauses in ordered
6355   /// directives.
6356   Expr *
6357   buildOrderedLoopData(Scope *S, Expr *Counter,
6358                        llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
6359                        SourceLocation Loc, Expr *Inc = nullptr,
6360                        OverloadedOperatorKind OOK = OO_Amp);
6361   /// Builds the minimum value for the loop counter.
6362   std::pair<Expr *, Expr *> buildMinMaxValues(
6363       Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
6364   /// Builds final condition for the non-rectangular loops.
6365   Expr *buildFinalCondition(Scope *S) const;
6366   /// Return true if any expression is dependent.
6367   bool dependent() const;
6368   /// Returns true if the initializer forms non-rectangular loop.
6369   bool doesInitDependOnLC() const { return InitDependOnLC.hasValue(); }
6370   /// Returns true if the condition forms non-rectangular loop.
6371   bool doesCondDependOnLC() const { return CondDependOnLC.hasValue(); }
6372   /// Returns index of the loop we depend on (starting from 1), or 0 otherwise.
6373   unsigned getLoopDependentIdx() const {
6374     return InitDependOnLC.getValueOr(CondDependOnLC.getValueOr(0));
6375   }
6376 
6377 private:
6378   /// Check the right-hand side of an assignment in the increment
6379   /// expression.
6380   bool checkAndSetIncRHS(Expr *RHS);
6381   /// Helper to set loop counter variable and its initializer.
6382   bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB,
6383                       bool EmitDiags);
6384   /// Helper to set upper bound.
6385   bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp,
6386              SourceRange SR, SourceLocation SL);
6387   /// Helper to set loop increment.
6388   bool setStep(Expr *NewStep, bool Subtract);
6389 };
6390 
6391 bool OpenMPIterationSpaceChecker::dependent() const {
6392   if (!LCDecl) {
6393     assert(!LB && !UB && !Step);
6394     return false;
6395   }
6396   return LCDecl->getType()->isDependentType() ||
6397          (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
6398          (Step && Step->isValueDependent());
6399 }
6400 
6401 bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
6402                                                  Expr *NewLCRefExpr,
6403                                                  Expr *NewLB, bool EmitDiags) {
6404   // State consistency checking to ensure correct usage.
6405   assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
6406          UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
6407   if (!NewLCDecl || !NewLB)
6408     return true;
6409   LCDecl = getCanonicalDecl(NewLCDecl);
6410   LCRef = NewLCRefExpr;
6411   if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
6412     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
6413       if ((Ctor->isCopyOrMoveConstructor() ||
6414            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
6415           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
6416         NewLB = CE->getArg(0)->IgnoreParenImpCasts();
6417   LB = NewLB;
6418   if (EmitDiags)
6419     InitDependOnLC = doesDependOnLoopCounter(LB, /*IsInitializer=*/true);
6420   return false;
6421 }
6422 
6423 bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB,
6424                                         llvm::Optional<bool> LessOp,
6425                                         bool StrictOp, SourceRange SR,
6426                                         SourceLocation SL) {
6427   // State consistency checking to ensure correct usage.
6428   assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
6429          Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
6430   if (!NewUB)
6431     return true;
6432   UB = NewUB;
6433   if (LessOp)
6434     TestIsLessOp = LessOp;
6435   TestIsStrictOp = StrictOp;
6436   ConditionSrcRange = SR;
6437   ConditionLoc = SL;
6438   CondDependOnLC = doesDependOnLoopCounter(UB, /*IsInitializer=*/false);
6439   return false;
6440 }
6441 
6442 bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
6443   // State consistency checking to ensure correct usage.
6444   assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
6445   if (!NewStep)
6446     return true;
6447   if (!NewStep->isValueDependent()) {
6448     // Check that the step is integer expression.
6449     SourceLocation StepLoc = NewStep->getBeginLoc();
6450     ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion(
6451         StepLoc, getExprAsWritten(NewStep));
6452     if (Val.isInvalid())
6453       return true;
6454     NewStep = Val.get();
6455 
6456     // OpenMP [2.6, Canonical Loop Form, Restrictions]
6457     //  If test-expr is of form var relational-op b and relational-op is < or
6458     //  <= then incr-expr must cause var to increase on each iteration of the
6459     //  loop. If test-expr is of form var relational-op b and relational-op is
6460     //  > or >= then incr-expr must cause var to decrease on each iteration of
6461     //  the loop.
6462     //  If test-expr is of form b relational-op var and relational-op is < or
6463     //  <= then incr-expr must cause var to decrease on each iteration of the
6464     //  loop. If test-expr is of form b relational-op var and relational-op is
6465     //  > or >= then incr-expr must cause var to increase on each iteration of
6466     //  the loop.
6467     llvm::APSInt Result;
6468     bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
6469     bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
6470     bool IsConstNeg =
6471         IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
6472     bool IsConstPos =
6473         IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
6474     bool IsConstZero = IsConstant && !Result.getBoolValue();
6475 
6476     // != with increment is treated as <; != with decrement is treated as >
6477     if (!TestIsLessOp.hasValue())
6478       TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract);
6479     if (UB && (IsConstZero ||
6480                (TestIsLessOp.getValue() ?
6481                   (IsConstNeg || (IsUnsigned && Subtract)) :
6482                   (IsConstPos || (IsUnsigned && !Subtract))))) {
6483       SemaRef.Diag(NewStep->getExprLoc(),
6484                    diag::err_omp_loop_incr_not_compatible)
6485           << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange();
6486       SemaRef.Diag(ConditionLoc,
6487                    diag::note_omp_loop_cond_requres_compatible_incr)
6488           << TestIsLessOp.getValue() << ConditionSrcRange;
6489       return true;
6490     }
6491     if (TestIsLessOp.getValue() == Subtract) {
6492       NewStep =
6493           SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
6494               .get();
6495       Subtract = !Subtract;
6496     }
6497   }
6498 
6499   Step = NewStep;
6500   SubtractStep = Subtract;
6501   return false;
6502 }
6503 
6504 namespace {
6505 /// Checker for the non-rectangular loops. Checks if the initializer or
6506 /// condition expression references loop counter variable.
6507 class LoopCounterRefChecker final
6508     : public ConstStmtVisitor<LoopCounterRefChecker, bool> {
6509   Sema &SemaRef;
6510   DSAStackTy &Stack;
6511   const ValueDecl *CurLCDecl = nullptr;
6512   const ValueDecl *DepDecl = nullptr;
6513   const ValueDecl *PrevDepDecl = nullptr;
6514   bool IsInitializer = true;
6515   unsigned BaseLoopId = 0;
6516   bool checkDecl(const Expr *E, const ValueDecl *VD) {
6517     if (getCanonicalDecl(VD) == getCanonicalDecl(CurLCDecl)) {
6518       SemaRef.Diag(E->getExprLoc(), diag::err_omp_stmt_depends_on_loop_counter)
6519           << (IsInitializer ? 0 : 1);
6520       return false;
6521     }
6522     const auto &&Data = Stack.isLoopControlVariable(VD);
6523     // OpenMP, 2.9.1 Canonical Loop Form, Restrictions.
6524     // The type of the loop iterator on which we depend may not have a random
6525     // access iterator type.
6526     if (Data.first && VD->getType()->isRecordType()) {
6527       SmallString<128> Name;
6528       llvm::raw_svector_ostream OS(Name);
6529       VD->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(),
6530                                /*Qualified=*/true);
6531       SemaRef.Diag(E->getExprLoc(),
6532                    diag::err_omp_wrong_dependency_iterator_type)
6533           << OS.str();
6534       SemaRef.Diag(VD->getLocation(), diag::note_previous_decl) << VD;
6535       return false;
6536     }
6537     if (Data.first &&
6538         (DepDecl || (PrevDepDecl &&
6539                      getCanonicalDecl(VD) != getCanonicalDecl(PrevDepDecl)))) {
6540       if (!DepDecl && PrevDepDecl)
6541         DepDecl = PrevDepDecl;
6542       SmallString<128> Name;
6543       llvm::raw_svector_ostream OS(Name);
6544       DepDecl->getNameForDiagnostic(OS, SemaRef.getPrintingPolicy(),
6545                                     /*Qualified=*/true);
6546       SemaRef.Diag(E->getExprLoc(),
6547                    diag::err_omp_invariant_or_linear_dependency)
6548           << OS.str();
6549       return false;
6550     }
6551     if (Data.first) {
6552       DepDecl = VD;
6553       BaseLoopId = Data.first;
6554     }
6555     return Data.first;
6556   }
6557 
6558 public:
6559   bool VisitDeclRefExpr(const DeclRefExpr *E) {
6560     const ValueDecl *VD = E->getDecl();
6561     if (isa<VarDecl>(VD))
6562       return checkDecl(E, VD);
6563     return false;
6564   }
6565   bool VisitMemberExpr(const MemberExpr *E) {
6566     if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
6567       const ValueDecl *VD = E->getMemberDecl();
6568       if (isa<VarDecl>(VD) || isa<FieldDecl>(VD))
6569         return checkDecl(E, VD);
6570     }
6571     return false;
6572   }
6573   bool VisitStmt(const Stmt *S) {
6574     bool Res = false;
6575     for (const Stmt *Child : S->children())
6576       Res = (Child && Visit(Child)) || Res;
6577     return Res;
6578   }
6579   explicit LoopCounterRefChecker(Sema &SemaRef, DSAStackTy &Stack,
6580                                  const ValueDecl *CurLCDecl, bool IsInitializer,
6581                                  const ValueDecl *PrevDepDecl = nullptr)
6582       : SemaRef(SemaRef), Stack(Stack), CurLCDecl(CurLCDecl),
6583         PrevDepDecl(PrevDepDecl), IsInitializer(IsInitializer) {}
6584   unsigned getBaseLoopId() const {
6585     assert(CurLCDecl && "Expected loop dependency.");
6586     return BaseLoopId;
6587   }
6588   const ValueDecl *getDepDecl() const {
6589     assert(CurLCDecl && "Expected loop dependency.");
6590     return DepDecl;
6591   }
6592 };
6593 } // namespace
6594 
6595 Optional<unsigned>
6596 OpenMPIterationSpaceChecker::doesDependOnLoopCounter(const Stmt *S,
6597                                                      bool IsInitializer) {
6598   // Check for the non-rectangular loops.
6599   LoopCounterRefChecker LoopStmtChecker(SemaRef, Stack, LCDecl, IsInitializer,
6600                                         DepDecl);
6601   if (LoopStmtChecker.Visit(S)) {
6602     DepDecl = LoopStmtChecker.getDepDecl();
6603     return LoopStmtChecker.getBaseLoopId();
6604   }
6605   return llvm::None;
6606 }
6607 
6608 bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
6609   // Check init-expr for canonical loop form and save loop counter
6610   // variable - #Var and its initialization value - #LB.
6611   // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
6612   //   var = lb
6613   //   integer-type var = lb
6614   //   random-access-iterator-type var = lb
6615   //   pointer-type var = lb
6616   //
6617   if (!S) {
6618     if (EmitDiags) {
6619       SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
6620     }
6621     return true;
6622   }
6623   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
6624     if (!ExprTemp->cleanupsHaveSideEffects())
6625       S = ExprTemp->getSubExpr();
6626 
6627   InitSrcRange = S->getSourceRange();
6628   if (Expr *E = dyn_cast<Expr>(S))
6629     S = E->IgnoreParens();
6630   if (auto *BO = dyn_cast<BinaryOperator>(S)) {
6631     if (BO->getOpcode() == BO_Assign) {
6632       Expr *LHS = BO->getLHS()->IgnoreParens();
6633       if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
6634         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
6635           if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
6636             return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
6637                                   EmitDiags);
6638         return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS(), EmitDiags);
6639       }
6640       if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
6641         if (ME->isArrow() &&
6642             isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
6643           return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
6644                                 EmitDiags);
6645       }
6646     }
6647   } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
6648     if (DS->isSingleDecl()) {
6649       if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
6650         if (Var->hasInit() && !Var->getType()->isReferenceType()) {
6651           // Accept non-canonical init form here but emit ext. warning.
6652           if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
6653             SemaRef.Diag(S->getBeginLoc(),
6654                          diag::ext_omp_loop_not_canonical_init)
6655                 << S->getSourceRange();
6656           return setLCDeclAndLB(
6657               Var,
6658               buildDeclRefExpr(SemaRef, Var,
6659                                Var->getType().getNonReferenceType(),
6660                                DS->getBeginLoc()),
6661               Var->getInit(), EmitDiags);
6662         }
6663       }
6664     }
6665   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
6666     if (CE->getOperator() == OO_Equal) {
6667       Expr *LHS = CE->getArg(0);
6668       if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
6669         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
6670           if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
6671             return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
6672                                   EmitDiags);
6673         return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1), EmitDiags);
6674       }
6675       if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
6676         if (ME->isArrow() &&
6677             isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
6678           return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS(),
6679                                 EmitDiags);
6680       }
6681     }
6682   }
6683 
6684   if (dependent() || SemaRef.CurContext->isDependentContext())
6685     return false;
6686   if (EmitDiags) {
6687     SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init)
6688         << S->getSourceRange();
6689   }
6690   return true;
6691 }
6692 
6693 /// Ignore parenthesizes, implicit casts, copy constructor and return the
6694 /// variable (which may be the loop variable) if possible.
6695 static const ValueDecl *getInitLCDecl(const Expr *E) {
6696   if (!E)
6697     return nullptr;
6698   E = getExprAsWritten(E);
6699   if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
6700     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
6701       if ((Ctor->isCopyOrMoveConstructor() ||
6702            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
6703           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
6704         E = CE->getArg(0)->IgnoreParenImpCasts();
6705   if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
6706     if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
6707       return getCanonicalDecl(VD);
6708   }
6709   if (const auto *ME = dyn_cast_or_null<MemberExpr>(E))
6710     if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
6711       return getCanonicalDecl(ME->getMemberDecl());
6712   return nullptr;
6713 }
6714 
6715 bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
6716   // Check test-expr for canonical form, save upper-bound UB, flags for
6717   // less/greater and for strict/non-strict comparison.
6718   // OpenMP [2.9] Canonical loop form. Test-expr may be one of the following:
6719   //   var relational-op b
6720   //   b relational-op var
6721   //
6722   bool IneqCondIsCanonical = SemaRef.getLangOpts().OpenMP >= 50;
6723   if (!S) {
6724     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond)
6725         << (IneqCondIsCanonical ? 1 : 0) << LCDecl;
6726     return true;
6727   }
6728   Condition = S;
6729   S = getExprAsWritten(S);
6730   SourceLocation CondLoc = S->getBeginLoc();
6731   if (auto *BO = dyn_cast<BinaryOperator>(S)) {
6732     if (BO->isRelationalOp()) {
6733       if (getInitLCDecl(BO->getLHS()) == LCDecl)
6734         return setUB(BO->getRHS(),
6735                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
6736                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
6737                      BO->getSourceRange(), BO->getOperatorLoc());
6738       if (getInitLCDecl(BO->getRHS()) == LCDecl)
6739         return setUB(BO->getLHS(),
6740                      (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
6741                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
6742                      BO->getSourceRange(), BO->getOperatorLoc());
6743     } else if (IneqCondIsCanonical && BO->getOpcode() == BO_NE)
6744       return setUB(
6745           getInitLCDecl(BO->getLHS()) == LCDecl ? BO->getRHS() : BO->getLHS(),
6746           /*LessOp=*/llvm::None,
6747           /*StrictOp=*/true, BO->getSourceRange(), BO->getOperatorLoc());
6748   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
6749     if (CE->getNumArgs() == 2) {
6750       auto Op = CE->getOperator();
6751       switch (Op) {
6752       case OO_Greater:
6753       case OO_GreaterEqual:
6754       case OO_Less:
6755       case OO_LessEqual:
6756         if (getInitLCDecl(CE->getArg(0)) == LCDecl)
6757           return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
6758                        Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
6759                        CE->getOperatorLoc());
6760         if (getInitLCDecl(CE->getArg(1)) == LCDecl)
6761           return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
6762                        Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
6763                        CE->getOperatorLoc());
6764         break;
6765       case OO_ExclaimEqual:
6766         if (IneqCondIsCanonical)
6767           return setUB(getInitLCDecl(CE->getArg(0)) == LCDecl ? CE->getArg(1)
6768                                                               : CE->getArg(0),
6769                        /*LessOp=*/llvm::None,
6770                        /*StrictOp=*/true, CE->getSourceRange(),
6771                        CE->getOperatorLoc());
6772         break;
6773       default:
6774         break;
6775       }
6776     }
6777   }
6778   if (dependent() || SemaRef.CurContext->isDependentContext())
6779     return false;
6780   SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
6781       << (IneqCondIsCanonical ? 1 : 0) << S->getSourceRange() << LCDecl;
6782   return true;
6783 }
6784 
6785 bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
6786   // RHS of canonical loop form increment can be:
6787   //   var + incr
6788   //   incr + var
6789   //   var - incr
6790   //
6791   RHS = RHS->IgnoreParenImpCasts();
6792   if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
6793     if (BO->isAdditiveOp()) {
6794       bool IsAdd = BO->getOpcode() == BO_Add;
6795       if (getInitLCDecl(BO->getLHS()) == LCDecl)
6796         return setStep(BO->getRHS(), !IsAdd);
6797       if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl)
6798         return setStep(BO->getLHS(), /*Subtract=*/false);
6799     }
6800   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
6801     bool IsAdd = CE->getOperator() == OO_Plus;
6802     if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
6803       if (getInitLCDecl(CE->getArg(0)) == LCDecl)
6804         return setStep(CE->getArg(1), !IsAdd);
6805       if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl)
6806         return setStep(CE->getArg(0), /*Subtract=*/false);
6807     }
6808   }
6809   if (dependent() || SemaRef.CurContext->isDependentContext())
6810     return false;
6811   SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
6812       << RHS->getSourceRange() << LCDecl;
6813   return true;
6814 }
6815 
6816 bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
6817   // Check incr-expr for canonical loop form and return true if it
6818   // does not conform.
6819   // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
6820   //   ++var
6821   //   var++
6822   //   --var
6823   //   var--
6824   //   var += incr
6825   //   var -= incr
6826   //   var = var + incr
6827   //   var = incr + var
6828   //   var = var - incr
6829   //
6830   if (!S) {
6831     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
6832     return true;
6833   }
6834   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
6835     if (!ExprTemp->cleanupsHaveSideEffects())
6836       S = ExprTemp->getSubExpr();
6837 
6838   IncrementSrcRange = S->getSourceRange();
6839   S = S->IgnoreParens();
6840   if (auto *UO = dyn_cast<UnaryOperator>(S)) {
6841     if (UO->isIncrementDecrementOp() &&
6842         getInitLCDecl(UO->getSubExpr()) == LCDecl)
6843       return setStep(SemaRef
6844                          .ActOnIntegerConstant(UO->getBeginLoc(),
6845                                                (UO->isDecrementOp() ? -1 : 1))
6846                          .get(),
6847                      /*Subtract=*/false);
6848   } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
6849     switch (BO->getOpcode()) {
6850     case BO_AddAssign:
6851     case BO_SubAssign:
6852       if (getInitLCDecl(BO->getLHS()) == LCDecl)
6853         return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
6854       break;
6855     case BO_Assign:
6856       if (getInitLCDecl(BO->getLHS()) == LCDecl)
6857         return checkAndSetIncRHS(BO->getRHS());
6858       break;
6859     default:
6860       break;
6861     }
6862   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
6863     switch (CE->getOperator()) {
6864     case OO_PlusPlus:
6865     case OO_MinusMinus:
6866       if (getInitLCDecl(CE->getArg(0)) == LCDecl)
6867         return setStep(SemaRef
6868                            .ActOnIntegerConstant(
6869                                CE->getBeginLoc(),
6870                                ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
6871                            .get(),
6872                        /*Subtract=*/false);
6873       break;
6874     case OO_PlusEqual:
6875     case OO_MinusEqual:
6876       if (getInitLCDecl(CE->getArg(0)) == LCDecl)
6877         return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
6878       break;
6879     case OO_Equal:
6880       if (getInitLCDecl(CE->getArg(0)) == LCDecl)
6881         return checkAndSetIncRHS(CE->getArg(1));
6882       break;
6883     default:
6884       break;
6885     }
6886   }
6887   if (dependent() || SemaRef.CurContext->isDependentContext())
6888     return false;
6889   SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
6890       << S->getSourceRange() << LCDecl;
6891   return true;
6892 }
6893 
6894 static ExprResult
6895 tryBuildCapture(Sema &SemaRef, Expr *Capture,
6896                 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
6897   if (SemaRef.CurContext->isDependentContext())
6898     return ExprResult(Capture);
6899   if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
6900     return SemaRef.PerformImplicitConversion(
6901         Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
6902         /*AllowExplicit=*/true);
6903   auto I = Captures.find(Capture);
6904   if (I != Captures.end())
6905     return buildCapture(SemaRef, Capture, I->second);
6906   DeclRefExpr *Ref = nullptr;
6907   ExprResult Res = buildCapture(SemaRef, Capture, Ref);
6908   Captures[Capture] = Ref;
6909   return Res;
6910 }
6911 
6912 /// Build the expression to calculate the number of iterations.
6913 Expr *OpenMPIterationSpaceChecker::buildNumIterations(
6914     Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType,
6915     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
6916   ExprResult Diff;
6917   QualType VarType = LCDecl->getType().getNonReferenceType();
6918   if (VarType->isIntegerType() || VarType->isPointerType() ||
6919       SemaRef.getLangOpts().CPlusPlus) {
6920     Expr *LBVal = LB;
6921     Expr *UBVal = UB;
6922     // LB = TestIsLessOp.getValue() ? min(LB(MinVal), LB(MaxVal)) :
6923     // max(LB(MinVal), LB(MaxVal))
6924     if (InitDependOnLC) {
6925       const LoopIterationSpace &IS =
6926           ResultIterSpaces[ResultIterSpaces.size() - 1 -
6927                            InitDependOnLC.getValueOr(
6928                                CondDependOnLC.getValueOr(0))];
6929       if (!IS.MinValue || !IS.MaxValue)
6930         return nullptr;
6931       // OuterVar = Min
6932       ExprResult MinValue =
6933           SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue);
6934       if (!MinValue.isUsable())
6935         return nullptr;
6936 
6937       ExprResult LBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
6938                                                IS.CounterVar, MinValue.get());
6939       if (!LBMinVal.isUsable())
6940         return nullptr;
6941       // OuterVar = Min, LBVal
6942       LBMinVal =
6943           SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMinVal.get(), LBVal);
6944       if (!LBMinVal.isUsable())
6945         return nullptr;
6946       // (OuterVar = Min, LBVal)
6947       LBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMinVal.get());
6948       if (!LBMinVal.isUsable())
6949         return nullptr;
6950 
6951       // OuterVar = Max
6952       ExprResult MaxValue =
6953           SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue);
6954       if (!MaxValue.isUsable())
6955         return nullptr;
6956 
6957       ExprResult LBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
6958                                                IS.CounterVar, MaxValue.get());
6959       if (!LBMaxVal.isUsable())
6960         return nullptr;
6961       // OuterVar = Max, LBVal
6962       LBMaxVal =
6963           SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, LBMaxVal.get(), LBVal);
6964       if (!LBMaxVal.isUsable())
6965         return nullptr;
6966       // (OuterVar = Max, LBVal)
6967       LBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, LBMaxVal.get());
6968       if (!LBMaxVal.isUsable())
6969         return nullptr;
6970 
6971       Expr *LBMin = tryBuildCapture(SemaRef, LBMinVal.get(), Captures).get();
6972       Expr *LBMax = tryBuildCapture(SemaRef, LBMaxVal.get(), Captures).get();
6973       if (!LBMin || !LBMax)
6974         return nullptr;
6975       // LB(MinVal) < LB(MaxVal)
6976       ExprResult MinLessMaxRes =
6977           SemaRef.BuildBinOp(S, DefaultLoc, BO_LT, LBMin, LBMax);
6978       if (!MinLessMaxRes.isUsable())
6979         return nullptr;
6980       Expr *MinLessMax =
6981           tryBuildCapture(SemaRef, MinLessMaxRes.get(), Captures).get();
6982       if (!MinLessMax)
6983         return nullptr;
6984       if (TestIsLessOp.getValue()) {
6985         // LB(MinVal) < LB(MaxVal) ? LB(MinVal) : LB(MaxVal) - min(LB(MinVal),
6986         // LB(MaxVal))
6987         ExprResult MinLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc,
6988                                                       MinLessMax, LBMin, LBMax);
6989         if (!MinLB.isUsable())
6990           return nullptr;
6991         LBVal = MinLB.get();
6992       } else {
6993         // LB(MinVal) < LB(MaxVal) ? LB(MaxVal) : LB(MinVal) - max(LB(MinVal),
6994         // LB(MaxVal))
6995         ExprResult MaxLB = SemaRef.ActOnConditionalOp(DefaultLoc, DefaultLoc,
6996                                                       MinLessMax, LBMax, LBMin);
6997         if (!MaxLB.isUsable())
6998           return nullptr;
6999         LBVal = MaxLB.get();
7000       }
7001     }
7002     // UB = TestIsLessOp.getValue() ? max(UB(MinVal), UB(MaxVal)) :
7003     // min(UB(MinVal), UB(MaxVal))
7004     if (CondDependOnLC) {
7005       const LoopIterationSpace &IS =
7006           ResultIterSpaces[ResultIterSpaces.size() - 1 -
7007                            InitDependOnLC.getValueOr(
7008                                CondDependOnLC.getValueOr(0))];
7009       if (!IS.MinValue || !IS.MaxValue)
7010         return nullptr;
7011       // OuterVar = Min
7012       ExprResult MinValue =
7013           SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MinValue);
7014       if (!MinValue.isUsable())
7015         return nullptr;
7016 
7017       ExprResult UBMinVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
7018                                                IS.CounterVar, MinValue.get());
7019       if (!UBMinVal.isUsable())
7020         return nullptr;
7021       // OuterVar = Min, UBVal
7022       UBMinVal =
7023           SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMinVal.get(), UBVal);
7024       if (!UBMinVal.isUsable())
7025         return nullptr;
7026       // (OuterVar = Min, UBVal)
7027       UBMinVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMinVal.get());
7028       if (!UBMinVal.isUsable())
7029         return nullptr;
7030 
7031       // OuterVar = Max
7032       ExprResult MaxValue =
7033           SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, IS.MaxValue);
7034       if (!MaxValue.isUsable())
7035         return nullptr;
7036 
7037       ExprResult UBMaxVal = SemaRef.BuildBinOp(S, DefaultLoc, BO_Assign,
7038                                                IS.CounterVar, MaxValue.get());
7039       if (!UBMaxVal.isUsable())
7040         return nullptr;
7041       // OuterVar = Max, UBVal
7042       UBMaxVal =
7043           SemaRef.BuildBinOp(S, DefaultLoc, BO_Comma, UBMaxVal.get(), UBVal);
7044       if (!UBMaxVal.isUsable())
7045         return nullptr;
7046       // (OuterVar = Max, UBVal)
7047       UBMaxVal = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, UBMaxVal.get());
7048       if (!UBMaxVal.isUsable())
7049         return nullptr;
7050 
7051       Expr *UBMin = tryBuildCapture(SemaRef, UBMinVal.get(), Captures).get();
7052       Expr *UBMax = tryBuildCapture(SemaRef, UBMaxVal.get(), Captures).get();
7053       if (!UBMin || !UBMax)
7054         return nullptr;
7055       // UB(MinVal) > UB(MaxVal)
7056       ExprResult MinGreaterMaxRes =
7057           SemaRef.BuildBinOp(S, DefaultLoc, BO_GT, UBMin, UBMax);
7058       if (!MinGreaterMaxRes.isUsable())
7059         return nullptr;
7060       Expr *MinGreaterMax =
7061           tryBuildCapture(SemaRef, MinGreaterMaxRes.get(), Captures).get();
7062       if (!MinGreaterMax)
7063         return nullptr;
7064       if (TestIsLessOp.getValue()) {
7065         // UB(MinVal) > UB(MaxVal) ? UB(MinVal) : UB(MaxVal) - max(UB(MinVal),
7066         // UB(MaxVal))
7067         ExprResult MaxUB = SemaRef.ActOnConditionalOp(
7068             DefaultLoc, DefaultLoc, MinGreaterMax, UBMin, UBMax);
7069         if (!MaxUB.isUsable())
7070           return nullptr;
7071         UBVal = MaxUB.get();
7072       } else {
7073         // UB(MinVal) > UB(MaxVal) ? UB(MaxVal) : UB(MinVal) - min(UB(MinVal),
7074         // UB(MaxVal))
7075         ExprResult MinUB = SemaRef.ActOnConditionalOp(
7076             DefaultLoc, DefaultLoc, MinGreaterMax, UBMax, UBMin);
7077         if (!MinUB.isUsable())
7078           return nullptr;
7079         UBVal = MinUB.get();
7080       }
7081     }
7082     // Upper - Lower
7083     Expr *UBExpr = TestIsLessOp.getValue() ? UBVal : LBVal;
7084     Expr *LBExpr = TestIsLessOp.getValue() ? LBVal : UBVal;
7085     Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
7086     Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
7087     if (!Upper || !Lower)
7088       return nullptr;
7089 
7090     Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
7091 
7092     if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
7093       // BuildBinOp already emitted error, this one is to point user to upper
7094       // and lower bound, and to tell what is passed to 'operator-'.
7095       SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
7096           << Upper->getSourceRange() << Lower->getSourceRange();
7097       return nullptr;
7098     }
7099   }
7100 
7101   if (!Diff.isUsable())
7102     return nullptr;
7103 
7104   // Upper - Lower [- 1]
7105   if (TestIsStrictOp)
7106     Diff = SemaRef.BuildBinOp(
7107         S, DefaultLoc, BO_Sub, Diff.get(),
7108         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
7109   if (!Diff.isUsable())
7110     return nullptr;
7111 
7112   // Upper - Lower [- 1] + Step
7113   ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
7114   if (!NewStep.isUsable())
7115     return nullptr;
7116   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
7117   if (!Diff.isUsable())
7118     return nullptr;
7119 
7120   // Parentheses (for dumping/debugging purposes only).
7121   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
7122   if (!Diff.isUsable())
7123     return nullptr;
7124 
7125   // (Upper - Lower [- 1] + Step) / Step
7126   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
7127   if (!Diff.isUsable())
7128     return nullptr;
7129 
7130   // OpenMP runtime requires 32-bit or 64-bit loop variables.
7131   QualType Type = Diff.get()->getType();
7132   ASTContext &C = SemaRef.Context;
7133   bool UseVarType = VarType->hasIntegerRepresentation() &&
7134                     C.getTypeSize(Type) > C.getTypeSize(VarType);
7135   if (!Type->isIntegerType() || UseVarType) {
7136     unsigned NewSize =
7137         UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
7138     bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
7139                                : Type->hasSignedIntegerRepresentation();
7140     Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
7141     if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
7142       Diff = SemaRef.PerformImplicitConversion(
7143           Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
7144       if (!Diff.isUsable())
7145         return nullptr;
7146     }
7147   }
7148   if (LimitedType) {
7149     unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
7150     if (NewSize != C.getTypeSize(Type)) {
7151       if (NewSize < C.getTypeSize(Type)) {
7152         assert(NewSize == 64 && "incorrect loop var size");
7153         SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
7154             << InitSrcRange << ConditionSrcRange;
7155       }
7156       QualType NewType = C.getIntTypeForBitwidth(
7157           NewSize, Type->hasSignedIntegerRepresentation() ||
7158                        C.getTypeSize(Type) < NewSize);
7159       if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
7160         Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
7161                                                  Sema::AA_Converting, true);
7162         if (!Diff.isUsable())
7163           return nullptr;
7164       }
7165     }
7166   }
7167 
7168   return Diff.get();
7169 }
7170 
7171 std::pair<Expr *, Expr *> OpenMPIterationSpaceChecker::buildMinMaxValues(
7172     Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
7173   // Do not build for iterators, they cannot be used in non-rectangular loop
7174   // nests.
7175   if (LCDecl->getType()->isRecordType())
7176     return std::make_pair(nullptr, nullptr);
7177   // If we subtract, the min is in the condition, otherwise the min is in the
7178   // init value.
7179   Expr *MinExpr = nullptr;
7180   Expr *MaxExpr = nullptr;
7181   Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB;
7182   Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB;
7183   bool LBNonRect = TestIsLessOp.getValue() ? InitDependOnLC.hasValue()
7184                                            : CondDependOnLC.hasValue();
7185   bool UBNonRect = TestIsLessOp.getValue() ? CondDependOnLC.hasValue()
7186                                            : InitDependOnLC.hasValue();
7187   Expr *Lower =
7188       LBNonRect ? LBExpr : tryBuildCapture(SemaRef, LBExpr, Captures).get();
7189   Expr *Upper =
7190       UBNonRect ? UBExpr : tryBuildCapture(SemaRef, UBExpr, Captures).get();
7191   if (!Upper || !Lower)
7192     return std::make_pair(nullptr, nullptr);
7193 
7194   if (TestIsLessOp.getValue())
7195     MinExpr = Lower;
7196   else
7197     MaxExpr = Upper;
7198 
7199   // Build minimum/maximum value based on number of iterations.
7200   ExprResult Diff;
7201   QualType VarType = LCDecl->getType().getNonReferenceType();
7202 
7203   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
7204   if (!Diff.isUsable())
7205     return std::make_pair(nullptr, nullptr);
7206 
7207   // Upper - Lower [- 1]
7208   if (TestIsStrictOp)
7209     Diff = SemaRef.BuildBinOp(
7210         S, DefaultLoc, BO_Sub, Diff.get(),
7211         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
7212   if (!Diff.isUsable())
7213     return std::make_pair(nullptr, nullptr);
7214 
7215   // Upper - Lower [- 1] + Step
7216   ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
7217   if (!NewStep.isUsable())
7218     return std::make_pair(nullptr, nullptr);
7219 
7220   // Parentheses (for dumping/debugging purposes only).
7221   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
7222   if (!Diff.isUsable())
7223     return std::make_pair(nullptr, nullptr);
7224 
7225   // (Upper - Lower [- 1]) / Step
7226   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
7227   if (!Diff.isUsable())
7228     return std::make_pair(nullptr, nullptr);
7229 
7230   // ((Upper - Lower [- 1]) / Step) * Step
7231   // Parentheses (for dumping/debugging purposes only).
7232   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
7233   if (!Diff.isUsable())
7234     return std::make_pair(nullptr, nullptr);
7235 
7236   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Mul, Diff.get(), NewStep.get());
7237   if (!Diff.isUsable())
7238     return std::make_pair(nullptr, nullptr);
7239 
7240   // Convert to the original type or ptrdiff_t, if original type is pointer.
7241   if (!VarType->isAnyPointerType() &&
7242       !SemaRef.Context.hasSameType(Diff.get()->getType(), VarType)) {
7243     Diff = SemaRef.PerformImplicitConversion(
7244         Diff.get(), VarType, Sema::AA_Converting, /*AllowExplicit=*/true);
7245   } else if (VarType->isAnyPointerType() &&
7246              !SemaRef.Context.hasSameType(
7247                  Diff.get()->getType(),
7248                  SemaRef.Context.getUnsignedPointerDiffType())) {
7249     Diff = SemaRef.PerformImplicitConversion(
7250         Diff.get(), SemaRef.Context.getUnsignedPointerDiffType(),
7251         Sema::AA_Converting, /*AllowExplicit=*/true);
7252   }
7253   if (!Diff.isUsable())
7254     return std::make_pair(nullptr, nullptr);
7255 
7256   // Parentheses (for dumping/debugging purposes only).
7257   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
7258   if (!Diff.isUsable())
7259     return std::make_pair(nullptr, nullptr);
7260 
7261   if (TestIsLessOp.getValue()) {
7262     // MinExpr = Lower;
7263     // MaxExpr = Lower + (((Upper - Lower [- 1]) / Step) * Step)
7264     Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Lower, Diff.get());
7265     if (!Diff.isUsable())
7266       return std::make_pair(nullptr, nullptr);
7267     Diff = SemaRef.ActOnFinishFullExpr(Diff.get(), /*DiscardedValue*/ false);
7268     if (!Diff.isUsable())
7269       return std::make_pair(nullptr, nullptr);
7270     MaxExpr = Diff.get();
7271   } else {
7272     // MaxExpr = Upper;
7273     // MinExpr = Upper - (((Upper - Lower [- 1]) / Step) * Step)
7274     Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Diff.get());
7275     if (!Diff.isUsable())
7276       return std::make_pair(nullptr, nullptr);
7277     Diff = SemaRef.ActOnFinishFullExpr(Diff.get(), /*DiscardedValue*/ false);
7278     if (!Diff.isUsable())
7279       return std::make_pair(nullptr, nullptr);
7280     MinExpr = Diff.get();
7281   }
7282 
7283   return std::make_pair(MinExpr, MaxExpr);
7284 }
7285 
7286 Expr *OpenMPIterationSpaceChecker::buildFinalCondition(Scope *S) const {
7287   if (InitDependOnLC || CondDependOnLC)
7288     return Condition;
7289   return nullptr;
7290 }
7291 
7292 Expr *OpenMPIterationSpaceChecker::buildPreCond(
7293     Scope *S, Expr *Cond,
7294     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
7295   // Do not build a precondition when the condition/initialization is dependent
7296   // to prevent pessimistic early loop exit.
7297   // TODO: this can be improved by calculating min/max values but not sure that
7298   // it will be very effective.
7299   if (CondDependOnLC || InitDependOnLC)
7300     return SemaRef.PerformImplicitConversion(
7301         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get(),
7302         SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
7303         /*AllowExplicit=*/true).get();
7304 
7305   // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
7306   Sema::TentativeAnalysisScope Trap(SemaRef);
7307 
7308   ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures);
7309   ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures);
7310   if (!NewLB.isUsable() || !NewUB.isUsable())
7311     return nullptr;
7312 
7313   ExprResult CondExpr =
7314       SemaRef.BuildBinOp(S, DefaultLoc,
7315                          TestIsLessOp.getValue() ?
7316                            (TestIsStrictOp ? BO_LT : BO_LE) :
7317                            (TestIsStrictOp ? BO_GT : BO_GE),
7318                          NewLB.get(), NewUB.get());
7319   if (CondExpr.isUsable()) {
7320     if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
7321                                                 SemaRef.Context.BoolTy))
7322       CondExpr = SemaRef.PerformImplicitConversion(
7323           CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
7324           /*AllowExplicit=*/true);
7325   }
7326 
7327   // Otherwise use original loop condition and evaluate it in runtime.
7328   return CondExpr.isUsable() ? CondExpr.get() : Cond;
7329 }
7330 
7331 /// Build reference expression to the counter be used for codegen.
7332 DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
7333     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
7334     DSAStackTy &DSA) const {
7335   auto *VD = dyn_cast<VarDecl>(LCDecl);
7336   if (!VD) {
7337     VD = SemaRef.isOpenMPCapturedDecl(LCDecl);
7338     DeclRefExpr *Ref = buildDeclRefExpr(
7339         SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
7340     const DSAStackTy::DSAVarData Data =
7341         DSA.getTopDSA(LCDecl, /*FromParent=*/false);
7342     // If the loop control decl is explicitly marked as private, do not mark it
7343     // as captured again.
7344     if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
7345       Captures.insert(std::make_pair(LCRef, Ref));
7346     return Ref;
7347   }
7348   return cast<DeclRefExpr>(LCRef);
7349 }
7350 
7351 Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
7352   if (LCDecl && !LCDecl->isInvalidDecl()) {
7353     QualType Type = LCDecl->getType().getNonReferenceType();
7354     VarDecl *PrivateVar = buildVarDecl(
7355         SemaRef, DefaultLoc, Type, LCDecl->getName(),
7356         LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
7357         isa<VarDecl>(LCDecl)
7358             ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc)
7359             : nullptr);
7360     if (PrivateVar->isInvalidDecl())
7361       return nullptr;
7362     return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
7363   }
7364   return nullptr;
7365 }
7366 
7367 /// Build initialization of the counter to be used for codegen.
7368 Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
7369 
7370 /// Build step of the counter be used for codegen.
7371 Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
7372 
7373 Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
7374     Scope *S, Expr *Counter,
7375     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
7376     Expr *Inc, OverloadedOperatorKind OOK) {
7377   Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get();
7378   if (!Cnt)
7379     return nullptr;
7380   if (Inc) {
7381     assert((OOK == OO_Plus || OOK == OO_Minus) &&
7382            "Expected only + or - operations for depend clauses.");
7383     BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
7384     Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get();
7385     if (!Cnt)
7386       return nullptr;
7387   }
7388   ExprResult Diff;
7389   QualType VarType = LCDecl->getType().getNonReferenceType();
7390   if (VarType->isIntegerType() || VarType->isPointerType() ||
7391       SemaRef.getLangOpts().CPlusPlus) {
7392     // Upper - Lower
7393     Expr *Upper = TestIsLessOp.getValue()
7394                       ? Cnt
7395                       : tryBuildCapture(SemaRef, LB, Captures).get();
7396     Expr *Lower = TestIsLessOp.getValue()
7397                       ? tryBuildCapture(SemaRef, LB, Captures).get()
7398                       : Cnt;
7399     if (!Upper || !Lower)
7400       return nullptr;
7401 
7402     Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
7403 
7404     if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
7405       // BuildBinOp already emitted error, this one is to point user to upper
7406       // and lower bound, and to tell what is passed to 'operator-'.
7407       SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
7408           << Upper->getSourceRange() << Lower->getSourceRange();
7409       return nullptr;
7410     }
7411   }
7412 
7413   if (!Diff.isUsable())
7414     return nullptr;
7415 
7416   // Parentheses (for dumping/debugging purposes only).
7417   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
7418   if (!Diff.isUsable())
7419     return nullptr;
7420 
7421   ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
7422   if (!NewStep.isUsable())
7423     return nullptr;
7424   // (Upper - Lower) / Step
7425   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
7426   if (!Diff.isUsable())
7427     return nullptr;
7428 
7429   return Diff.get();
7430 }
7431 } // namespace
7432 
7433 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
7434   assert(getLangOpts().OpenMP && "OpenMP is not active.");
7435   assert(Init && "Expected loop in canonical form.");
7436   unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
7437   if (AssociatedLoops > 0 &&
7438       isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
7439     DSAStack->loopStart();
7440     OpenMPIterationSpaceChecker ISC(*this, *DSAStack, ForLoc);
7441     if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) {
7442       if (ValueDecl *D = ISC.getLoopDecl()) {
7443         auto *VD = dyn_cast<VarDecl>(D);
7444         DeclRefExpr *PrivateRef = nullptr;
7445         if (!VD) {
7446           if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
7447             VD = Private;
7448           } else {
7449             PrivateRef = buildCapture(*this, D, ISC.getLoopDeclRefExpr(),
7450                                       /*WithInit=*/false);
7451             VD = cast<VarDecl>(PrivateRef->getDecl());
7452           }
7453         }
7454         DSAStack->addLoopControlVariable(D, VD);
7455         const Decl *LD = DSAStack->getPossiblyLoopCunter();
7456         if (LD != D->getCanonicalDecl()) {
7457           DSAStack->resetPossibleLoopCounter();
7458           if (auto *Var = dyn_cast_or_null<VarDecl>(LD))
7459             MarkDeclarationsReferencedInExpr(
7460                 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var),
7461                                  Var->getType().getNonLValueExprType(Context),
7462                                  ForLoc, /*RefersToCapture=*/true));
7463         }
7464         OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
7465         // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables
7466         // Referenced in a Construct, C/C++]. The loop iteration variable in the
7467         // associated for-loop of a simd construct with just one associated
7468         // for-loop may be listed in a linear clause with a constant-linear-step
7469         // that is the increment of the associated for-loop. The loop iteration
7470         // variable(s) in the associated for-loop(s) of a for or parallel for
7471         // construct may be listed in a private or lastprivate clause.
7472         DSAStackTy::DSAVarData DVar =
7473             DSAStack->getTopDSA(D, /*FromParent=*/false);
7474         // If LoopVarRefExpr is nullptr it means the corresponding loop variable
7475         // is declared in the loop and it is predetermined as a private.
7476         Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
7477         OpenMPClauseKind PredeterminedCKind =
7478             isOpenMPSimdDirective(DKind)
7479                 ? (DSAStack->hasMutipleLoops() ? OMPC_lastprivate : OMPC_linear)
7480                 : OMPC_private;
7481         if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
7482               DVar.CKind != PredeterminedCKind && DVar.RefExpr &&
7483               (LangOpts.OpenMP <= 45 || (DVar.CKind != OMPC_lastprivate &&
7484                                          DVar.CKind != OMPC_private))) ||
7485              ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
7486                DKind == OMPD_master_taskloop ||
7487                DKind == OMPD_parallel_master_taskloop ||
7488                isOpenMPDistributeDirective(DKind)) &&
7489               !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
7490               DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
7491             (DVar.CKind != OMPC_private || DVar.RefExpr)) {
7492           Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa)
7493               << getOpenMPClauseName(DVar.CKind)
7494               << getOpenMPDirectiveName(DKind)
7495               << getOpenMPClauseName(PredeterminedCKind);
7496           if (DVar.RefExpr == nullptr)
7497             DVar.CKind = PredeterminedCKind;
7498           reportOriginalDsa(*this, DSAStack, D, DVar,
7499                             /*IsLoopIterVar=*/true);
7500         } else if (LoopDeclRefExpr) {
7501           // Make the loop iteration variable private (for worksharing
7502           // constructs), linear (for simd directives with the only one
7503           // associated loop) or lastprivate (for simd directives with several
7504           // collapsed or ordered loops).
7505           if (DVar.CKind == OMPC_unknown)
7506             DSAStack->addDSA(D, LoopDeclRefExpr, PredeterminedCKind,
7507                              PrivateRef);
7508         }
7509       }
7510     }
7511     DSAStack->setAssociatedLoops(AssociatedLoops - 1);
7512   }
7513 }
7514 
7515 /// Called on a for stmt to check and extract its iteration space
7516 /// for further processing (such as collapsing).
7517 static bool checkOpenMPIterationSpace(
7518     OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
7519     unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
7520     unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
7521     Expr *OrderedLoopCountExpr,
7522     Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
7523     llvm::MutableArrayRef<LoopIterationSpace> ResultIterSpaces,
7524     llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
7525   // OpenMP [2.9.1, Canonical Loop Form]
7526   //   for (init-expr; test-expr; incr-expr) structured-block
7527   //   for (range-decl: range-expr) structured-block
7528   auto *For = dyn_cast_or_null<ForStmt>(S);
7529   auto *CXXFor = dyn_cast_or_null<CXXForRangeStmt>(S);
7530   // Ranged for is supported only in OpenMP 5.0.
7531   if (!For && (SemaRef.LangOpts.OpenMP <= 45 || !CXXFor)) {
7532     SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for)
7533         << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
7534         << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount
7535         << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
7536     if (TotalNestedLoopCount > 1) {
7537       if (CollapseLoopCountExpr && OrderedLoopCountExpr)
7538         SemaRef.Diag(DSA.getConstructLoc(),
7539                      diag::note_omp_collapse_ordered_expr)
7540             << 2 << CollapseLoopCountExpr->getSourceRange()
7541             << OrderedLoopCountExpr->getSourceRange();
7542       else if (CollapseLoopCountExpr)
7543         SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
7544                      diag::note_omp_collapse_ordered_expr)
7545             << 0 << CollapseLoopCountExpr->getSourceRange();
7546       else
7547         SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
7548                      diag::note_omp_collapse_ordered_expr)
7549             << 1 << OrderedLoopCountExpr->getSourceRange();
7550     }
7551     return true;
7552   }
7553   assert(((For && For->getBody()) || (CXXFor && CXXFor->getBody())) &&
7554          "No loop body.");
7555 
7556   OpenMPIterationSpaceChecker ISC(SemaRef, DSA,
7557                                   For ? For->getForLoc() : CXXFor->getForLoc());
7558 
7559   // Check init.
7560   Stmt *Init = For ? For->getInit() : CXXFor->getBeginStmt();
7561   if (ISC.checkAndSetInit(Init))
7562     return true;
7563 
7564   bool HasErrors = false;
7565 
7566   // Check loop variable's type.
7567   if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
7568     // OpenMP [2.6, Canonical Loop Form]
7569     // Var is one of the following:
7570     //   A variable of signed or unsigned integer type.
7571     //   For C++, a variable of a random access iterator type.
7572     //   For C, a variable of a pointer type.
7573     QualType VarType = LCDecl->getType().getNonReferenceType();
7574     if (!VarType->isDependentType() && !VarType->isIntegerType() &&
7575         !VarType->isPointerType() &&
7576         !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
7577       SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type)
7578           << SemaRef.getLangOpts().CPlusPlus;
7579       HasErrors = true;
7580     }
7581 
7582     // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
7583     // a Construct
7584     // The loop iteration variable(s) in the associated for-loop(s) of a for or
7585     // parallel for construct is (are) private.
7586     // The loop iteration variable in the associated for-loop of a simd
7587     // construct with just one associated for-loop is linear with a
7588     // constant-linear-step that is the increment of the associated for-loop.
7589     // Exclude loop var from the list of variables with implicitly defined data
7590     // sharing attributes.
7591     VarsWithImplicitDSA.erase(LCDecl);
7592 
7593     assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
7594 
7595     // Check test-expr.
7596     HasErrors |= ISC.checkAndSetCond(For ? For->getCond() : CXXFor->getCond());
7597 
7598     // Check incr-expr.
7599     HasErrors |= ISC.checkAndSetInc(For ? For->getInc() : CXXFor->getInc());
7600   }
7601 
7602   if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
7603     return HasErrors;
7604 
7605   // Build the loop's iteration space representation.
7606   ResultIterSpaces[CurrentNestedLoopCount].PreCond = ISC.buildPreCond(
7607       DSA.getCurScope(), For ? For->getCond() : CXXFor->getCond(), Captures);
7608   ResultIterSpaces[CurrentNestedLoopCount].NumIterations =
7609       ISC.buildNumIterations(DSA.getCurScope(), ResultIterSpaces,
7610                              (isOpenMPWorksharingDirective(DKind) ||
7611                               isOpenMPTaskLoopDirective(DKind) ||
7612                               isOpenMPDistributeDirective(DKind)),
7613                              Captures);
7614   ResultIterSpaces[CurrentNestedLoopCount].CounterVar =
7615       ISC.buildCounterVar(Captures, DSA);
7616   ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar =
7617       ISC.buildPrivateCounterVar();
7618   ResultIterSpaces[CurrentNestedLoopCount].CounterInit = ISC.buildCounterInit();
7619   ResultIterSpaces[CurrentNestedLoopCount].CounterStep = ISC.buildCounterStep();
7620   ResultIterSpaces[CurrentNestedLoopCount].InitSrcRange = ISC.getInitSrcRange();
7621   ResultIterSpaces[CurrentNestedLoopCount].CondSrcRange =
7622       ISC.getConditionSrcRange();
7623   ResultIterSpaces[CurrentNestedLoopCount].IncSrcRange =
7624       ISC.getIncrementSrcRange();
7625   ResultIterSpaces[CurrentNestedLoopCount].Subtract = ISC.shouldSubtractStep();
7626   ResultIterSpaces[CurrentNestedLoopCount].IsStrictCompare =
7627       ISC.isStrictTestOp();
7628   std::tie(ResultIterSpaces[CurrentNestedLoopCount].MinValue,
7629            ResultIterSpaces[CurrentNestedLoopCount].MaxValue) =
7630       ISC.buildMinMaxValues(DSA.getCurScope(), Captures);
7631   ResultIterSpaces[CurrentNestedLoopCount].FinalCondition =
7632       ISC.buildFinalCondition(DSA.getCurScope());
7633   ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularLB =
7634       ISC.doesInitDependOnLC();
7635   ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularUB =
7636       ISC.doesCondDependOnLC();
7637   ResultIterSpaces[CurrentNestedLoopCount].LoopDependentIdx =
7638       ISC.getLoopDependentIdx();
7639 
7640   HasErrors |=
7641       (ResultIterSpaces[CurrentNestedLoopCount].PreCond == nullptr ||
7642        ResultIterSpaces[CurrentNestedLoopCount].NumIterations == nullptr ||
7643        ResultIterSpaces[CurrentNestedLoopCount].CounterVar == nullptr ||
7644        ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar == nullptr ||
7645        ResultIterSpaces[CurrentNestedLoopCount].CounterInit == nullptr ||
7646        ResultIterSpaces[CurrentNestedLoopCount].CounterStep == nullptr);
7647   if (!HasErrors && DSA.isOrderedRegion()) {
7648     if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
7649       if (CurrentNestedLoopCount <
7650           DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
7651         DSA.getOrderedRegionParam().second->setLoopNumIterations(
7652             CurrentNestedLoopCount,
7653             ResultIterSpaces[CurrentNestedLoopCount].NumIterations);
7654         DSA.getOrderedRegionParam().second->setLoopCounter(
7655             CurrentNestedLoopCount,
7656             ResultIterSpaces[CurrentNestedLoopCount].CounterVar);
7657       }
7658     }
7659     for (auto &Pair : DSA.getDoacrossDependClauses()) {
7660       if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) {
7661         // Erroneous case - clause has some problems.
7662         continue;
7663       }
7664       if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink &&
7665           Pair.second.size() <= CurrentNestedLoopCount) {
7666         // Erroneous case - clause has some problems.
7667         Pair.first->setLoopData(CurrentNestedLoopCount, nullptr);
7668         continue;
7669       }
7670       Expr *CntValue;
7671       if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
7672         CntValue = ISC.buildOrderedLoopData(
7673             DSA.getCurScope(),
7674             ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures,
7675             Pair.first->getDependencyLoc());
7676       else
7677         CntValue = ISC.buildOrderedLoopData(
7678             DSA.getCurScope(),
7679             ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures,
7680             Pair.first->getDependencyLoc(),
7681             Pair.second[CurrentNestedLoopCount].first,
7682             Pair.second[CurrentNestedLoopCount].second);
7683       Pair.first->setLoopData(CurrentNestedLoopCount, CntValue);
7684     }
7685   }
7686 
7687   return HasErrors;
7688 }
7689 
7690 /// Build 'VarRef = Start.
7691 static ExprResult
7692 buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
7693                  ExprResult Start, bool IsNonRectangularLB,
7694                  llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
7695   // Build 'VarRef = Start.
7696   ExprResult NewStart = IsNonRectangularLB
7697                             ? Start.get()
7698                             : tryBuildCapture(SemaRef, Start.get(), Captures);
7699   if (!NewStart.isUsable())
7700     return ExprError();
7701   if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
7702                                    VarRef.get()->getType())) {
7703     NewStart = SemaRef.PerformImplicitConversion(
7704         NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
7705         /*AllowExplicit=*/true);
7706     if (!NewStart.isUsable())
7707       return ExprError();
7708   }
7709 
7710   ExprResult Init =
7711       SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
7712   return Init;
7713 }
7714 
7715 /// Build 'VarRef = Start + Iter * Step'.
7716 static ExprResult buildCounterUpdate(
7717     Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
7718     ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
7719     bool IsNonRectangularLB,
7720     llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
7721   // Add parentheses (for debugging purposes only).
7722   Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
7723   if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
7724       !Step.isUsable())
7725     return ExprError();
7726 
7727   ExprResult NewStep = Step;
7728   if (Captures)
7729     NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
7730   if (NewStep.isInvalid())
7731     return ExprError();
7732   ExprResult Update =
7733       SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
7734   if (!Update.isUsable())
7735     return ExprError();
7736 
7737   // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
7738   // 'VarRef = Start (+|-) Iter * Step'.
7739   if (!Start.isUsable())
7740     return ExprError();
7741   ExprResult NewStart = SemaRef.ActOnParenExpr(Loc, Loc, Start.get());
7742   if (!NewStart.isUsable())
7743     return ExprError();
7744   if (Captures && !IsNonRectangularLB)
7745     NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
7746   if (NewStart.isInvalid())
7747     return ExprError();
7748 
7749   // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
7750   ExprResult SavedUpdate = Update;
7751   ExprResult UpdateVal;
7752   if (VarRef.get()->getType()->isOverloadableType() ||
7753       NewStart.get()->getType()->isOverloadableType() ||
7754       Update.get()->getType()->isOverloadableType()) {
7755     Sema::TentativeAnalysisScope Trap(SemaRef);
7756 
7757     Update =
7758         SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
7759     if (Update.isUsable()) {
7760       UpdateVal =
7761           SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
7762                              VarRef.get(), SavedUpdate.get());
7763       if (UpdateVal.isUsable()) {
7764         Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
7765                                             UpdateVal.get());
7766       }
7767     }
7768   }
7769 
7770   // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
7771   if (!Update.isUsable() || !UpdateVal.isUsable()) {
7772     Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
7773                                 NewStart.get(), SavedUpdate.get());
7774     if (!Update.isUsable())
7775       return ExprError();
7776 
7777     if (!SemaRef.Context.hasSameType(Update.get()->getType(),
7778                                      VarRef.get()->getType())) {
7779       Update = SemaRef.PerformImplicitConversion(
7780           Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
7781       if (!Update.isUsable())
7782         return ExprError();
7783     }
7784 
7785     Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
7786   }
7787   return Update;
7788 }
7789 
7790 /// Convert integer expression \a E to make it have at least \a Bits
7791 /// bits.
7792 static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
7793   if (E == nullptr)
7794     return ExprError();
7795   ASTContext &C = SemaRef.Context;
7796   QualType OldType = E->getType();
7797   unsigned HasBits = C.getTypeSize(OldType);
7798   if (HasBits >= Bits)
7799     return ExprResult(E);
7800   // OK to convert to signed, because new type has more bits than old.
7801   QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
7802   return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
7803                                            true);
7804 }
7805 
7806 /// Check if the given expression \a E is a constant integer that fits
7807 /// into \a Bits bits.
7808 static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
7809   if (E == nullptr)
7810     return false;
7811   llvm::APSInt Result;
7812   if (E->isIntegerConstantExpr(Result, SemaRef.Context))
7813     return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
7814   return false;
7815 }
7816 
7817 /// Build preinits statement for the given declarations.
7818 static Stmt *buildPreInits(ASTContext &Context,
7819                            MutableArrayRef<Decl *> PreInits) {
7820   if (!PreInits.empty()) {
7821     return new (Context) DeclStmt(
7822         DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
7823         SourceLocation(), SourceLocation());
7824   }
7825   return nullptr;
7826 }
7827 
7828 /// Build preinits statement for the given declarations.
7829 static Stmt *
7830 buildPreInits(ASTContext &Context,
7831               const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
7832   if (!Captures.empty()) {
7833     SmallVector<Decl *, 16> PreInits;
7834     for (const auto &Pair : Captures)
7835       PreInits.push_back(Pair.second->getDecl());
7836     return buildPreInits(Context, PreInits);
7837   }
7838   return nullptr;
7839 }
7840 
7841 /// Build postupdate expression for the given list of postupdates expressions.
7842 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
7843   Expr *PostUpdate = nullptr;
7844   if (!PostUpdates.empty()) {
7845     for (Expr *E : PostUpdates) {
7846       Expr *ConvE = S.BuildCStyleCastExpr(
7847                          E->getExprLoc(),
7848                          S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
7849                          E->getExprLoc(), E)
7850                         .get();
7851       PostUpdate = PostUpdate
7852                        ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
7853                                               PostUpdate, ConvE)
7854                              .get()
7855                        : ConvE;
7856     }
7857   }
7858   return PostUpdate;
7859 }
7860 
7861 /// Called on a for stmt to check itself and nested loops (if any).
7862 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
7863 /// number of collapsed loops otherwise.
7864 static unsigned
7865 checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
7866                 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
7867                 DSAStackTy &DSA,
7868                 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
7869                 OMPLoopDirective::HelperExprs &Built) {
7870   unsigned NestedLoopCount = 1;
7871   if (CollapseLoopCountExpr) {
7872     // Found 'collapse' clause - calculate collapse number.
7873     Expr::EvalResult Result;
7874     if (!CollapseLoopCountExpr->isValueDependent() &&
7875         CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
7876       NestedLoopCount = Result.Val.getInt().getLimitedValue();
7877     } else {
7878       Built.clear(/*Size=*/1);
7879       return 1;
7880     }
7881   }
7882   unsigned OrderedLoopCount = 1;
7883   if (OrderedLoopCountExpr) {
7884     // Found 'ordered' clause - calculate collapse number.
7885     Expr::EvalResult EVResult;
7886     if (!OrderedLoopCountExpr->isValueDependent() &&
7887         OrderedLoopCountExpr->EvaluateAsInt(EVResult,
7888                                             SemaRef.getASTContext())) {
7889       llvm::APSInt Result = EVResult.Val.getInt();
7890       if (Result.getLimitedValue() < NestedLoopCount) {
7891         SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
7892                      diag::err_omp_wrong_ordered_loop_count)
7893             << OrderedLoopCountExpr->getSourceRange();
7894         SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
7895                      diag::note_collapse_loop_count)
7896             << CollapseLoopCountExpr->getSourceRange();
7897       }
7898       OrderedLoopCount = Result.getLimitedValue();
7899     } else {
7900       Built.clear(/*Size=*/1);
7901       return 1;
7902     }
7903   }
7904   // This is helper routine for loop directives (e.g., 'for', 'simd',
7905   // 'for simd', etc.).
7906   llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
7907   SmallVector<LoopIterationSpace, 4> IterSpaces(
7908       std::max(OrderedLoopCount, NestedLoopCount));
7909   Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
7910   for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
7911     if (checkOpenMPIterationSpace(
7912             DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
7913             std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
7914             OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces, Captures))
7915       return 0;
7916     // Move on to the next nested for loop, or to the loop body.
7917     // OpenMP [2.8.1, simd construct, Restrictions]
7918     // All loops associated with the construct must be perfectly nested; that
7919     // is, there must be no intervening code nor any OpenMP directive between
7920     // any two loops.
7921     if (auto *For = dyn_cast<ForStmt>(CurStmt)) {
7922       CurStmt = For->getBody();
7923     } else {
7924       assert(isa<CXXForRangeStmt>(CurStmt) &&
7925              "Expected canonical for or range-based for loops.");
7926       CurStmt = cast<CXXForRangeStmt>(CurStmt)->getBody();
7927     }
7928     CurStmt = OMPLoopDirective::tryToFindNextInnerLoop(
7929         CurStmt, SemaRef.LangOpts.OpenMP >= 50);
7930   }
7931   for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) {
7932     if (checkOpenMPIterationSpace(
7933             DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
7934             std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
7935             OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces, Captures))
7936       return 0;
7937     if (Cnt > 0 && IterSpaces[Cnt].CounterVar) {
7938       // Handle initialization of captured loop iterator variables.
7939       auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar);
7940       if (isa<OMPCapturedExprDecl>(DRE->getDecl())) {
7941         Captures[DRE] = DRE;
7942       }
7943     }
7944     // Move on to the next nested for loop, or to the loop body.
7945     // OpenMP [2.8.1, simd construct, Restrictions]
7946     // All loops associated with the construct must be perfectly nested; that
7947     // is, there must be no intervening code nor any OpenMP directive between
7948     // any two loops.
7949     if (auto *For = dyn_cast<ForStmt>(CurStmt)) {
7950       CurStmt = For->getBody();
7951     } else {
7952       assert(isa<CXXForRangeStmt>(CurStmt) &&
7953              "Expected canonical for or range-based for loops.");
7954       CurStmt = cast<CXXForRangeStmt>(CurStmt)->getBody();
7955     }
7956     CurStmt = OMPLoopDirective::tryToFindNextInnerLoop(
7957         CurStmt, SemaRef.LangOpts.OpenMP >= 50);
7958   }
7959 
7960   Built.clear(/* size */ NestedLoopCount);
7961 
7962   if (SemaRef.CurContext->isDependentContext())
7963     return NestedLoopCount;
7964 
7965   // An example of what is generated for the following code:
7966   //
7967   //   #pragma omp simd collapse(2) ordered(2)
7968   //   for (i = 0; i < NI; ++i)
7969   //     for (k = 0; k < NK; ++k)
7970   //       for (j = J0; j < NJ; j+=2) {
7971   //         <loop body>
7972   //       }
7973   //
7974   // We generate the code below.
7975   // Note: the loop body may be outlined in CodeGen.
7976   // Note: some counters may be C++ classes, operator- is used to find number of
7977   // iterations and operator+= to calculate counter value.
7978   // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
7979   // or i64 is currently supported).
7980   //
7981   //   #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
7982   //   for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
7983   //     .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
7984   //     .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
7985   //     // similar updates for vars in clauses (e.g. 'linear')
7986   //     <loop body (using local i and j)>
7987   //   }
7988   //   i = NI; // assign final values of counters
7989   //   j = NJ;
7990   //
7991 
7992   // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
7993   // the iteration counts of the collapsed for loops.
7994   // Precondition tests if there is at least one iteration (all conditions are
7995   // true).
7996   auto PreCond = ExprResult(IterSpaces[0].PreCond);
7997   Expr *N0 = IterSpaces[0].NumIterations;
7998   ExprResult LastIteration32 =
7999       widenIterationCount(/*Bits=*/32,
8000                           SemaRef
8001                               .PerformImplicitConversion(
8002                                   N0->IgnoreImpCasts(), N0->getType(),
8003                                   Sema::AA_Converting, /*AllowExplicit=*/true)
8004                               .get(),
8005                           SemaRef);
8006   ExprResult LastIteration64 = widenIterationCount(
8007       /*Bits=*/64,
8008       SemaRef
8009           .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(),
8010                                      Sema::AA_Converting,
8011                                      /*AllowExplicit=*/true)
8012           .get(),
8013       SemaRef);
8014 
8015   if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
8016     return NestedLoopCount;
8017 
8018   ASTContext &C = SemaRef.Context;
8019   bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
8020 
8021   Scope *CurScope = DSA.getCurScope();
8022   for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
8023     if (PreCond.isUsable()) {
8024       PreCond =
8025           SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
8026                              PreCond.get(), IterSpaces[Cnt].PreCond);
8027     }
8028     Expr *N = IterSpaces[Cnt].NumIterations;
8029     SourceLocation Loc = N->getExprLoc();
8030     AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
8031     if (LastIteration32.isUsable())
8032       LastIteration32 = SemaRef.BuildBinOp(
8033           CurScope, Loc, BO_Mul, LastIteration32.get(),
8034           SemaRef
8035               .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
8036                                          Sema::AA_Converting,
8037                                          /*AllowExplicit=*/true)
8038               .get());
8039     if (LastIteration64.isUsable())
8040       LastIteration64 = SemaRef.BuildBinOp(
8041           CurScope, Loc, BO_Mul, LastIteration64.get(),
8042           SemaRef
8043               .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
8044                                          Sema::AA_Converting,
8045                                          /*AllowExplicit=*/true)
8046               .get());
8047   }
8048 
8049   // Choose either the 32-bit or 64-bit version.
8050   ExprResult LastIteration = LastIteration64;
8051   if (SemaRef.getLangOpts().OpenMPOptimisticCollapse ||
8052       (LastIteration32.isUsable() &&
8053        C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
8054        (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
8055         fitsInto(
8056             /*Bits=*/32,
8057             LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
8058             LastIteration64.get(), SemaRef))))
8059     LastIteration = LastIteration32;
8060   QualType VType = LastIteration.get()->getType();
8061   QualType RealVType = VType;
8062   QualType StrideVType = VType;
8063   if (isOpenMPTaskLoopDirective(DKind)) {
8064     VType =
8065         SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
8066     StrideVType =
8067         SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
8068   }
8069 
8070   if (!LastIteration.isUsable())
8071     return 0;
8072 
8073   // Save the number of iterations.
8074   ExprResult NumIterations = LastIteration;
8075   {
8076     LastIteration = SemaRef.BuildBinOp(
8077         CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
8078         LastIteration.get(),
8079         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
8080     if (!LastIteration.isUsable())
8081       return 0;
8082   }
8083 
8084   // Calculate the last iteration number beforehand instead of doing this on
8085   // each iteration. Do not do this if the number of iterations may be kfold-ed.
8086   llvm::APSInt Result;
8087   bool IsConstant =
8088       LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
8089   ExprResult CalcLastIteration;
8090   if (!IsConstant) {
8091     ExprResult SaveRef =
8092         tryBuildCapture(SemaRef, LastIteration.get(), Captures);
8093     LastIteration = SaveRef;
8094 
8095     // Prepare SaveRef + 1.
8096     NumIterations = SemaRef.BuildBinOp(
8097         CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
8098         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
8099     if (!NumIterations.isUsable())
8100       return 0;
8101   }
8102 
8103   SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
8104 
8105   // Build variables passed into runtime, necessary for worksharing directives.
8106   ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
8107   if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
8108       isOpenMPDistributeDirective(DKind)) {
8109     // Lower bound variable, initialized with zero.
8110     VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
8111     LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
8112     SemaRef.AddInitializerToDecl(LBDecl,
8113                                  SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
8114                                  /*DirectInit*/ false);
8115 
8116     // Upper bound variable, initialized with last iteration number.
8117     VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
8118     UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
8119     SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
8120                                  /*DirectInit*/ false);
8121 
8122     // A 32-bit variable-flag where runtime returns 1 for the last iteration.
8123     // This will be used to implement clause 'lastprivate'.
8124     QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
8125     VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
8126     IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
8127     SemaRef.AddInitializerToDecl(ILDecl,
8128                                  SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
8129                                  /*DirectInit*/ false);
8130 
8131     // Stride variable returned by runtime (we initialize it to 1 by default).
8132     VarDecl *STDecl =
8133         buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
8134     ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
8135     SemaRef.AddInitializerToDecl(STDecl,
8136                                  SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
8137                                  /*DirectInit*/ false);
8138 
8139     // Build expression: UB = min(UB, LastIteration)
8140     // It is necessary for CodeGen of directives with static scheduling.
8141     ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
8142                                                 UB.get(), LastIteration.get());
8143     ExprResult CondOp = SemaRef.ActOnConditionalOp(
8144         LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(),
8145         LastIteration.get(), UB.get());
8146     EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
8147                              CondOp.get());
8148     EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false);
8149 
8150     // If we have a combined directive that combines 'distribute', 'for' or
8151     // 'simd' we need to be able to access the bounds of the schedule of the
8152     // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
8153     // by scheduling 'distribute' have to be passed to the schedule of 'for'.
8154     if (isOpenMPLoopBoundSharingDirective(DKind)) {
8155       // Lower bound variable, initialized with zero.
8156       VarDecl *CombLBDecl =
8157           buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
8158       CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
8159       SemaRef.AddInitializerToDecl(
8160           CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
8161           /*DirectInit*/ false);
8162 
8163       // Upper bound variable, initialized with last iteration number.
8164       VarDecl *CombUBDecl =
8165           buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
8166       CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
8167       SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
8168                                    /*DirectInit*/ false);
8169 
8170       ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
8171           CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
8172       ExprResult CombCondOp =
8173           SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
8174                                      LastIteration.get(), CombUB.get());
8175       CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
8176                                    CombCondOp.get());
8177       CombEUB =
8178           SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false);
8179 
8180       const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
8181       // We expect to have at least 2 more parameters than the 'parallel'
8182       // directive does - the lower and upper bounds of the previous schedule.
8183       assert(CD->getNumParams() >= 4 &&
8184              "Unexpected number of parameters in loop combined directive");
8185 
8186       // Set the proper type for the bounds given what we learned from the
8187       // enclosed loops.
8188       ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
8189       ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
8190 
8191       // Previous lower and upper bounds are obtained from the region
8192       // parameters.
8193       PrevLB =
8194           buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
8195       PrevUB =
8196           buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
8197     }
8198   }
8199 
8200   // Build the iteration variable and its initialization before loop.
8201   ExprResult IV;
8202   ExprResult Init, CombInit;
8203   {
8204     VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
8205     IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
8206     Expr *RHS =
8207         (isOpenMPWorksharingDirective(DKind) ||
8208          isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
8209             ? LB.get()
8210             : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
8211     Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
8212     Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false);
8213 
8214     if (isOpenMPLoopBoundSharingDirective(DKind)) {
8215       Expr *CombRHS =
8216           (isOpenMPWorksharingDirective(DKind) ||
8217            isOpenMPTaskLoopDirective(DKind) ||
8218            isOpenMPDistributeDirective(DKind))
8219               ? CombLB.get()
8220               : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
8221       CombInit =
8222           SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
8223       CombInit =
8224           SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false);
8225     }
8226   }
8227 
8228   bool UseStrictCompare =
8229       RealVType->hasUnsignedIntegerRepresentation() &&
8230       llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) {
8231         return LIS.IsStrictCompare;
8232       });
8233   // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for
8234   // unsigned IV)) for worksharing loops.
8235   SourceLocation CondLoc = AStmt->getBeginLoc();
8236   Expr *BoundUB = UB.get();
8237   if (UseStrictCompare) {
8238     BoundUB =
8239         SemaRef
8240             .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB,
8241                         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
8242             .get();
8243     BoundUB =
8244         SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get();
8245   }
8246   ExprResult Cond =
8247       (isOpenMPWorksharingDirective(DKind) ||
8248        isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
8249           ? SemaRef.BuildBinOp(CurScope, CondLoc,
8250                                UseStrictCompare ? BO_LT : BO_LE, IV.get(),
8251                                BoundUB)
8252           : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
8253                                NumIterations.get());
8254   ExprResult CombDistCond;
8255   if (isOpenMPLoopBoundSharingDirective(DKind)) {
8256     CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
8257                                       NumIterations.get());
8258   }
8259 
8260   ExprResult CombCond;
8261   if (isOpenMPLoopBoundSharingDirective(DKind)) {
8262     Expr *BoundCombUB = CombUB.get();
8263     if (UseStrictCompare) {
8264       BoundCombUB =
8265           SemaRef
8266               .BuildBinOp(
8267                   CurScope, CondLoc, BO_Add, BoundCombUB,
8268                   SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
8269               .get();
8270       BoundCombUB =
8271           SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false)
8272               .get();
8273     }
8274     CombCond =
8275         SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE,
8276                            IV.get(), BoundCombUB);
8277   }
8278   // Loop increment (IV = IV + 1)
8279   SourceLocation IncLoc = AStmt->getBeginLoc();
8280   ExprResult Inc =
8281       SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
8282                          SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
8283   if (!Inc.isUsable())
8284     return 0;
8285   Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
8286   Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false);
8287   if (!Inc.isUsable())
8288     return 0;
8289 
8290   // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
8291   // Used for directives with static scheduling.
8292   // In combined construct, add combined version that use CombLB and CombUB
8293   // base variables for the update
8294   ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
8295   if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
8296       isOpenMPDistributeDirective(DKind)) {
8297     // LB + ST
8298     NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
8299     if (!NextLB.isUsable())
8300       return 0;
8301     // LB = LB + ST
8302     NextLB =
8303         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
8304     NextLB =
8305         SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false);
8306     if (!NextLB.isUsable())
8307       return 0;
8308     // UB + ST
8309     NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
8310     if (!NextUB.isUsable())
8311       return 0;
8312     // UB = UB + ST
8313     NextUB =
8314         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
8315     NextUB =
8316         SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false);
8317     if (!NextUB.isUsable())
8318       return 0;
8319     if (isOpenMPLoopBoundSharingDirective(DKind)) {
8320       CombNextLB =
8321           SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
8322       if (!NextLB.isUsable())
8323         return 0;
8324       // LB = LB + ST
8325       CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
8326                                       CombNextLB.get());
8327       CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(),
8328                                                /*DiscardedValue*/ false);
8329       if (!CombNextLB.isUsable())
8330         return 0;
8331       // UB + ST
8332       CombNextUB =
8333           SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
8334       if (!CombNextUB.isUsable())
8335         return 0;
8336       // UB = UB + ST
8337       CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
8338                                       CombNextUB.get());
8339       CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(),
8340                                                /*DiscardedValue*/ false);
8341       if (!CombNextUB.isUsable())
8342         return 0;
8343     }
8344   }
8345 
8346   // Create increment expression for distribute loop when combined in a same
8347   // directive with for as IV = IV + ST; ensure upper bound expression based
8348   // on PrevUB instead of NumIterations - used to implement 'for' when found
8349   // in combination with 'distribute', like in 'distribute parallel for'
8350   SourceLocation DistIncLoc = AStmt->getBeginLoc();
8351   ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond;
8352   if (isOpenMPLoopBoundSharingDirective(DKind)) {
8353     DistCond = SemaRef.BuildBinOp(
8354         CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB);
8355     assert(DistCond.isUsable() && "distribute cond expr was not built");
8356 
8357     DistInc =
8358         SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
8359     assert(DistInc.isUsable() && "distribute inc expr was not built");
8360     DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
8361                                  DistInc.get());
8362     DistInc =
8363         SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false);
8364     assert(DistInc.isUsable() && "distribute inc expr was not built");
8365 
8366     // Build expression: UB = min(UB, prevUB) for #for in composite or combined
8367     // construct
8368     SourceLocation DistEUBLoc = AStmt->getBeginLoc();
8369     ExprResult IsUBGreater =
8370         SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
8371     ExprResult CondOp = SemaRef.ActOnConditionalOp(
8372         DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
8373     PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
8374                                  CondOp.get());
8375     PrevEUB =
8376         SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false);
8377 
8378     // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in
8379     // parallel for is in combination with a distribute directive with
8380     // schedule(static, 1)
8381     Expr *BoundPrevUB = PrevUB.get();
8382     if (UseStrictCompare) {
8383       BoundPrevUB =
8384           SemaRef
8385               .BuildBinOp(
8386                   CurScope, CondLoc, BO_Add, BoundPrevUB,
8387                   SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
8388               .get();
8389       BoundPrevUB =
8390           SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false)
8391               .get();
8392     }
8393     ParForInDistCond =
8394         SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE,
8395                            IV.get(), BoundPrevUB);
8396   }
8397 
8398   // Build updates and final values of the loop counters.
8399   bool HasErrors = false;
8400   Built.Counters.resize(NestedLoopCount);
8401   Built.Inits.resize(NestedLoopCount);
8402   Built.Updates.resize(NestedLoopCount);
8403   Built.Finals.resize(NestedLoopCount);
8404   Built.DependentCounters.resize(NestedLoopCount);
8405   Built.DependentInits.resize(NestedLoopCount);
8406   Built.FinalsConditions.resize(NestedLoopCount);
8407   {
8408     // We implement the following algorithm for obtaining the
8409     // original loop iteration variable values based on the
8410     // value of the collapsed loop iteration variable IV.
8411     //
8412     // Let n+1 be the number of collapsed loops in the nest.
8413     // Iteration variables (I0, I1, .... In)
8414     // Iteration counts (N0, N1, ... Nn)
8415     //
8416     // Acc = IV;
8417     //
8418     // To compute Ik for loop k, 0 <= k <= n, generate:
8419     //    Prod = N(k+1) * N(k+2) * ... * Nn;
8420     //    Ik = Acc / Prod;
8421     //    Acc -= Ik * Prod;
8422     //
8423     ExprResult Acc = IV;
8424     for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
8425       LoopIterationSpace &IS = IterSpaces[Cnt];
8426       SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
8427       ExprResult Iter;
8428 
8429       // Compute prod
8430       ExprResult Prod =
8431           SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
8432       for (unsigned int K = Cnt+1; K < NestedLoopCount; ++K)
8433         Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(),
8434                                   IterSpaces[K].NumIterations);
8435 
8436       // Iter = Acc / Prod
8437       // If there is at least one more inner loop to avoid
8438       // multiplication by 1.
8439       if (Cnt + 1 < NestedLoopCount)
8440         Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div,
8441                                   Acc.get(), Prod.get());
8442       else
8443         Iter = Acc;
8444       if (!Iter.isUsable()) {
8445         HasErrors = true;
8446         break;
8447       }
8448 
8449       // Update Acc:
8450       // Acc -= Iter * Prod
8451       // Check if there is at least one more inner loop to avoid
8452       // multiplication by 1.
8453       if (Cnt + 1 < NestedLoopCount)
8454         Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul,
8455                                   Iter.get(), Prod.get());
8456       else
8457         Prod = Iter;
8458       Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub,
8459                                Acc.get(), Prod.get());
8460 
8461       // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
8462       auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
8463       DeclRefExpr *CounterVar = buildDeclRefExpr(
8464           SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
8465           /*RefersToCapture=*/true);
8466       ExprResult Init =
8467           buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
8468                            IS.CounterInit, IS.IsNonRectangularLB, Captures);
8469       if (!Init.isUsable()) {
8470         HasErrors = true;
8471         break;
8472       }
8473       ExprResult Update = buildCounterUpdate(
8474           SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
8475           IS.CounterStep, IS.Subtract, IS.IsNonRectangularLB, &Captures);
8476       if (!Update.isUsable()) {
8477         HasErrors = true;
8478         break;
8479       }
8480 
8481       // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
8482       ExprResult Final =
8483           buildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
8484                              IS.CounterInit, IS.NumIterations, IS.CounterStep,
8485                              IS.Subtract, IS.IsNonRectangularLB, &Captures);
8486       if (!Final.isUsable()) {
8487         HasErrors = true;
8488         break;
8489       }
8490 
8491       if (!Update.isUsable() || !Final.isUsable()) {
8492         HasErrors = true;
8493         break;
8494       }
8495       // Save results
8496       Built.Counters[Cnt] = IS.CounterVar;
8497       Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
8498       Built.Inits[Cnt] = Init.get();
8499       Built.Updates[Cnt] = Update.get();
8500       Built.Finals[Cnt] = Final.get();
8501       Built.DependentCounters[Cnt] = nullptr;
8502       Built.DependentInits[Cnt] = nullptr;
8503       Built.FinalsConditions[Cnt] = nullptr;
8504       if (IS.IsNonRectangularLB || IS.IsNonRectangularUB) {
8505         Built.DependentCounters[Cnt] =
8506             Built.Counters[NestedLoopCount - 1 - IS.LoopDependentIdx];
8507         Built.DependentInits[Cnt] =
8508             Built.Inits[NestedLoopCount - 1 - IS.LoopDependentIdx];
8509         Built.FinalsConditions[Cnt] = IS.FinalCondition;
8510       }
8511     }
8512   }
8513 
8514   if (HasErrors)
8515     return 0;
8516 
8517   // Save results
8518   Built.IterationVarRef = IV.get();
8519   Built.LastIteration = LastIteration.get();
8520   Built.NumIterations = NumIterations.get();
8521   Built.CalcLastIteration = SemaRef
8522                                 .ActOnFinishFullExpr(CalcLastIteration.get(),
8523                                                      /*DiscardedValue=*/false)
8524                                 .get();
8525   Built.PreCond = PreCond.get();
8526   Built.PreInits = buildPreInits(C, Captures);
8527   Built.Cond = Cond.get();
8528   Built.Init = Init.get();
8529   Built.Inc = Inc.get();
8530   Built.LB = LB.get();
8531   Built.UB = UB.get();
8532   Built.IL = IL.get();
8533   Built.ST = ST.get();
8534   Built.EUB = EUB.get();
8535   Built.NLB = NextLB.get();
8536   Built.NUB = NextUB.get();
8537   Built.PrevLB = PrevLB.get();
8538   Built.PrevUB = PrevUB.get();
8539   Built.DistInc = DistInc.get();
8540   Built.PrevEUB = PrevEUB.get();
8541   Built.DistCombinedFields.LB = CombLB.get();
8542   Built.DistCombinedFields.UB = CombUB.get();
8543   Built.DistCombinedFields.EUB = CombEUB.get();
8544   Built.DistCombinedFields.Init = CombInit.get();
8545   Built.DistCombinedFields.Cond = CombCond.get();
8546   Built.DistCombinedFields.NLB = CombNextLB.get();
8547   Built.DistCombinedFields.NUB = CombNextUB.get();
8548   Built.DistCombinedFields.DistCond = CombDistCond.get();
8549   Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get();
8550 
8551   return NestedLoopCount;
8552 }
8553 
8554 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
8555   auto CollapseClauses =
8556       OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
8557   if (CollapseClauses.begin() != CollapseClauses.end())
8558     return (*CollapseClauses.begin())->getNumForLoops();
8559   return nullptr;
8560 }
8561 
8562 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
8563   auto OrderedClauses =
8564       OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
8565   if (OrderedClauses.begin() != OrderedClauses.end())
8566     return (*OrderedClauses.begin())->getNumForLoops();
8567   return nullptr;
8568 }
8569 
8570 static bool checkSimdlenSafelenSpecified(Sema &S,
8571                                          const ArrayRef<OMPClause *> Clauses) {
8572   const OMPSafelenClause *Safelen = nullptr;
8573   const OMPSimdlenClause *Simdlen = nullptr;
8574 
8575   for (const OMPClause *Clause : Clauses) {
8576     if (Clause->getClauseKind() == OMPC_safelen)
8577       Safelen = cast<OMPSafelenClause>(Clause);
8578     else if (Clause->getClauseKind() == OMPC_simdlen)
8579       Simdlen = cast<OMPSimdlenClause>(Clause);
8580     if (Safelen && Simdlen)
8581       break;
8582   }
8583 
8584   if (Simdlen && Safelen) {
8585     const Expr *SimdlenLength = Simdlen->getSimdlen();
8586     const Expr *SafelenLength = Safelen->getSafelen();
8587     if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
8588         SimdlenLength->isInstantiationDependent() ||
8589         SimdlenLength->containsUnexpandedParameterPack())
8590       return false;
8591     if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
8592         SafelenLength->isInstantiationDependent() ||
8593         SafelenLength->containsUnexpandedParameterPack())
8594       return false;
8595     Expr::EvalResult SimdlenResult, SafelenResult;
8596     SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context);
8597     SafelenLength->EvaluateAsInt(SafelenResult, S.Context);
8598     llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt();
8599     llvm::APSInt SafelenRes = SafelenResult.Val.getInt();
8600     // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
8601     // If both simdlen and safelen clauses are specified, the value of the
8602     // simdlen parameter must be less than or equal to the value of the safelen
8603     // parameter.
8604     if (SimdlenRes > SafelenRes) {
8605       S.Diag(SimdlenLength->getExprLoc(),
8606              diag::err_omp_wrong_simdlen_safelen_values)
8607           << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
8608       return true;
8609     }
8610   }
8611   return false;
8612 }
8613 
8614 StmtResult
8615 Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
8616                                SourceLocation StartLoc, SourceLocation EndLoc,
8617                                VarsWithInheritedDSAType &VarsWithImplicitDSA) {
8618   if (!AStmt)
8619     return StmtError();
8620 
8621   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8622   OMPLoopDirective::HelperExprs B;
8623   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
8624   // define the nested loops number.
8625   unsigned NestedLoopCount = checkOpenMPLoop(
8626       OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
8627       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
8628   if (NestedLoopCount == 0)
8629     return StmtError();
8630 
8631   assert((CurContext->isDependentContext() || B.builtAll()) &&
8632          "omp simd loop exprs were not built");
8633 
8634   if (!CurContext->isDependentContext()) {
8635     // Finalize the clauses that need pre-built expressions for CodeGen.
8636     for (OMPClause *C : Clauses) {
8637       if (auto *LC = dyn_cast<OMPLinearClause>(C))
8638         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8639                                      B.NumIterations, *this, CurScope,
8640                                      DSAStack))
8641           return StmtError();
8642     }
8643   }
8644 
8645   if (checkSimdlenSafelenSpecified(*this, Clauses))
8646     return StmtError();
8647 
8648   setFunctionHasBranchProtectedScope();
8649   return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
8650                                   Clauses, AStmt, B);
8651 }
8652 
8653 StmtResult
8654 Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
8655                               SourceLocation StartLoc, SourceLocation EndLoc,
8656                               VarsWithInheritedDSAType &VarsWithImplicitDSA) {
8657   if (!AStmt)
8658     return StmtError();
8659 
8660   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8661   OMPLoopDirective::HelperExprs B;
8662   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
8663   // define the nested loops number.
8664   unsigned NestedLoopCount = checkOpenMPLoop(
8665       OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
8666       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
8667   if (NestedLoopCount == 0)
8668     return StmtError();
8669 
8670   assert((CurContext->isDependentContext() || B.builtAll()) &&
8671          "omp for loop exprs were not built");
8672 
8673   if (!CurContext->isDependentContext()) {
8674     // Finalize the clauses that need pre-built expressions for CodeGen.
8675     for (OMPClause *C : Clauses) {
8676       if (auto *LC = dyn_cast<OMPLinearClause>(C))
8677         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8678                                      B.NumIterations, *this, CurScope,
8679                                      DSAStack))
8680           return StmtError();
8681     }
8682   }
8683 
8684   setFunctionHasBranchProtectedScope();
8685   return OMPForDirective::Create(
8686       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
8687       DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
8688 }
8689 
8690 StmtResult Sema::ActOnOpenMPForSimdDirective(
8691     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
8692     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
8693   if (!AStmt)
8694     return StmtError();
8695 
8696   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8697   OMPLoopDirective::HelperExprs B;
8698   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
8699   // define the nested loops number.
8700   unsigned NestedLoopCount =
8701       checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
8702                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
8703                       VarsWithImplicitDSA, B);
8704   if (NestedLoopCount == 0)
8705     return StmtError();
8706 
8707   assert((CurContext->isDependentContext() || B.builtAll()) &&
8708          "omp for simd loop exprs were not built");
8709 
8710   if (!CurContext->isDependentContext()) {
8711     // Finalize the clauses that need pre-built expressions for CodeGen.
8712     for (OMPClause *C : Clauses) {
8713       if (auto *LC = dyn_cast<OMPLinearClause>(C))
8714         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8715                                      B.NumIterations, *this, CurScope,
8716                                      DSAStack))
8717           return StmtError();
8718     }
8719   }
8720 
8721   if (checkSimdlenSafelenSpecified(*this, Clauses))
8722     return StmtError();
8723 
8724   setFunctionHasBranchProtectedScope();
8725   return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
8726                                      Clauses, AStmt, B);
8727 }
8728 
8729 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
8730                                               Stmt *AStmt,
8731                                               SourceLocation StartLoc,
8732                                               SourceLocation EndLoc) {
8733   if (!AStmt)
8734     return StmtError();
8735 
8736   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8737   auto BaseStmt = AStmt;
8738   while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
8739     BaseStmt = CS->getCapturedStmt();
8740   if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
8741     auto S = C->children();
8742     if (S.begin() == S.end())
8743       return StmtError();
8744     // All associated statements must be '#pragma omp section' except for
8745     // the first one.
8746     for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
8747       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
8748         if (SectionStmt)
8749           Diag(SectionStmt->getBeginLoc(),
8750                diag::err_omp_sections_substmt_not_section);
8751         return StmtError();
8752       }
8753       cast<OMPSectionDirective>(SectionStmt)
8754           ->setHasCancel(DSAStack->isCancelRegion());
8755     }
8756   } else {
8757     Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt);
8758     return StmtError();
8759   }
8760 
8761   setFunctionHasBranchProtectedScope();
8762 
8763   return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
8764                                       DSAStack->getTaskgroupReductionRef(),
8765                                       DSAStack->isCancelRegion());
8766 }
8767 
8768 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
8769                                              SourceLocation StartLoc,
8770                                              SourceLocation EndLoc) {
8771   if (!AStmt)
8772     return StmtError();
8773 
8774   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8775 
8776   setFunctionHasBranchProtectedScope();
8777   DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
8778 
8779   return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
8780                                      DSAStack->isCancelRegion());
8781 }
8782 
8783 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
8784                                             Stmt *AStmt,
8785                                             SourceLocation StartLoc,
8786                                             SourceLocation EndLoc) {
8787   if (!AStmt)
8788     return StmtError();
8789 
8790   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8791 
8792   setFunctionHasBranchProtectedScope();
8793 
8794   // OpenMP [2.7.3, single Construct, Restrictions]
8795   // The copyprivate clause must not be used with the nowait clause.
8796   const OMPClause *Nowait = nullptr;
8797   const OMPClause *Copyprivate = nullptr;
8798   for (const OMPClause *Clause : Clauses) {
8799     if (Clause->getClauseKind() == OMPC_nowait)
8800       Nowait = Clause;
8801     else if (Clause->getClauseKind() == OMPC_copyprivate)
8802       Copyprivate = Clause;
8803     if (Copyprivate && Nowait) {
8804       Diag(Copyprivate->getBeginLoc(),
8805            diag::err_omp_single_copyprivate_with_nowait);
8806       Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here);
8807       return StmtError();
8808     }
8809   }
8810 
8811   return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
8812 }
8813 
8814 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
8815                                             SourceLocation StartLoc,
8816                                             SourceLocation EndLoc) {
8817   if (!AStmt)
8818     return StmtError();
8819 
8820   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8821 
8822   setFunctionHasBranchProtectedScope();
8823 
8824   return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
8825 }
8826 
8827 StmtResult Sema::ActOnOpenMPCriticalDirective(
8828     const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
8829     Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
8830   if (!AStmt)
8831     return StmtError();
8832 
8833   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8834 
8835   bool ErrorFound = false;
8836   llvm::APSInt Hint;
8837   SourceLocation HintLoc;
8838   bool DependentHint = false;
8839   for (const OMPClause *C : Clauses) {
8840     if (C->getClauseKind() == OMPC_hint) {
8841       if (!DirName.getName()) {
8842         Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name);
8843         ErrorFound = true;
8844       }
8845       Expr *E = cast<OMPHintClause>(C)->getHint();
8846       if (E->isTypeDependent() || E->isValueDependent() ||
8847           E->isInstantiationDependent()) {
8848         DependentHint = true;
8849       } else {
8850         Hint = E->EvaluateKnownConstInt(Context);
8851         HintLoc = C->getBeginLoc();
8852       }
8853     }
8854   }
8855   if (ErrorFound)
8856     return StmtError();
8857   const auto Pair = DSAStack->getCriticalWithHint(DirName);
8858   if (Pair.first && DirName.getName() && !DependentHint) {
8859     if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
8860       Diag(StartLoc, diag::err_omp_critical_with_hint);
8861       if (HintLoc.isValid())
8862         Diag(HintLoc, diag::note_omp_critical_hint_here)
8863             << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
8864       else
8865         Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
8866       if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
8867         Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here)
8868             << 1
8869             << C->getHint()->EvaluateKnownConstInt(Context).toString(
8870                    /*Radix=*/10, /*Signed=*/false);
8871       } else {
8872         Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1;
8873       }
8874     }
8875   }
8876 
8877   setFunctionHasBranchProtectedScope();
8878 
8879   auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
8880                                            Clauses, AStmt);
8881   if (!Pair.first && DirName.getName() && !DependentHint)
8882     DSAStack->addCriticalWithHint(Dir, Hint);
8883   return Dir;
8884 }
8885 
8886 StmtResult Sema::ActOnOpenMPParallelForDirective(
8887     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
8888     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
8889   if (!AStmt)
8890     return StmtError();
8891 
8892   auto *CS = cast<CapturedStmt>(AStmt);
8893   // 1.2.2 OpenMP Language Terminology
8894   // Structured block - An executable statement with a single entry at the
8895   // top and a single exit at the bottom.
8896   // The point of exit cannot be a branch out of the structured block.
8897   // longjmp() and throw() must not violate the entry/exit criteria.
8898   CS->getCapturedDecl()->setNothrow();
8899 
8900   OMPLoopDirective::HelperExprs B;
8901   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
8902   // define the nested loops number.
8903   unsigned NestedLoopCount =
8904       checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
8905                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
8906                       VarsWithImplicitDSA, B);
8907   if (NestedLoopCount == 0)
8908     return StmtError();
8909 
8910   assert((CurContext->isDependentContext() || B.builtAll()) &&
8911          "omp parallel for loop exprs were not built");
8912 
8913   if (!CurContext->isDependentContext()) {
8914     // Finalize the clauses that need pre-built expressions for CodeGen.
8915     for (OMPClause *C : Clauses) {
8916       if (auto *LC = dyn_cast<OMPLinearClause>(C))
8917         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8918                                      B.NumIterations, *this, CurScope,
8919                                      DSAStack))
8920           return StmtError();
8921     }
8922   }
8923 
8924   setFunctionHasBranchProtectedScope();
8925   return OMPParallelForDirective::Create(
8926       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
8927       DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
8928 }
8929 
8930 StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
8931     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
8932     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
8933   if (!AStmt)
8934     return StmtError();
8935 
8936   auto *CS = cast<CapturedStmt>(AStmt);
8937   // 1.2.2 OpenMP Language Terminology
8938   // Structured block - An executable statement with a single entry at the
8939   // top and a single exit at the bottom.
8940   // The point of exit cannot be a branch out of the structured block.
8941   // longjmp() and throw() must not violate the entry/exit criteria.
8942   CS->getCapturedDecl()->setNothrow();
8943 
8944   OMPLoopDirective::HelperExprs B;
8945   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
8946   // define the nested loops number.
8947   unsigned NestedLoopCount =
8948       checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
8949                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
8950                       VarsWithImplicitDSA, B);
8951   if (NestedLoopCount == 0)
8952     return StmtError();
8953 
8954   if (!CurContext->isDependentContext()) {
8955     // Finalize the clauses that need pre-built expressions for CodeGen.
8956     for (OMPClause *C : Clauses) {
8957       if (auto *LC = dyn_cast<OMPLinearClause>(C))
8958         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8959                                      B.NumIterations, *this, CurScope,
8960                                      DSAStack))
8961           return StmtError();
8962     }
8963   }
8964 
8965   if (checkSimdlenSafelenSpecified(*this, Clauses))
8966     return StmtError();
8967 
8968   setFunctionHasBranchProtectedScope();
8969   return OMPParallelForSimdDirective::Create(
8970       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8971 }
8972 
8973 StmtResult
8974 Sema::ActOnOpenMPParallelMasterDirective(ArrayRef<OMPClause *> Clauses,
8975                                          Stmt *AStmt, SourceLocation StartLoc,
8976                                          SourceLocation EndLoc) {
8977   if (!AStmt)
8978     return StmtError();
8979 
8980   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
8981   auto *CS = cast<CapturedStmt>(AStmt);
8982   // 1.2.2 OpenMP Language Terminology
8983   // Structured block - An executable statement with a single entry at the
8984   // top and a single exit at the bottom.
8985   // The point of exit cannot be a branch out of the structured block.
8986   // longjmp() and throw() must not violate the entry/exit criteria.
8987   CS->getCapturedDecl()->setNothrow();
8988 
8989   setFunctionHasBranchProtectedScope();
8990 
8991   return OMPParallelMasterDirective::Create(
8992       Context, StartLoc, EndLoc, Clauses, AStmt,
8993       DSAStack->getTaskgroupReductionRef());
8994 }
8995 
8996 StmtResult
8997 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
8998                                            Stmt *AStmt, SourceLocation StartLoc,
8999                                            SourceLocation EndLoc) {
9000   if (!AStmt)
9001     return StmtError();
9002 
9003   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9004   auto BaseStmt = AStmt;
9005   while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
9006     BaseStmt = CS->getCapturedStmt();
9007   if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
9008     auto S = C->children();
9009     if (S.begin() == S.end())
9010       return StmtError();
9011     // All associated statements must be '#pragma omp section' except for
9012     // the first one.
9013     for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
9014       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
9015         if (SectionStmt)
9016           Diag(SectionStmt->getBeginLoc(),
9017                diag::err_omp_parallel_sections_substmt_not_section);
9018         return StmtError();
9019       }
9020       cast<OMPSectionDirective>(SectionStmt)
9021           ->setHasCancel(DSAStack->isCancelRegion());
9022     }
9023   } else {
9024     Diag(AStmt->getBeginLoc(),
9025          diag::err_omp_parallel_sections_not_compound_stmt);
9026     return StmtError();
9027   }
9028 
9029   setFunctionHasBranchProtectedScope();
9030 
9031   return OMPParallelSectionsDirective::Create(
9032       Context, StartLoc, EndLoc, Clauses, AStmt,
9033       DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
9034 }
9035 
9036 /// detach and mergeable clauses are mutially exclusive, check for it.
9037 static bool checkDetachMergeableClauses(Sema &S,
9038                                         ArrayRef<OMPClause *> Clauses) {
9039   const OMPClause *PrevClause = nullptr;
9040   bool ErrorFound = false;
9041   for (const OMPClause *C : Clauses) {
9042     if (C->getClauseKind() == OMPC_detach ||
9043         C->getClauseKind() == OMPC_mergeable) {
9044       if (!PrevClause) {
9045         PrevClause = C;
9046       } else if (PrevClause->getClauseKind() != C->getClauseKind()) {
9047         S.Diag(C->getBeginLoc(), diag::err_omp_clauses_mutually_exclusive)
9048             << getOpenMPClauseName(C->getClauseKind())
9049             << getOpenMPClauseName(PrevClause->getClauseKind());
9050         S.Diag(PrevClause->getBeginLoc(), diag::note_omp_previous_clause)
9051             << getOpenMPClauseName(PrevClause->getClauseKind());
9052         ErrorFound = true;
9053       }
9054     }
9055   }
9056   return ErrorFound;
9057 }
9058 
9059 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
9060                                           Stmt *AStmt, SourceLocation StartLoc,
9061                                           SourceLocation EndLoc) {
9062   if (!AStmt)
9063     return StmtError();
9064 
9065   // OpenMP 5.0, 2.10.1 task Construct
9066   // If a detach clause appears on the directive, then a mergeable clause cannot
9067   // appear on the same directive.
9068   if (checkDetachMergeableClauses(*this, Clauses))
9069     return StmtError();
9070 
9071   auto *CS = cast<CapturedStmt>(AStmt);
9072   // 1.2.2 OpenMP Language Terminology
9073   // Structured block - An executable statement with a single entry at the
9074   // top and a single exit at the bottom.
9075   // The point of exit cannot be a branch out of the structured block.
9076   // longjmp() and throw() must not violate the entry/exit criteria.
9077   CS->getCapturedDecl()->setNothrow();
9078 
9079   setFunctionHasBranchProtectedScope();
9080 
9081   return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
9082                                   DSAStack->isCancelRegion());
9083 }
9084 
9085 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
9086                                                SourceLocation EndLoc) {
9087   return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
9088 }
9089 
9090 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
9091                                              SourceLocation EndLoc) {
9092   return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
9093 }
9094 
9095 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
9096                                               SourceLocation EndLoc) {
9097   return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
9098 }
9099 
9100 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
9101                                                Stmt *AStmt,
9102                                                SourceLocation StartLoc,
9103                                                SourceLocation EndLoc) {
9104   if (!AStmt)
9105     return StmtError();
9106 
9107   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9108 
9109   setFunctionHasBranchProtectedScope();
9110 
9111   return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
9112                                        AStmt,
9113                                        DSAStack->getTaskgroupReductionRef());
9114 }
9115 
9116 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
9117                                            SourceLocation StartLoc,
9118                                            SourceLocation EndLoc) {
9119   OMPFlushClause *FC = nullptr;
9120   OMPClause *OrderClause = nullptr;
9121   for (OMPClause *C : Clauses) {
9122     if (C->getClauseKind() == OMPC_flush)
9123       FC = cast<OMPFlushClause>(C);
9124     else
9125       OrderClause = C;
9126   }
9127   OpenMPClauseKind MemOrderKind = OMPC_unknown;
9128   SourceLocation MemOrderLoc;
9129   for (const OMPClause *C : Clauses) {
9130     if (C->getClauseKind() == OMPC_acq_rel ||
9131         C->getClauseKind() == OMPC_acquire ||
9132         C->getClauseKind() == OMPC_release) {
9133       if (MemOrderKind != OMPC_unknown) {
9134         Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses)
9135             << getOpenMPDirectiveName(OMPD_flush) << 1
9136             << SourceRange(C->getBeginLoc(), C->getEndLoc());
9137         Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause)
9138             << getOpenMPClauseName(MemOrderKind);
9139       } else {
9140         MemOrderKind = C->getClauseKind();
9141         MemOrderLoc = C->getBeginLoc();
9142       }
9143     }
9144   }
9145   if (FC && OrderClause) {
9146     Diag(FC->getLParenLoc(), diag::err_omp_flush_order_clause_and_list)
9147         << getOpenMPClauseName(OrderClause->getClauseKind());
9148     Diag(OrderClause->getBeginLoc(), diag::note_omp_flush_order_clause_here)
9149         << getOpenMPClauseName(OrderClause->getClauseKind());
9150     return StmtError();
9151   }
9152   return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
9153 }
9154 
9155 StmtResult Sema::ActOnOpenMPDepobjDirective(ArrayRef<OMPClause *> Clauses,
9156                                             SourceLocation StartLoc,
9157                                             SourceLocation EndLoc) {
9158   if (Clauses.empty()) {
9159     Diag(StartLoc, diag::err_omp_depobj_expected);
9160     return StmtError();
9161   } else if (Clauses[0]->getClauseKind() != OMPC_depobj) {
9162     Diag(Clauses[0]->getBeginLoc(), diag::err_omp_depobj_expected);
9163     return StmtError();
9164   }
9165   // Only depobj expression and another single clause is allowed.
9166   if (Clauses.size() > 2) {
9167     Diag(Clauses[2]->getBeginLoc(),
9168          diag::err_omp_depobj_single_clause_expected);
9169     return StmtError();
9170   } else if (Clauses.size() < 1) {
9171     Diag(Clauses[0]->getEndLoc(), diag::err_omp_depobj_single_clause_expected);
9172     return StmtError();
9173   }
9174   return OMPDepobjDirective::Create(Context, StartLoc, EndLoc, Clauses);
9175 }
9176 
9177 StmtResult Sema::ActOnOpenMPScanDirective(ArrayRef<OMPClause *> Clauses,
9178                                           SourceLocation StartLoc,
9179                                           SourceLocation EndLoc) {
9180   // Check that exactly one clause is specified.
9181   if (Clauses.size() != 1) {
9182     Diag(Clauses.empty() ? EndLoc : Clauses[1]->getBeginLoc(),
9183          diag::err_omp_scan_single_clause_expected);
9184     return StmtError();
9185   }
9186   // Check that only one instance of scan directives is used in the same outer
9187   // region.
9188   if (DSAStack->doesParentHasScanDirective()) {
9189     Diag(StartLoc, diag::err_omp_several_scan_directives_in_region);
9190     Diag(DSAStack->getParentScanDirectiveLoc(),
9191          diag::note_omp_previous_scan_directive);
9192     return StmtError();
9193   }
9194   DSAStack->setParentHasScanDirective(StartLoc);
9195   return OMPScanDirective::Create(Context, StartLoc, EndLoc, Clauses);
9196 }
9197 
9198 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
9199                                              Stmt *AStmt,
9200                                              SourceLocation StartLoc,
9201                                              SourceLocation EndLoc) {
9202   const OMPClause *DependFound = nullptr;
9203   const OMPClause *DependSourceClause = nullptr;
9204   const OMPClause *DependSinkClause = nullptr;
9205   bool ErrorFound = false;
9206   const OMPThreadsClause *TC = nullptr;
9207   const OMPSIMDClause *SC = nullptr;
9208   for (const OMPClause *C : Clauses) {
9209     if (auto *DC = dyn_cast<OMPDependClause>(C)) {
9210       DependFound = C;
9211       if (DC->getDependencyKind() == OMPC_DEPEND_source) {
9212         if (DependSourceClause) {
9213           Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
9214               << getOpenMPDirectiveName(OMPD_ordered)
9215               << getOpenMPClauseName(OMPC_depend) << 2;
9216           ErrorFound = true;
9217         } else {
9218           DependSourceClause = C;
9219         }
9220         if (DependSinkClause) {
9221           Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
9222               << 0;
9223           ErrorFound = true;
9224         }
9225       } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
9226         if (DependSourceClause) {
9227           Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
9228               << 1;
9229           ErrorFound = true;
9230         }
9231         DependSinkClause = C;
9232       }
9233     } else if (C->getClauseKind() == OMPC_threads) {
9234       TC = cast<OMPThreadsClause>(C);
9235     } else if (C->getClauseKind() == OMPC_simd) {
9236       SC = cast<OMPSIMDClause>(C);
9237     }
9238   }
9239   if (!ErrorFound && !SC &&
9240       isOpenMPSimdDirective(DSAStack->getParentDirective())) {
9241     // OpenMP [2.8.1,simd Construct, Restrictions]
9242     // An ordered construct with the simd clause is the only OpenMP construct
9243     // that can appear in the simd region.
9244     Diag(StartLoc, diag::err_omp_prohibited_region_simd)
9245         << (LangOpts.OpenMP >= 50 ? 1 : 0);
9246     ErrorFound = true;
9247   } else if (DependFound && (TC || SC)) {
9248     Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd)
9249         << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
9250     ErrorFound = true;
9251   } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) {
9252     Diag(DependFound->getBeginLoc(),
9253          diag::err_omp_ordered_directive_without_param);
9254     ErrorFound = true;
9255   } else if (TC || Clauses.empty()) {
9256     if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
9257       SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
9258       Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
9259           << (TC != nullptr);
9260       Diag(Param->getBeginLoc(), diag::note_omp_ordered_param) << 1;
9261       ErrorFound = true;
9262     }
9263   }
9264   if ((!AStmt && !DependFound) || ErrorFound)
9265     return StmtError();
9266 
9267   if (AStmt) {
9268     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
9269 
9270     setFunctionHasBranchProtectedScope();
9271   }
9272 
9273   return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
9274 }
9275 
9276 namespace {
9277 /// Helper class for checking expression in 'omp atomic [update]'
9278 /// construct.
9279 class OpenMPAtomicUpdateChecker {
9280   /// Error results for atomic update expressions.
9281   enum ExprAnalysisErrorCode {
9282     /// A statement is not an expression statement.
9283     NotAnExpression,
9284     /// Expression is not builtin binary or unary operation.
9285     NotABinaryOrUnaryExpression,
9286     /// Unary operation is not post-/pre- increment/decrement operation.
9287     NotAnUnaryIncDecExpression,
9288     /// An expression is not of scalar type.
9289     NotAScalarType,
9290     /// A binary operation is not an assignment operation.
9291     NotAnAssignmentOp,
9292     /// RHS part of the binary operation is not a binary expression.
9293     NotABinaryExpression,
9294     /// RHS part is not additive/multiplicative/shift/biwise binary
9295     /// expression.
9296     NotABinaryOperator,
9297     /// RHS binary operation does not have reference to the updated LHS
9298     /// part.
9299     NotAnUpdateExpression,
9300     /// No errors is found.
9301     NoError
9302   };
9303   /// Reference to Sema.
9304   Sema &SemaRef;
9305   /// A location for note diagnostics (when error is found).
9306   SourceLocation NoteLoc;
9307   /// 'x' lvalue part of the source atomic expression.
9308   Expr *X;
9309   /// 'expr' rvalue part of the source atomic expression.
9310   Expr *E;
9311   /// Helper expression of the form
9312   /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
9313   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
9314   Expr *UpdateExpr;
9315   /// Is 'x' a LHS in a RHS part of full update expression. It is
9316   /// important for non-associative operations.
9317   bool IsXLHSInRHSPart;
9318   BinaryOperatorKind Op;
9319   SourceLocation OpLoc;
9320   /// true if the source expression is a postfix unary operation, false
9321   /// if it is a prefix unary operation.
9322   bool IsPostfixUpdate;
9323 
9324 public:
9325   OpenMPAtomicUpdateChecker(Sema &SemaRef)
9326       : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
9327         IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
9328   /// Check specified statement that it is suitable for 'atomic update'
9329   /// constructs and extract 'x', 'expr' and Operation from the original
9330   /// expression. If DiagId and NoteId == 0, then only check is performed
9331   /// without error notification.
9332   /// \param DiagId Diagnostic which should be emitted if error is found.
9333   /// \param NoteId Diagnostic note for the main error message.
9334   /// \return true if statement is not an update expression, false otherwise.
9335   bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
9336   /// Return the 'x' lvalue part of the source atomic expression.
9337   Expr *getX() const { return X; }
9338   /// Return the 'expr' rvalue part of the source atomic expression.
9339   Expr *getExpr() const { return E; }
9340   /// Return the update expression used in calculation of the updated
9341   /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
9342   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
9343   Expr *getUpdateExpr() const { return UpdateExpr; }
9344   /// Return true if 'x' is LHS in RHS part of full update expression,
9345   /// false otherwise.
9346   bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
9347 
9348   /// true if the source expression is a postfix unary operation, false
9349   /// if it is a prefix unary operation.
9350   bool isPostfixUpdate() const { return IsPostfixUpdate; }
9351 
9352 private:
9353   bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
9354                             unsigned NoteId = 0);
9355 };
9356 } // namespace
9357 
9358 bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
9359     BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
9360   ExprAnalysisErrorCode ErrorFound = NoError;
9361   SourceLocation ErrorLoc, NoteLoc;
9362   SourceRange ErrorRange, NoteRange;
9363   // Allowed constructs are:
9364   //  x = x binop expr;
9365   //  x = expr binop x;
9366   if (AtomicBinOp->getOpcode() == BO_Assign) {
9367     X = AtomicBinOp->getLHS();
9368     if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
9369             AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
9370       if (AtomicInnerBinOp->isMultiplicativeOp() ||
9371           AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
9372           AtomicInnerBinOp->isBitwiseOp()) {
9373         Op = AtomicInnerBinOp->getOpcode();
9374         OpLoc = AtomicInnerBinOp->getOperatorLoc();
9375         Expr *LHS = AtomicInnerBinOp->getLHS();
9376         Expr *RHS = AtomicInnerBinOp->getRHS();
9377         llvm::FoldingSetNodeID XId, LHSId, RHSId;
9378         X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
9379                                           /*Canonical=*/true);
9380         LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
9381                                             /*Canonical=*/true);
9382         RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
9383                                             /*Canonical=*/true);
9384         if (XId == LHSId) {
9385           E = RHS;
9386           IsXLHSInRHSPart = true;
9387         } else if (XId == RHSId) {
9388           E = LHS;
9389           IsXLHSInRHSPart = false;
9390         } else {
9391           ErrorLoc = AtomicInnerBinOp->getExprLoc();
9392           ErrorRange = AtomicInnerBinOp->getSourceRange();
9393           NoteLoc = X->getExprLoc();
9394           NoteRange = X->getSourceRange();
9395           ErrorFound = NotAnUpdateExpression;
9396         }
9397       } else {
9398         ErrorLoc = AtomicInnerBinOp->getExprLoc();
9399         ErrorRange = AtomicInnerBinOp->getSourceRange();
9400         NoteLoc = AtomicInnerBinOp->getOperatorLoc();
9401         NoteRange = SourceRange(NoteLoc, NoteLoc);
9402         ErrorFound = NotABinaryOperator;
9403       }
9404     } else {
9405       NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
9406       NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
9407       ErrorFound = NotABinaryExpression;
9408     }
9409   } else {
9410     ErrorLoc = AtomicBinOp->getExprLoc();
9411     ErrorRange = AtomicBinOp->getSourceRange();
9412     NoteLoc = AtomicBinOp->getOperatorLoc();
9413     NoteRange = SourceRange(NoteLoc, NoteLoc);
9414     ErrorFound = NotAnAssignmentOp;
9415   }
9416   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
9417     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
9418     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
9419     return true;
9420   }
9421   if (SemaRef.CurContext->isDependentContext())
9422     E = X = UpdateExpr = nullptr;
9423   return ErrorFound != NoError;
9424 }
9425 
9426 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
9427                                                unsigned NoteId) {
9428   ExprAnalysisErrorCode ErrorFound = NoError;
9429   SourceLocation ErrorLoc, NoteLoc;
9430   SourceRange ErrorRange, NoteRange;
9431   // Allowed constructs are:
9432   //  x++;
9433   //  x--;
9434   //  ++x;
9435   //  --x;
9436   //  x binop= expr;
9437   //  x = x binop expr;
9438   //  x = expr binop x;
9439   if (auto *AtomicBody = dyn_cast<Expr>(S)) {
9440     AtomicBody = AtomicBody->IgnoreParenImpCasts();
9441     if (AtomicBody->getType()->isScalarType() ||
9442         AtomicBody->isInstantiationDependent()) {
9443       if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
9444               AtomicBody->IgnoreParenImpCasts())) {
9445         // Check for Compound Assignment Operation
9446         Op = BinaryOperator::getOpForCompoundAssignment(
9447             AtomicCompAssignOp->getOpcode());
9448         OpLoc = AtomicCompAssignOp->getOperatorLoc();
9449         E = AtomicCompAssignOp->getRHS();
9450         X = AtomicCompAssignOp->getLHS()->IgnoreParens();
9451         IsXLHSInRHSPart = true;
9452       } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
9453                      AtomicBody->IgnoreParenImpCasts())) {
9454         // Check for Binary Operation
9455         if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
9456           return true;
9457       } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
9458                      AtomicBody->IgnoreParenImpCasts())) {
9459         // Check for Unary Operation
9460         if (AtomicUnaryOp->isIncrementDecrementOp()) {
9461           IsPostfixUpdate = AtomicUnaryOp->isPostfix();
9462           Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
9463           OpLoc = AtomicUnaryOp->getOperatorLoc();
9464           X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
9465           E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
9466           IsXLHSInRHSPart = true;
9467         } else {
9468           ErrorFound = NotAnUnaryIncDecExpression;
9469           ErrorLoc = AtomicUnaryOp->getExprLoc();
9470           ErrorRange = AtomicUnaryOp->getSourceRange();
9471           NoteLoc = AtomicUnaryOp->getOperatorLoc();
9472           NoteRange = SourceRange(NoteLoc, NoteLoc);
9473         }
9474       } else if (!AtomicBody->isInstantiationDependent()) {
9475         ErrorFound = NotABinaryOrUnaryExpression;
9476         NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
9477         NoteRange = ErrorRange = AtomicBody->getSourceRange();
9478       }
9479     } else {
9480       ErrorFound = NotAScalarType;
9481       NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
9482       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
9483     }
9484   } else {
9485     ErrorFound = NotAnExpression;
9486     NoteLoc = ErrorLoc = S->getBeginLoc();
9487     NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
9488   }
9489   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
9490     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
9491     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
9492     return true;
9493   }
9494   if (SemaRef.CurContext->isDependentContext())
9495     E = X = UpdateExpr = nullptr;
9496   if (ErrorFound == NoError && E && X) {
9497     // Build an update expression of form 'OpaqueValueExpr(x) binop
9498     // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
9499     // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
9500     auto *OVEX = new (SemaRef.getASTContext())
9501         OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
9502     auto *OVEExpr = new (SemaRef.getASTContext())
9503         OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
9504     ExprResult Update =
9505         SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
9506                                    IsXLHSInRHSPart ? OVEExpr : OVEX);
9507     if (Update.isInvalid())
9508       return true;
9509     Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
9510                                                Sema::AA_Casting);
9511     if (Update.isInvalid())
9512       return true;
9513     UpdateExpr = Update.get();
9514   }
9515   return ErrorFound != NoError;
9516 }
9517 
9518 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
9519                                             Stmt *AStmt,
9520                                             SourceLocation StartLoc,
9521                                             SourceLocation EndLoc) {
9522   // Register location of the first atomic directive.
9523   DSAStack->addAtomicDirectiveLoc(StartLoc);
9524   if (!AStmt)
9525     return StmtError();
9526 
9527   auto *CS = cast<CapturedStmt>(AStmt);
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   OpenMPClauseKind AtomicKind = OMPC_unknown;
9534   SourceLocation AtomicKindLoc;
9535   OpenMPClauseKind MemOrderKind = OMPC_unknown;
9536   SourceLocation MemOrderLoc;
9537   for (const OMPClause *C : Clauses) {
9538     if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
9539         C->getClauseKind() == OMPC_update ||
9540         C->getClauseKind() == OMPC_capture) {
9541       if (AtomicKind != OMPC_unknown) {
9542         Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses)
9543             << SourceRange(C->getBeginLoc(), C->getEndLoc());
9544         Diag(AtomicKindLoc, diag::note_omp_previous_mem_order_clause)
9545             << getOpenMPClauseName(AtomicKind);
9546       } else {
9547         AtomicKind = C->getClauseKind();
9548         AtomicKindLoc = C->getBeginLoc();
9549       }
9550     }
9551     if (C->getClauseKind() == OMPC_seq_cst ||
9552         C->getClauseKind() == OMPC_acq_rel ||
9553         C->getClauseKind() == OMPC_acquire ||
9554         C->getClauseKind() == OMPC_release ||
9555         C->getClauseKind() == OMPC_relaxed) {
9556       if (MemOrderKind != OMPC_unknown) {
9557         Diag(C->getBeginLoc(), diag::err_omp_several_mem_order_clauses)
9558             << getOpenMPDirectiveName(OMPD_atomic) << 0
9559             << SourceRange(C->getBeginLoc(), C->getEndLoc());
9560         Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause)
9561             << getOpenMPClauseName(MemOrderKind);
9562       } else {
9563         MemOrderKind = C->getClauseKind();
9564         MemOrderLoc = C->getBeginLoc();
9565       }
9566     }
9567   }
9568   // OpenMP 5.0, 2.17.7 atomic Construct, Restrictions
9569   // If atomic-clause is read then memory-order-clause must not be acq_rel or
9570   // release.
9571   // If atomic-clause is write then memory-order-clause must not be acq_rel or
9572   // acquire.
9573   // If atomic-clause is update or not present then memory-order-clause must not
9574   // be acq_rel or acquire.
9575   if ((AtomicKind == OMPC_read &&
9576        (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_release)) ||
9577       ((AtomicKind == OMPC_write || AtomicKind == OMPC_update ||
9578         AtomicKind == OMPC_unknown) &&
9579        (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_acquire))) {
9580     SourceLocation Loc = AtomicKindLoc;
9581     if (AtomicKind == OMPC_unknown)
9582       Loc = StartLoc;
9583     Diag(Loc, diag::err_omp_atomic_incompatible_mem_order_clause)
9584         << getOpenMPClauseName(AtomicKind)
9585         << (AtomicKind == OMPC_unknown ? 1 : 0)
9586         << getOpenMPClauseName(MemOrderKind);
9587     Diag(MemOrderLoc, diag::note_omp_previous_mem_order_clause)
9588         << getOpenMPClauseName(MemOrderKind);
9589   }
9590 
9591   Stmt *Body = CS->getCapturedStmt();
9592   if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
9593     Body = EWC->getSubExpr();
9594 
9595   Expr *X = nullptr;
9596   Expr *V = nullptr;
9597   Expr *E = nullptr;
9598   Expr *UE = nullptr;
9599   bool IsXLHSInRHSPart = false;
9600   bool IsPostfixUpdate = false;
9601   // OpenMP [2.12.6, atomic Construct]
9602   // In the next expressions:
9603   // * x and v (as applicable) are both l-value expressions with scalar type.
9604   // * During the execution of an atomic region, multiple syntactic
9605   // occurrences of x must designate the same storage location.
9606   // * Neither of v and expr (as applicable) may access the storage location
9607   // designated by x.
9608   // * Neither of x and expr (as applicable) may access the storage location
9609   // designated by v.
9610   // * expr is an expression with scalar type.
9611   // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
9612   // * binop, binop=, ++, and -- are not overloaded operators.
9613   // * The expression x binop expr must be numerically equivalent to x binop
9614   // (expr). This requirement is satisfied if the operators in expr have
9615   // precedence greater than binop, or by using parentheses around expr or
9616   // subexpressions of expr.
9617   // * The expression expr binop x must be numerically equivalent to (expr)
9618   // binop x. This requirement is satisfied if the operators in expr have
9619   // precedence equal to or greater than binop, or by using parentheses around
9620   // expr or subexpressions of expr.
9621   // * For forms that allow multiple occurrences of x, the number of times
9622   // that x is evaluated is unspecified.
9623   if (AtomicKind == OMPC_read) {
9624     enum {
9625       NotAnExpression,
9626       NotAnAssignmentOp,
9627       NotAScalarType,
9628       NotAnLValue,
9629       NoError
9630     } ErrorFound = NoError;
9631     SourceLocation ErrorLoc, NoteLoc;
9632     SourceRange ErrorRange, NoteRange;
9633     // If clause is read:
9634     //  v = x;
9635     if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
9636       const auto *AtomicBinOp =
9637           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
9638       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
9639         X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
9640         V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
9641         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
9642             (V->isInstantiationDependent() || V->getType()->isScalarType())) {
9643           if (!X->isLValue() || !V->isLValue()) {
9644             const Expr *NotLValueExpr = X->isLValue() ? V : X;
9645             ErrorFound = NotAnLValue;
9646             ErrorLoc = AtomicBinOp->getExprLoc();
9647             ErrorRange = AtomicBinOp->getSourceRange();
9648             NoteLoc = NotLValueExpr->getExprLoc();
9649             NoteRange = NotLValueExpr->getSourceRange();
9650           }
9651         } else if (!X->isInstantiationDependent() ||
9652                    !V->isInstantiationDependent()) {
9653           const Expr *NotScalarExpr =
9654               (X->isInstantiationDependent() || X->getType()->isScalarType())
9655                   ? V
9656                   : X;
9657           ErrorFound = NotAScalarType;
9658           ErrorLoc = AtomicBinOp->getExprLoc();
9659           ErrorRange = AtomicBinOp->getSourceRange();
9660           NoteLoc = NotScalarExpr->getExprLoc();
9661           NoteRange = NotScalarExpr->getSourceRange();
9662         }
9663       } else if (!AtomicBody->isInstantiationDependent()) {
9664         ErrorFound = NotAnAssignmentOp;
9665         ErrorLoc = AtomicBody->getExprLoc();
9666         ErrorRange = AtomicBody->getSourceRange();
9667         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
9668                               : AtomicBody->getExprLoc();
9669         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
9670                                 : AtomicBody->getSourceRange();
9671       }
9672     } else {
9673       ErrorFound = NotAnExpression;
9674       NoteLoc = ErrorLoc = Body->getBeginLoc();
9675       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
9676     }
9677     if (ErrorFound != NoError) {
9678       Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
9679           << ErrorRange;
9680       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
9681                                                       << NoteRange;
9682       return StmtError();
9683     }
9684     if (CurContext->isDependentContext())
9685       V = X = nullptr;
9686   } else if (AtomicKind == OMPC_write) {
9687     enum {
9688       NotAnExpression,
9689       NotAnAssignmentOp,
9690       NotAScalarType,
9691       NotAnLValue,
9692       NoError
9693     } ErrorFound = NoError;
9694     SourceLocation ErrorLoc, NoteLoc;
9695     SourceRange ErrorRange, NoteRange;
9696     // If clause is write:
9697     //  x = expr;
9698     if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
9699       const auto *AtomicBinOp =
9700           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
9701       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
9702         X = AtomicBinOp->getLHS();
9703         E = AtomicBinOp->getRHS();
9704         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
9705             (E->isInstantiationDependent() || E->getType()->isScalarType())) {
9706           if (!X->isLValue()) {
9707             ErrorFound = NotAnLValue;
9708             ErrorLoc = AtomicBinOp->getExprLoc();
9709             ErrorRange = AtomicBinOp->getSourceRange();
9710             NoteLoc = X->getExprLoc();
9711             NoteRange = X->getSourceRange();
9712           }
9713         } else if (!X->isInstantiationDependent() ||
9714                    !E->isInstantiationDependent()) {
9715           const Expr *NotScalarExpr =
9716               (X->isInstantiationDependent() || X->getType()->isScalarType())
9717                   ? E
9718                   : X;
9719           ErrorFound = NotAScalarType;
9720           ErrorLoc = AtomicBinOp->getExprLoc();
9721           ErrorRange = AtomicBinOp->getSourceRange();
9722           NoteLoc = NotScalarExpr->getExprLoc();
9723           NoteRange = NotScalarExpr->getSourceRange();
9724         }
9725       } else if (!AtomicBody->isInstantiationDependent()) {
9726         ErrorFound = NotAnAssignmentOp;
9727         ErrorLoc = AtomicBody->getExprLoc();
9728         ErrorRange = AtomicBody->getSourceRange();
9729         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
9730                               : AtomicBody->getExprLoc();
9731         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
9732                                 : AtomicBody->getSourceRange();
9733       }
9734     } else {
9735       ErrorFound = NotAnExpression;
9736       NoteLoc = ErrorLoc = Body->getBeginLoc();
9737       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
9738     }
9739     if (ErrorFound != NoError) {
9740       Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
9741           << ErrorRange;
9742       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
9743                                                       << NoteRange;
9744       return StmtError();
9745     }
9746     if (CurContext->isDependentContext())
9747       E = X = nullptr;
9748   } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
9749     // If clause is update:
9750     //  x++;
9751     //  x--;
9752     //  ++x;
9753     //  --x;
9754     //  x binop= expr;
9755     //  x = x binop expr;
9756     //  x = expr binop x;
9757     OpenMPAtomicUpdateChecker Checker(*this);
9758     if (Checker.checkStatement(
9759             Body, (AtomicKind == OMPC_update)
9760                       ? diag::err_omp_atomic_update_not_expression_statement
9761                       : diag::err_omp_atomic_not_expression_statement,
9762             diag::note_omp_atomic_update))
9763       return StmtError();
9764     if (!CurContext->isDependentContext()) {
9765       E = Checker.getExpr();
9766       X = Checker.getX();
9767       UE = Checker.getUpdateExpr();
9768       IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
9769     }
9770   } else if (AtomicKind == OMPC_capture) {
9771     enum {
9772       NotAnAssignmentOp,
9773       NotACompoundStatement,
9774       NotTwoSubstatements,
9775       NotASpecificExpression,
9776       NoError
9777     } ErrorFound = NoError;
9778     SourceLocation ErrorLoc, NoteLoc;
9779     SourceRange ErrorRange, NoteRange;
9780     if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
9781       // If clause is a capture:
9782       //  v = x++;
9783       //  v = x--;
9784       //  v = ++x;
9785       //  v = --x;
9786       //  v = x binop= expr;
9787       //  v = x = x binop expr;
9788       //  v = x = expr binop x;
9789       const auto *AtomicBinOp =
9790           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
9791       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
9792         V = AtomicBinOp->getLHS();
9793         Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
9794         OpenMPAtomicUpdateChecker Checker(*this);
9795         if (Checker.checkStatement(
9796                 Body, diag::err_omp_atomic_capture_not_expression_statement,
9797                 diag::note_omp_atomic_update))
9798           return StmtError();
9799         E = Checker.getExpr();
9800         X = Checker.getX();
9801         UE = Checker.getUpdateExpr();
9802         IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
9803         IsPostfixUpdate = Checker.isPostfixUpdate();
9804       } else if (!AtomicBody->isInstantiationDependent()) {
9805         ErrorLoc = AtomicBody->getExprLoc();
9806         ErrorRange = AtomicBody->getSourceRange();
9807         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
9808                               : AtomicBody->getExprLoc();
9809         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
9810                                 : AtomicBody->getSourceRange();
9811         ErrorFound = NotAnAssignmentOp;
9812       }
9813       if (ErrorFound != NoError) {
9814         Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
9815             << ErrorRange;
9816         Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
9817         return StmtError();
9818       }
9819       if (CurContext->isDependentContext())
9820         UE = V = E = X = nullptr;
9821     } else {
9822       // If clause is a capture:
9823       //  { v = x; x = expr; }
9824       //  { v = x; x++; }
9825       //  { v = x; x--; }
9826       //  { v = x; ++x; }
9827       //  { v = x; --x; }
9828       //  { v = x; x binop= expr; }
9829       //  { v = x; x = x binop expr; }
9830       //  { v = x; x = expr binop x; }
9831       //  { x++; v = x; }
9832       //  { x--; v = x; }
9833       //  { ++x; v = x; }
9834       //  { --x; v = x; }
9835       //  { x binop= expr; v = x; }
9836       //  { x = x binop expr; v = x; }
9837       //  { x = expr binop x; v = x; }
9838       if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
9839         // Check that this is { expr1; expr2; }
9840         if (CS->size() == 2) {
9841           Stmt *First = CS->body_front();
9842           Stmt *Second = CS->body_back();
9843           if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
9844             First = EWC->getSubExpr()->IgnoreParenImpCasts();
9845           if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
9846             Second = EWC->getSubExpr()->IgnoreParenImpCasts();
9847           // Need to find what subexpression is 'v' and what is 'x'.
9848           OpenMPAtomicUpdateChecker Checker(*this);
9849           bool IsUpdateExprFound = !Checker.checkStatement(Second);
9850           BinaryOperator *BinOp = nullptr;
9851           if (IsUpdateExprFound) {
9852             BinOp = dyn_cast<BinaryOperator>(First);
9853             IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
9854           }
9855           if (IsUpdateExprFound && !CurContext->isDependentContext()) {
9856             //  { v = x; x++; }
9857             //  { v = x; x--; }
9858             //  { v = x; ++x; }
9859             //  { v = x; --x; }
9860             //  { v = x; x binop= expr; }
9861             //  { v = x; x = x binop expr; }
9862             //  { v = x; x = expr binop x; }
9863             // Check that the first expression has form v = x.
9864             Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
9865             llvm::FoldingSetNodeID XId, PossibleXId;
9866             Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
9867             PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
9868             IsUpdateExprFound = XId == PossibleXId;
9869             if (IsUpdateExprFound) {
9870               V = BinOp->getLHS();
9871               X = Checker.getX();
9872               E = Checker.getExpr();
9873               UE = Checker.getUpdateExpr();
9874               IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
9875               IsPostfixUpdate = true;
9876             }
9877           }
9878           if (!IsUpdateExprFound) {
9879             IsUpdateExprFound = !Checker.checkStatement(First);
9880             BinOp = nullptr;
9881             if (IsUpdateExprFound) {
9882               BinOp = dyn_cast<BinaryOperator>(Second);
9883               IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
9884             }
9885             if (IsUpdateExprFound && !CurContext->isDependentContext()) {
9886               //  { x++; v = x; }
9887               //  { x--; v = x; }
9888               //  { ++x; v = x; }
9889               //  { --x; v = x; }
9890               //  { x binop= expr; v = x; }
9891               //  { x = x binop expr; v = x; }
9892               //  { x = expr binop x; v = x; }
9893               // Check that the second expression has form v = x.
9894               Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
9895               llvm::FoldingSetNodeID XId, PossibleXId;
9896               Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
9897               PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
9898               IsUpdateExprFound = XId == PossibleXId;
9899               if (IsUpdateExprFound) {
9900                 V = BinOp->getLHS();
9901                 X = Checker.getX();
9902                 E = Checker.getExpr();
9903                 UE = Checker.getUpdateExpr();
9904                 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
9905                 IsPostfixUpdate = false;
9906               }
9907             }
9908           }
9909           if (!IsUpdateExprFound) {
9910             //  { v = x; x = expr; }
9911             auto *FirstExpr = dyn_cast<Expr>(First);
9912             auto *SecondExpr = dyn_cast<Expr>(Second);
9913             if (!FirstExpr || !SecondExpr ||
9914                 !(FirstExpr->isInstantiationDependent() ||
9915                   SecondExpr->isInstantiationDependent())) {
9916               auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
9917               if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
9918                 ErrorFound = NotAnAssignmentOp;
9919                 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
9920                                                 : First->getBeginLoc();
9921                 NoteRange = ErrorRange = FirstBinOp
9922                                              ? FirstBinOp->getSourceRange()
9923                                              : SourceRange(ErrorLoc, ErrorLoc);
9924               } else {
9925                 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
9926                 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
9927                   ErrorFound = NotAnAssignmentOp;
9928                   NoteLoc = ErrorLoc = SecondBinOp
9929                                            ? SecondBinOp->getOperatorLoc()
9930                                            : Second->getBeginLoc();
9931                   NoteRange = ErrorRange =
9932                       SecondBinOp ? SecondBinOp->getSourceRange()
9933                                   : SourceRange(ErrorLoc, ErrorLoc);
9934                 } else {
9935                   Expr *PossibleXRHSInFirst =
9936                       FirstBinOp->getRHS()->IgnoreParenImpCasts();
9937                   Expr *PossibleXLHSInSecond =
9938                       SecondBinOp->getLHS()->IgnoreParenImpCasts();
9939                   llvm::FoldingSetNodeID X1Id, X2Id;
9940                   PossibleXRHSInFirst->Profile(X1Id, Context,
9941                                                /*Canonical=*/true);
9942                   PossibleXLHSInSecond->Profile(X2Id, Context,
9943                                                 /*Canonical=*/true);
9944                   IsUpdateExprFound = X1Id == X2Id;
9945                   if (IsUpdateExprFound) {
9946                     V = FirstBinOp->getLHS();
9947                     X = SecondBinOp->getLHS();
9948                     E = SecondBinOp->getRHS();
9949                     UE = nullptr;
9950                     IsXLHSInRHSPart = false;
9951                     IsPostfixUpdate = true;
9952                   } else {
9953                     ErrorFound = NotASpecificExpression;
9954                     ErrorLoc = FirstBinOp->getExprLoc();
9955                     ErrorRange = FirstBinOp->getSourceRange();
9956                     NoteLoc = SecondBinOp->getLHS()->getExprLoc();
9957                     NoteRange = SecondBinOp->getRHS()->getSourceRange();
9958                   }
9959                 }
9960               }
9961             }
9962           }
9963         } else {
9964           NoteLoc = ErrorLoc = Body->getBeginLoc();
9965           NoteRange = ErrorRange =
9966               SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
9967           ErrorFound = NotTwoSubstatements;
9968         }
9969       } else {
9970         NoteLoc = ErrorLoc = Body->getBeginLoc();
9971         NoteRange = ErrorRange =
9972             SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
9973         ErrorFound = NotACompoundStatement;
9974       }
9975       if (ErrorFound != NoError) {
9976         Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
9977             << ErrorRange;
9978         Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
9979         return StmtError();
9980       }
9981       if (CurContext->isDependentContext())
9982         UE = V = E = X = nullptr;
9983     }
9984   }
9985 
9986   setFunctionHasBranchProtectedScope();
9987 
9988   return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
9989                                     X, V, E, UE, IsXLHSInRHSPart,
9990                                     IsPostfixUpdate);
9991 }
9992 
9993 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
9994                                             Stmt *AStmt,
9995                                             SourceLocation StartLoc,
9996                                             SourceLocation EndLoc) {
9997   if (!AStmt)
9998     return StmtError();
9999 
10000   auto *CS = cast<CapturedStmt>(AStmt);
10001   // 1.2.2 OpenMP Language Terminology
10002   // Structured block - An executable statement with a single entry at the
10003   // top and a single exit at the bottom.
10004   // The point of exit cannot be a branch out of the structured block.
10005   // longjmp() and throw() must not violate the entry/exit criteria.
10006   CS->getCapturedDecl()->setNothrow();
10007   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target);
10008        ThisCaptureLevel > 1; --ThisCaptureLevel) {
10009     CS = cast<CapturedStmt>(CS->getCapturedStmt());
10010     // 1.2.2 OpenMP Language Terminology
10011     // Structured block - An executable statement with a single entry at the
10012     // top and a single exit at the bottom.
10013     // The point of exit cannot be a branch out of the structured block.
10014     // longjmp() and throw() must not violate the entry/exit criteria.
10015     CS->getCapturedDecl()->setNothrow();
10016   }
10017 
10018   // OpenMP [2.16, Nesting of Regions]
10019   // If specified, a teams construct must be contained within a target
10020   // construct. That target construct must contain no statements or directives
10021   // outside of the teams construct.
10022   if (DSAStack->hasInnerTeamsRegion()) {
10023     const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
10024     bool OMPTeamsFound = true;
10025     if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
10026       auto I = CS->body_begin();
10027       while (I != CS->body_end()) {
10028         const auto *OED = dyn_cast<OMPExecutableDirective>(*I);
10029         if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) ||
10030             OMPTeamsFound) {
10031 
10032           OMPTeamsFound = false;
10033           break;
10034         }
10035         ++I;
10036       }
10037       assert(I != CS->body_end() && "Not found statement");
10038       S = *I;
10039     } else {
10040       const auto *OED = dyn_cast<OMPExecutableDirective>(S);
10041       OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
10042     }
10043     if (!OMPTeamsFound) {
10044       Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
10045       Diag(DSAStack->getInnerTeamsRegionLoc(),
10046            diag::note_omp_nested_teams_construct_here);
10047       Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here)
10048           << isa<OMPExecutableDirective>(S);
10049       return StmtError();
10050     }
10051   }
10052 
10053   setFunctionHasBranchProtectedScope();
10054 
10055   return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
10056 }
10057 
10058 StmtResult
10059 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
10060                                          Stmt *AStmt, SourceLocation StartLoc,
10061                                          SourceLocation EndLoc) {
10062   if (!AStmt)
10063     return StmtError();
10064 
10065   auto *CS = cast<CapturedStmt>(AStmt);
10066   // 1.2.2 OpenMP Language Terminology
10067   // Structured block - An executable statement with a single entry at the
10068   // top and a single exit at the bottom.
10069   // The point of exit cannot be a branch out of the structured block.
10070   // longjmp() and throw() must not violate the entry/exit criteria.
10071   CS->getCapturedDecl()->setNothrow();
10072   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel);
10073        ThisCaptureLevel > 1; --ThisCaptureLevel) {
10074     CS = cast<CapturedStmt>(CS->getCapturedStmt());
10075     // 1.2.2 OpenMP Language Terminology
10076     // Structured block - An executable statement with a single entry at the
10077     // top and a single exit at the bottom.
10078     // The point of exit cannot be a branch out of the structured block.
10079     // longjmp() and throw() must not violate the entry/exit criteria.
10080     CS->getCapturedDecl()->setNothrow();
10081   }
10082 
10083   setFunctionHasBranchProtectedScope();
10084 
10085   return OMPTargetParallelDirective::Create(
10086       Context, StartLoc, EndLoc, Clauses, AStmt,
10087       DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
10088 }
10089 
10090 StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
10091     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10092     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10093   if (!AStmt)
10094     return StmtError();
10095 
10096   auto *CS = cast<CapturedStmt>(AStmt);
10097   // 1.2.2 OpenMP Language Terminology
10098   // Structured block - An executable statement with a single entry at the
10099   // top and a single exit at the bottom.
10100   // The point of exit cannot be a branch out of the structured block.
10101   // longjmp() and throw() must not violate the entry/exit criteria.
10102   CS->getCapturedDecl()->setNothrow();
10103   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
10104        ThisCaptureLevel > 1; --ThisCaptureLevel) {
10105     CS = cast<CapturedStmt>(CS->getCapturedStmt());
10106     // 1.2.2 OpenMP Language Terminology
10107     // Structured block - An executable statement with a single entry at the
10108     // top and a single exit at the bottom.
10109     // The point of exit cannot be a branch out of the structured block.
10110     // longjmp() and throw() must not violate the entry/exit criteria.
10111     CS->getCapturedDecl()->setNothrow();
10112   }
10113 
10114   OMPLoopDirective::HelperExprs B;
10115   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
10116   // define the nested loops number.
10117   unsigned NestedLoopCount =
10118       checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
10119                       getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
10120                       VarsWithImplicitDSA, B);
10121   if (NestedLoopCount == 0)
10122     return StmtError();
10123 
10124   assert((CurContext->isDependentContext() || B.builtAll()) &&
10125          "omp target parallel for loop exprs were not built");
10126 
10127   if (!CurContext->isDependentContext()) {
10128     // Finalize the clauses that need pre-built expressions for CodeGen.
10129     for (OMPClause *C : Clauses) {
10130       if (auto *LC = dyn_cast<OMPLinearClause>(C))
10131         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
10132                                      B.NumIterations, *this, CurScope,
10133                                      DSAStack))
10134           return StmtError();
10135     }
10136   }
10137 
10138   setFunctionHasBranchProtectedScope();
10139   return OMPTargetParallelForDirective::Create(
10140       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
10141       DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
10142 }
10143 
10144 /// Check for existence of a map clause in the list of clauses.
10145 static bool hasClauses(ArrayRef<OMPClause *> Clauses,
10146                        const OpenMPClauseKind K) {
10147   return llvm::any_of(
10148       Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
10149 }
10150 
10151 template <typename... Params>
10152 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
10153                        const Params... ClauseTypes) {
10154   return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
10155 }
10156 
10157 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
10158                                                 Stmt *AStmt,
10159                                                 SourceLocation StartLoc,
10160                                                 SourceLocation EndLoc) {
10161   if (!AStmt)
10162     return StmtError();
10163 
10164   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10165 
10166   // OpenMP [2.10.1, Restrictions, p. 97]
10167   // At least one map clause must appear on the directive.
10168   if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
10169     Diag(StartLoc, diag::err_omp_no_clause_for_directive)
10170         << "'map' or 'use_device_ptr'"
10171         << getOpenMPDirectiveName(OMPD_target_data);
10172     return StmtError();
10173   }
10174 
10175   setFunctionHasBranchProtectedScope();
10176 
10177   return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
10178                                         AStmt);
10179 }
10180 
10181 StmtResult
10182 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
10183                                           SourceLocation StartLoc,
10184                                           SourceLocation EndLoc, Stmt *AStmt) {
10185   if (!AStmt)
10186     return StmtError();
10187 
10188   auto *CS = cast<CapturedStmt>(AStmt);
10189   // 1.2.2 OpenMP Language Terminology
10190   // Structured block - An executable statement with a single entry at the
10191   // top and a single exit at the bottom.
10192   // The point of exit cannot be a branch out of the structured block.
10193   // longjmp() and throw() must not violate the entry/exit criteria.
10194   CS->getCapturedDecl()->setNothrow();
10195   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
10196        ThisCaptureLevel > 1; --ThisCaptureLevel) {
10197     CS = cast<CapturedStmt>(CS->getCapturedStmt());
10198     // 1.2.2 OpenMP Language Terminology
10199     // Structured block - An executable statement with a single entry at the
10200     // top and a single exit at the bottom.
10201     // The point of exit cannot be a branch out of the structured block.
10202     // longjmp() and throw() must not violate the entry/exit criteria.
10203     CS->getCapturedDecl()->setNothrow();
10204   }
10205 
10206   // OpenMP [2.10.2, Restrictions, p. 99]
10207   // At least one map clause must appear on the directive.
10208   if (!hasClauses(Clauses, OMPC_map)) {
10209     Diag(StartLoc, diag::err_omp_no_clause_for_directive)
10210         << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
10211     return StmtError();
10212   }
10213 
10214   return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
10215                                              AStmt);
10216 }
10217 
10218 StmtResult
10219 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
10220                                          SourceLocation StartLoc,
10221                                          SourceLocation EndLoc, Stmt *AStmt) {
10222   if (!AStmt)
10223     return StmtError();
10224 
10225   auto *CS = cast<CapturedStmt>(AStmt);
10226   // 1.2.2 OpenMP Language Terminology
10227   // Structured block - An executable statement with a single entry at the
10228   // top and a single exit at the bottom.
10229   // The point of exit cannot be a branch out of the structured block.
10230   // longjmp() and throw() must not violate the entry/exit criteria.
10231   CS->getCapturedDecl()->setNothrow();
10232   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
10233        ThisCaptureLevel > 1; --ThisCaptureLevel) {
10234     CS = cast<CapturedStmt>(CS->getCapturedStmt());
10235     // 1.2.2 OpenMP Language Terminology
10236     // Structured block - An executable statement with a single entry at the
10237     // top and a single exit at the bottom.
10238     // The point of exit cannot be a branch out of the structured block.
10239     // longjmp() and throw() must not violate the entry/exit criteria.
10240     CS->getCapturedDecl()->setNothrow();
10241   }
10242 
10243   // OpenMP [2.10.3, Restrictions, p. 102]
10244   // At least one map clause must appear on the directive.
10245   if (!hasClauses(Clauses, OMPC_map)) {
10246     Diag(StartLoc, diag::err_omp_no_clause_for_directive)
10247         << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
10248     return StmtError();
10249   }
10250 
10251   return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
10252                                             AStmt);
10253 }
10254 
10255 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
10256                                                   SourceLocation StartLoc,
10257                                                   SourceLocation EndLoc,
10258                                                   Stmt *AStmt) {
10259   if (!AStmt)
10260     return StmtError();
10261 
10262   auto *CS = cast<CapturedStmt>(AStmt);
10263   // 1.2.2 OpenMP Language Terminology
10264   // Structured block - An executable statement with a single entry at the
10265   // top and a single exit at the bottom.
10266   // The point of exit cannot be a branch out of the structured block.
10267   // longjmp() and throw() must not violate the entry/exit criteria.
10268   CS->getCapturedDecl()->setNothrow();
10269   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
10270        ThisCaptureLevel > 1; --ThisCaptureLevel) {
10271     CS = cast<CapturedStmt>(CS->getCapturedStmt());
10272     // 1.2.2 OpenMP Language Terminology
10273     // Structured block - An executable statement with a single entry at the
10274     // top and a single exit at the bottom.
10275     // The point of exit cannot be a branch out of the structured block.
10276     // longjmp() and throw() must not violate the entry/exit criteria.
10277     CS->getCapturedDecl()->setNothrow();
10278   }
10279 
10280   if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
10281     Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
10282     return StmtError();
10283   }
10284   return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
10285                                           AStmt);
10286 }
10287 
10288 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
10289                                            Stmt *AStmt, SourceLocation StartLoc,
10290                                            SourceLocation EndLoc) {
10291   if (!AStmt)
10292     return StmtError();
10293 
10294   auto *CS = cast<CapturedStmt>(AStmt);
10295   // 1.2.2 OpenMP Language Terminology
10296   // Structured block - An executable statement with a single entry at the
10297   // top and a single exit at the bottom.
10298   // The point of exit cannot be a branch out of the structured block.
10299   // longjmp() and throw() must not violate the entry/exit criteria.
10300   CS->getCapturedDecl()->setNothrow();
10301 
10302   setFunctionHasBranchProtectedScope();
10303 
10304   DSAStack->setParentTeamsRegionLoc(StartLoc);
10305 
10306   return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
10307 }
10308 
10309 StmtResult
10310 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
10311                                             SourceLocation EndLoc,
10312                                             OpenMPDirectiveKind CancelRegion) {
10313   if (DSAStack->isParentNowaitRegion()) {
10314     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
10315     return StmtError();
10316   }
10317   if (DSAStack->isParentOrderedRegion()) {
10318     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
10319     return StmtError();
10320   }
10321   return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
10322                                                CancelRegion);
10323 }
10324 
10325 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
10326                                             SourceLocation StartLoc,
10327                                             SourceLocation EndLoc,
10328                                             OpenMPDirectiveKind CancelRegion) {
10329   if (DSAStack->isParentNowaitRegion()) {
10330     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
10331     return StmtError();
10332   }
10333   if (DSAStack->isParentOrderedRegion()) {
10334     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
10335     return StmtError();
10336   }
10337   DSAStack->setParentCancelRegion(/*Cancel=*/true);
10338   return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
10339                                     CancelRegion);
10340 }
10341 
10342 static bool checkGrainsizeNumTasksClauses(Sema &S,
10343                                           ArrayRef<OMPClause *> Clauses) {
10344   const OMPClause *PrevClause = nullptr;
10345   bool ErrorFound = false;
10346   for (const OMPClause *C : Clauses) {
10347     if (C->getClauseKind() == OMPC_grainsize ||
10348         C->getClauseKind() == OMPC_num_tasks) {
10349       if (!PrevClause)
10350         PrevClause = C;
10351       else if (PrevClause->getClauseKind() != C->getClauseKind()) {
10352         S.Diag(C->getBeginLoc(), diag::err_omp_clauses_mutually_exclusive)
10353             << getOpenMPClauseName(C->getClauseKind())
10354             << getOpenMPClauseName(PrevClause->getClauseKind());
10355         S.Diag(PrevClause->getBeginLoc(), diag::note_omp_previous_clause)
10356             << getOpenMPClauseName(PrevClause->getClauseKind());
10357         ErrorFound = true;
10358       }
10359     }
10360   }
10361   return ErrorFound;
10362 }
10363 
10364 static bool checkReductionClauseWithNogroup(Sema &S,
10365                                             ArrayRef<OMPClause *> Clauses) {
10366   const OMPClause *ReductionClause = nullptr;
10367   const OMPClause *NogroupClause = nullptr;
10368   for (const OMPClause *C : Clauses) {
10369     if (C->getClauseKind() == OMPC_reduction) {
10370       ReductionClause = C;
10371       if (NogroupClause)
10372         break;
10373       continue;
10374     }
10375     if (C->getClauseKind() == OMPC_nogroup) {
10376       NogroupClause = C;
10377       if (ReductionClause)
10378         break;
10379       continue;
10380     }
10381   }
10382   if (ReductionClause && NogroupClause) {
10383     S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup)
10384         << SourceRange(NogroupClause->getBeginLoc(),
10385                        NogroupClause->getEndLoc());
10386     return true;
10387   }
10388   return false;
10389 }
10390 
10391 StmtResult Sema::ActOnOpenMPTaskLoopDirective(
10392     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10393     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10394   if (!AStmt)
10395     return StmtError();
10396 
10397   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10398   OMPLoopDirective::HelperExprs B;
10399   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
10400   // define the nested loops number.
10401   unsigned NestedLoopCount =
10402       checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
10403                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
10404                       VarsWithImplicitDSA, B);
10405   if (NestedLoopCount == 0)
10406     return StmtError();
10407 
10408   assert((CurContext->isDependentContext() || B.builtAll()) &&
10409          "omp for loop exprs were not built");
10410 
10411   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
10412   // The grainsize clause and num_tasks clause are mutually exclusive and may
10413   // not appear on the same taskloop directive.
10414   if (checkGrainsizeNumTasksClauses(*this, Clauses))
10415     return StmtError();
10416   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
10417   // If a reduction clause is present on the taskloop directive, the nogroup
10418   // clause must not be specified.
10419   if (checkReductionClauseWithNogroup(*this, Clauses))
10420     return StmtError();
10421 
10422   setFunctionHasBranchProtectedScope();
10423   return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
10424                                       NestedLoopCount, Clauses, AStmt, B,
10425                                       DSAStack->isCancelRegion());
10426 }
10427 
10428 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
10429     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10430     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10431   if (!AStmt)
10432     return StmtError();
10433 
10434   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10435   OMPLoopDirective::HelperExprs B;
10436   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
10437   // define the nested loops number.
10438   unsigned NestedLoopCount =
10439       checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
10440                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
10441                       VarsWithImplicitDSA, B);
10442   if (NestedLoopCount == 0)
10443     return StmtError();
10444 
10445   assert((CurContext->isDependentContext() || B.builtAll()) &&
10446          "omp for loop exprs were not built");
10447 
10448   if (!CurContext->isDependentContext()) {
10449     // Finalize the clauses that need pre-built expressions for CodeGen.
10450     for (OMPClause *C : Clauses) {
10451       if (auto *LC = dyn_cast<OMPLinearClause>(C))
10452         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
10453                                      B.NumIterations, *this, CurScope,
10454                                      DSAStack))
10455           return StmtError();
10456     }
10457   }
10458 
10459   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
10460   // The grainsize clause and num_tasks clause are mutually exclusive and may
10461   // not appear on the same taskloop directive.
10462   if (checkGrainsizeNumTasksClauses(*this, Clauses))
10463     return StmtError();
10464   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
10465   // If a reduction clause is present on the taskloop directive, the nogroup
10466   // clause must not be specified.
10467   if (checkReductionClauseWithNogroup(*this, Clauses))
10468     return StmtError();
10469   if (checkSimdlenSafelenSpecified(*this, Clauses))
10470     return StmtError();
10471 
10472   setFunctionHasBranchProtectedScope();
10473   return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
10474                                           NestedLoopCount, Clauses, AStmt, B);
10475 }
10476 
10477 StmtResult Sema::ActOnOpenMPMasterTaskLoopDirective(
10478     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10479     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10480   if (!AStmt)
10481     return StmtError();
10482 
10483   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10484   OMPLoopDirective::HelperExprs B;
10485   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
10486   // define the nested loops number.
10487   unsigned NestedLoopCount =
10488       checkOpenMPLoop(OMPD_master_taskloop, getCollapseNumberExpr(Clauses),
10489                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
10490                       VarsWithImplicitDSA, B);
10491   if (NestedLoopCount == 0)
10492     return StmtError();
10493 
10494   assert((CurContext->isDependentContext() || B.builtAll()) &&
10495          "omp for loop exprs were not built");
10496 
10497   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
10498   // The grainsize clause and num_tasks clause are mutually exclusive and may
10499   // not appear on the same taskloop directive.
10500   if (checkGrainsizeNumTasksClauses(*this, Clauses))
10501     return StmtError();
10502   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
10503   // If a reduction clause is present on the taskloop directive, the nogroup
10504   // clause must not be specified.
10505   if (checkReductionClauseWithNogroup(*this, Clauses))
10506     return StmtError();
10507 
10508   setFunctionHasBranchProtectedScope();
10509   return OMPMasterTaskLoopDirective::Create(Context, StartLoc, EndLoc,
10510                                             NestedLoopCount, Clauses, AStmt, B,
10511                                             DSAStack->isCancelRegion());
10512 }
10513 
10514 StmtResult Sema::ActOnOpenMPMasterTaskLoopSimdDirective(
10515     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10516     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10517   if (!AStmt)
10518     return StmtError();
10519 
10520   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10521   OMPLoopDirective::HelperExprs B;
10522   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
10523   // define the nested loops number.
10524   unsigned NestedLoopCount =
10525       checkOpenMPLoop(OMPD_master_taskloop_simd, getCollapseNumberExpr(Clauses),
10526                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
10527                       VarsWithImplicitDSA, B);
10528   if (NestedLoopCount == 0)
10529     return StmtError();
10530 
10531   assert((CurContext->isDependentContext() || B.builtAll()) &&
10532          "omp for loop exprs were not built");
10533 
10534   if (!CurContext->isDependentContext()) {
10535     // Finalize the clauses that need pre-built expressions for CodeGen.
10536     for (OMPClause *C : Clauses) {
10537       if (auto *LC = dyn_cast<OMPLinearClause>(C))
10538         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
10539                                      B.NumIterations, *this, CurScope,
10540                                      DSAStack))
10541           return StmtError();
10542     }
10543   }
10544 
10545   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
10546   // The grainsize clause and num_tasks clause are mutually exclusive and may
10547   // not appear on the same taskloop directive.
10548   if (checkGrainsizeNumTasksClauses(*this, Clauses))
10549     return StmtError();
10550   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
10551   // If a reduction clause is present on the taskloop directive, the nogroup
10552   // clause must not be specified.
10553   if (checkReductionClauseWithNogroup(*this, Clauses))
10554     return StmtError();
10555   if (checkSimdlenSafelenSpecified(*this, Clauses))
10556     return StmtError();
10557 
10558   setFunctionHasBranchProtectedScope();
10559   return OMPMasterTaskLoopSimdDirective::Create(
10560       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
10561 }
10562 
10563 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopDirective(
10564     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10565     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10566   if (!AStmt)
10567     return StmtError();
10568 
10569   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10570   auto *CS = cast<CapturedStmt>(AStmt);
10571   // 1.2.2 OpenMP Language Terminology
10572   // Structured block - An executable statement with a single entry at the
10573   // top and a single exit at the bottom.
10574   // The point of exit cannot be a branch out of the structured block.
10575   // longjmp() and throw() must not violate the entry/exit criteria.
10576   CS->getCapturedDecl()->setNothrow();
10577   for (int ThisCaptureLevel =
10578            getOpenMPCaptureLevels(OMPD_parallel_master_taskloop);
10579        ThisCaptureLevel > 1; --ThisCaptureLevel) {
10580     CS = cast<CapturedStmt>(CS->getCapturedStmt());
10581     // 1.2.2 OpenMP Language Terminology
10582     // Structured block - An executable statement with a single entry at the
10583     // top and a single exit at the bottom.
10584     // The point of exit cannot be a branch out of the structured block.
10585     // longjmp() and throw() must not violate the entry/exit criteria.
10586     CS->getCapturedDecl()->setNothrow();
10587   }
10588 
10589   OMPLoopDirective::HelperExprs B;
10590   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
10591   // define the nested loops number.
10592   unsigned NestedLoopCount = checkOpenMPLoop(
10593       OMPD_parallel_master_taskloop, getCollapseNumberExpr(Clauses),
10594       /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack,
10595       VarsWithImplicitDSA, B);
10596   if (NestedLoopCount == 0)
10597     return StmtError();
10598 
10599   assert((CurContext->isDependentContext() || B.builtAll()) &&
10600          "omp for loop exprs were not built");
10601 
10602   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
10603   // The grainsize clause and num_tasks clause are mutually exclusive and may
10604   // not appear on the same taskloop directive.
10605   if (checkGrainsizeNumTasksClauses(*this, Clauses))
10606     return StmtError();
10607   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
10608   // If a reduction clause is present on the taskloop directive, the nogroup
10609   // clause must not be specified.
10610   if (checkReductionClauseWithNogroup(*this, Clauses))
10611     return StmtError();
10612 
10613   setFunctionHasBranchProtectedScope();
10614   return OMPParallelMasterTaskLoopDirective::Create(
10615       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
10616       DSAStack->isCancelRegion());
10617 }
10618 
10619 StmtResult Sema::ActOnOpenMPParallelMasterTaskLoopSimdDirective(
10620     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10621     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10622   if (!AStmt)
10623     return StmtError();
10624 
10625   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10626   auto *CS = cast<CapturedStmt>(AStmt);
10627   // 1.2.2 OpenMP Language Terminology
10628   // Structured block - An executable statement with a single entry at the
10629   // top and a single exit at the bottom.
10630   // The point of exit cannot be a branch out of the structured block.
10631   // longjmp() and throw() must not violate the entry/exit criteria.
10632   CS->getCapturedDecl()->setNothrow();
10633   for (int ThisCaptureLevel =
10634            getOpenMPCaptureLevels(OMPD_parallel_master_taskloop_simd);
10635        ThisCaptureLevel > 1; --ThisCaptureLevel) {
10636     CS = cast<CapturedStmt>(CS->getCapturedStmt());
10637     // 1.2.2 OpenMP Language Terminology
10638     // Structured block - An executable statement with a single entry at the
10639     // top and a single exit at the bottom.
10640     // The point of exit cannot be a branch out of the structured block.
10641     // longjmp() and throw() must not violate the entry/exit criteria.
10642     CS->getCapturedDecl()->setNothrow();
10643   }
10644 
10645   OMPLoopDirective::HelperExprs B;
10646   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
10647   // define the nested loops number.
10648   unsigned NestedLoopCount = checkOpenMPLoop(
10649       OMPD_parallel_master_taskloop_simd, getCollapseNumberExpr(Clauses),
10650       /*OrderedLoopCountExpr=*/nullptr, CS, *this, *DSAStack,
10651       VarsWithImplicitDSA, B);
10652   if (NestedLoopCount == 0)
10653     return StmtError();
10654 
10655   assert((CurContext->isDependentContext() || B.builtAll()) &&
10656          "omp for loop exprs were not built");
10657 
10658   if (!CurContext->isDependentContext()) {
10659     // Finalize the clauses that need pre-built expressions for CodeGen.
10660     for (OMPClause *C : Clauses) {
10661       if (auto *LC = dyn_cast<OMPLinearClause>(C))
10662         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
10663                                      B.NumIterations, *this, CurScope,
10664                                      DSAStack))
10665           return StmtError();
10666     }
10667   }
10668 
10669   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
10670   // The grainsize clause and num_tasks clause are mutually exclusive and may
10671   // not appear on the same taskloop directive.
10672   if (checkGrainsizeNumTasksClauses(*this, Clauses))
10673     return StmtError();
10674   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
10675   // If a reduction clause is present on the taskloop directive, the nogroup
10676   // clause must not be specified.
10677   if (checkReductionClauseWithNogroup(*this, Clauses))
10678     return StmtError();
10679   if (checkSimdlenSafelenSpecified(*this, Clauses))
10680     return StmtError();
10681 
10682   setFunctionHasBranchProtectedScope();
10683   return OMPParallelMasterTaskLoopSimdDirective::Create(
10684       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
10685 }
10686 
10687 StmtResult Sema::ActOnOpenMPDistributeDirective(
10688     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10689     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10690   if (!AStmt)
10691     return StmtError();
10692 
10693   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10694   OMPLoopDirective::HelperExprs B;
10695   // In presence of clause 'collapse' with number of loops, it will
10696   // define the nested loops number.
10697   unsigned NestedLoopCount =
10698       checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
10699                       nullptr /*ordered not a clause on distribute*/, AStmt,
10700                       *this, *DSAStack, VarsWithImplicitDSA, B);
10701   if (NestedLoopCount == 0)
10702     return StmtError();
10703 
10704   assert((CurContext->isDependentContext() || B.builtAll()) &&
10705          "omp for loop exprs were not built");
10706 
10707   setFunctionHasBranchProtectedScope();
10708   return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
10709                                         NestedLoopCount, Clauses, AStmt, B);
10710 }
10711 
10712 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
10713     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10714     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10715   if (!AStmt)
10716     return StmtError();
10717 
10718   auto *CS = cast<CapturedStmt>(AStmt);
10719   // 1.2.2 OpenMP Language Terminology
10720   // Structured block - An executable statement with a single entry at the
10721   // top and a single exit at the bottom.
10722   // The point of exit cannot be a branch out of the structured block.
10723   // longjmp() and throw() must not violate the entry/exit criteria.
10724   CS->getCapturedDecl()->setNothrow();
10725   for (int ThisCaptureLevel =
10726            getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
10727        ThisCaptureLevel > 1; --ThisCaptureLevel) {
10728     CS = cast<CapturedStmt>(CS->getCapturedStmt());
10729     // 1.2.2 OpenMP Language Terminology
10730     // Structured block - An executable statement with a single entry at the
10731     // top and a single exit at the bottom.
10732     // The point of exit cannot be a branch out of the structured block.
10733     // longjmp() and throw() must not violate the entry/exit criteria.
10734     CS->getCapturedDecl()->setNothrow();
10735   }
10736 
10737   OMPLoopDirective::HelperExprs B;
10738   // In presence of clause 'collapse' with number of loops, it will
10739   // define the nested loops number.
10740   unsigned NestedLoopCount = checkOpenMPLoop(
10741       OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
10742       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
10743       VarsWithImplicitDSA, B);
10744   if (NestedLoopCount == 0)
10745     return StmtError();
10746 
10747   assert((CurContext->isDependentContext() || B.builtAll()) &&
10748          "omp for loop exprs were not built");
10749 
10750   setFunctionHasBranchProtectedScope();
10751   return OMPDistributeParallelForDirective::Create(
10752       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
10753       DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
10754 }
10755 
10756 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
10757     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10758     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10759   if (!AStmt)
10760     return StmtError();
10761 
10762   auto *CS = cast<CapturedStmt>(AStmt);
10763   // 1.2.2 OpenMP Language Terminology
10764   // Structured block - An executable statement with a single entry at the
10765   // top and a single exit at the bottom.
10766   // The point of exit cannot be a branch out of the structured block.
10767   // longjmp() and throw() must not violate the entry/exit criteria.
10768   CS->getCapturedDecl()->setNothrow();
10769   for (int ThisCaptureLevel =
10770            getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
10771        ThisCaptureLevel > 1; --ThisCaptureLevel) {
10772     CS = cast<CapturedStmt>(CS->getCapturedStmt());
10773     // 1.2.2 OpenMP Language Terminology
10774     // Structured block - An executable statement with a single entry at the
10775     // top and a single exit at the bottom.
10776     // The point of exit cannot be a branch out of the structured block.
10777     // longjmp() and throw() must not violate the entry/exit criteria.
10778     CS->getCapturedDecl()->setNothrow();
10779   }
10780 
10781   OMPLoopDirective::HelperExprs B;
10782   // In presence of clause 'collapse' with number of loops, it will
10783   // define the nested loops number.
10784   unsigned NestedLoopCount = checkOpenMPLoop(
10785       OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
10786       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
10787       VarsWithImplicitDSA, B);
10788   if (NestedLoopCount == 0)
10789     return StmtError();
10790 
10791   assert((CurContext->isDependentContext() || B.builtAll()) &&
10792          "omp for loop exprs were not built");
10793 
10794   if (!CurContext->isDependentContext()) {
10795     // Finalize the clauses that need pre-built expressions for CodeGen.
10796     for (OMPClause *C : Clauses) {
10797       if (auto *LC = dyn_cast<OMPLinearClause>(C))
10798         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
10799                                      B.NumIterations, *this, CurScope,
10800                                      DSAStack))
10801           return StmtError();
10802     }
10803   }
10804 
10805   if (checkSimdlenSafelenSpecified(*this, Clauses))
10806     return StmtError();
10807 
10808   setFunctionHasBranchProtectedScope();
10809   return OMPDistributeParallelForSimdDirective::Create(
10810       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
10811 }
10812 
10813 StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
10814     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10815     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10816   if (!AStmt)
10817     return StmtError();
10818 
10819   auto *CS = cast<CapturedStmt>(AStmt);
10820   // 1.2.2 OpenMP Language Terminology
10821   // Structured block - An executable statement with a single entry at the
10822   // top and a single exit at the bottom.
10823   // The point of exit cannot be a branch out of the structured block.
10824   // longjmp() and throw() must not violate the entry/exit criteria.
10825   CS->getCapturedDecl()->setNothrow();
10826   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
10827        ThisCaptureLevel > 1; --ThisCaptureLevel) {
10828     CS = cast<CapturedStmt>(CS->getCapturedStmt());
10829     // 1.2.2 OpenMP Language Terminology
10830     // Structured block - An executable statement with a single entry at the
10831     // top and a single exit at the bottom.
10832     // The point of exit cannot be a branch out of the structured block.
10833     // longjmp() and throw() must not violate the entry/exit criteria.
10834     CS->getCapturedDecl()->setNothrow();
10835   }
10836 
10837   OMPLoopDirective::HelperExprs B;
10838   // In presence of clause 'collapse' with number of loops, it will
10839   // define the nested loops number.
10840   unsigned NestedLoopCount =
10841       checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
10842                       nullptr /*ordered not a clause on distribute*/, CS, *this,
10843                       *DSAStack, VarsWithImplicitDSA, B);
10844   if (NestedLoopCount == 0)
10845     return StmtError();
10846 
10847   assert((CurContext->isDependentContext() || B.builtAll()) &&
10848          "omp for loop exprs were not built");
10849 
10850   if (!CurContext->isDependentContext()) {
10851     // Finalize the clauses that need pre-built expressions for CodeGen.
10852     for (OMPClause *C : Clauses) {
10853       if (auto *LC = dyn_cast<OMPLinearClause>(C))
10854         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
10855                                      B.NumIterations, *this, CurScope,
10856                                      DSAStack))
10857           return StmtError();
10858     }
10859   }
10860 
10861   if (checkSimdlenSafelenSpecified(*this, Clauses))
10862     return StmtError();
10863 
10864   setFunctionHasBranchProtectedScope();
10865   return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
10866                                             NestedLoopCount, Clauses, AStmt, B);
10867 }
10868 
10869 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
10870     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10871     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10872   if (!AStmt)
10873     return StmtError();
10874 
10875   auto *CS = cast<CapturedStmt>(AStmt);
10876   // 1.2.2 OpenMP Language Terminology
10877   // Structured block - An executable statement with a single entry at the
10878   // top and a single exit at the bottom.
10879   // The point of exit cannot be a branch out of the structured block.
10880   // longjmp() and throw() must not violate the entry/exit criteria.
10881   CS->getCapturedDecl()->setNothrow();
10882   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
10883        ThisCaptureLevel > 1; --ThisCaptureLevel) {
10884     CS = cast<CapturedStmt>(CS->getCapturedStmt());
10885     // 1.2.2 OpenMP Language Terminology
10886     // Structured block - An executable statement with a single entry at the
10887     // top and a single exit at the bottom.
10888     // The point of exit cannot be a branch out of the structured block.
10889     // longjmp() and throw() must not violate the entry/exit criteria.
10890     CS->getCapturedDecl()->setNothrow();
10891   }
10892 
10893   OMPLoopDirective::HelperExprs B;
10894   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
10895   // define the nested loops number.
10896   unsigned NestedLoopCount = checkOpenMPLoop(
10897       OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
10898       getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
10899       VarsWithImplicitDSA, B);
10900   if (NestedLoopCount == 0)
10901     return StmtError();
10902 
10903   assert((CurContext->isDependentContext() || B.builtAll()) &&
10904          "omp target parallel for simd loop exprs were not built");
10905 
10906   if (!CurContext->isDependentContext()) {
10907     // Finalize the clauses that need pre-built expressions for CodeGen.
10908     for (OMPClause *C : Clauses) {
10909       if (auto *LC = dyn_cast<OMPLinearClause>(C))
10910         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
10911                                      B.NumIterations, *this, CurScope,
10912                                      DSAStack))
10913           return StmtError();
10914     }
10915   }
10916   if (checkSimdlenSafelenSpecified(*this, Clauses))
10917     return StmtError();
10918 
10919   setFunctionHasBranchProtectedScope();
10920   return OMPTargetParallelForSimdDirective::Create(
10921       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
10922 }
10923 
10924 StmtResult Sema::ActOnOpenMPTargetSimdDirective(
10925     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10926     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10927   if (!AStmt)
10928     return StmtError();
10929 
10930   auto *CS = cast<CapturedStmt>(AStmt);
10931   // 1.2.2 OpenMP Language Terminology
10932   // Structured block - An executable statement with a single entry at the
10933   // top and a single exit at the bottom.
10934   // The point of exit cannot be a branch out of the structured block.
10935   // longjmp() and throw() must not violate the entry/exit criteria.
10936   CS->getCapturedDecl()->setNothrow();
10937   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
10938        ThisCaptureLevel > 1; --ThisCaptureLevel) {
10939     CS = cast<CapturedStmt>(CS->getCapturedStmt());
10940     // 1.2.2 OpenMP Language Terminology
10941     // Structured block - An executable statement with a single entry at the
10942     // top and a single exit at the bottom.
10943     // The point of exit cannot be a branch out of the structured block.
10944     // longjmp() and throw() must not violate the entry/exit criteria.
10945     CS->getCapturedDecl()->setNothrow();
10946   }
10947 
10948   OMPLoopDirective::HelperExprs B;
10949   // In presence of clause 'collapse' with number of loops, it will define the
10950   // nested loops number.
10951   unsigned NestedLoopCount =
10952       checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
10953                       getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
10954                       VarsWithImplicitDSA, B);
10955   if (NestedLoopCount == 0)
10956     return StmtError();
10957 
10958   assert((CurContext->isDependentContext() || B.builtAll()) &&
10959          "omp target simd loop exprs were not built");
10960 
10961   if (!CurContext->isDependentContext()) {
10962     // Finalize the clauses that need pre-built expressions for CodeGen.
10963     for (OMPClause *C : Clauses) {
10964       if (auto *LC = dyn_cast<OMPLinearClause>(C))
10965         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
10966                                      B.NumIterations, *this, CurScope,
10967                                      DSAStack))
10968           return StmtError();
10969     }
10970   }
10971 
10972   if (checkSimdlenSafelenSpecified(*this, Clauses))
10973     return StmtError();
10974 
10975   setFunctionHasBranchProtectedScope();
10976   return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
10977                                         NestedLoopCount, Clauses, AStmt, B);
10978 }
10979 
10980 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
10981     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10982     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10983   if (!AStmt)
10984     return StmtError();
10985 
10986   auto *CS = cast<CapturedStmt>(AStmt);
10987   // 1.2.2 OpenMP Language Terminology
10988   // Structured block - An executable statement with a single entry at the
10989   // top and a single exit at the bottom.
10990   // The point of exit cannot be a branch out of the structured block.
10991   // longjmp() and throw() must not violate the entry/exit criteria.
10992   CS->getCapturedDecl()->setNothrow();
10993   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
10994        ThisCaptureLevel > 1; --ThisCaptureLevel) {
10995     CS = cast<CapturedStmt>(CS->getCapturedStmt());
10996     // 1.2.2 OpenMP Language Terminology
10997     // Structured block - An executable statement with a single entry at the
10998     // top and a single exit at the bottom.
10999     // The point of exit cannot be a branch out of the structured block.
11000     // longjmp() and throw() must not violate the entry/exit criteria.
11001     CS->getCapturedDecl()->setNothrow();
11002   }
11003 
11004   OMPLoopDirective::HelperExprs B;
11005   // In presence of clause 'collapse' with number of loops, it will
11006   // define the nested loops number.
11007   unsigned NestedLoopCount =
11008       checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
11009                       nullptr /*ordered not a clause on distribute*/, CS, *this,
11010                       *DSAStack, VarsWithImplicitDSA, B);
11011   if (NestedLoopCount == 0)
11012     return StmtError();
11013 
11014   assert((CurContext->isDependentContext() || B.builtAll()) &&
11015          "omp teams distribute loop exprs were not built");
11016 
11017   setFunctionHasBranchProtectedScope();
11018 
11019   DSAStack->setParentTeamsRegionLoc(StartLoc);
11020 
11021   return OMPTeamsDistributeDirective::Create(
11022       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
11023 }
11024 
11025 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
11026     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11027     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11028   if (!AStmt)
11029     return StmtError();
11030 
11031   auto *CS = cast<CapturedStmt>(AStmt);
11032   // 1.2.2 OpenMP Language Terminology
11033   // Structured block - An executable statement with a single entry at the
11034   // top and a single exit at the bottom.
11035   // The point of exit cannot be a branch out of the structured block.
11036   // longjmp() and throw() must not violate the entry/exit criteria.
11037   CS->getCapturedDecl()->setNothrow();
11038   for (int ThisCaptureLevel =
11039            getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
11040        ThisCaptureLevel > 1; --ThisCaptureLevel) {
11041     CS = cast<CapturedStmt>(CS->getCapturedStmt());
11042     // 1.2.2 OpenMP Language Terminology
11043     // Structured block - An executable statement with a single entry at the
11044     // top and a single exit at the bottom.
11045     // The point of exit cannot be a branch out of the structured block.
11046     // longjmp() and throw() must not violate the entry/exit criteria.
11047     CS->getCapturedDecl()->setNothrow();
11048   }
11049 
11050   OMPLoopDirective::HelperExprs B;
11051   // In presence of clause 'collapse' with number of loops, it will
11052   // define the nested loops number.
11053   unsigned NestedLoopCount = checkOpenMPLoop(
11054       OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
11055       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
11056       VarsWithImplicitDSA, B);
11057 
11058   if (NestedLoopCount == 0)
11059     return StmtError();
11060 
11061   assert((CurContext->isDependentContext() || B.builtAll()) &&
11062          "omp teams distribute simd loop exprs were not built");
11063 
11064   if (!CurContext->isDependentContext()) {
11065     // Finalize the clauses that need pre-built expressions for CodeGen.
11066     for (OMPClause *C : Clauses) {
11067       if (auto *LC = dyn_cast<OMPLinearClause>(C))
11068         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
11069                                      B.NumIterations, *this, CurScope,
11070                                      DSAStack))
11071           return StmtError();
11072     }
11073   }
11074 
11075   if (checkSimdlenSafelenSpecified(*this, Clauses))
11076     return StmtError();
11077 
11078   setFunctionHasBranchProtectedScope();
11079 
11080   DSAStack->setParentTeamsRegionLoc(StartLoc);
11081 
11082   return OMPTeamsDistributeSimdDirective::Create(
11083       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
11084 }
11085 
11086 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
11087     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11088     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11089   if (!AStmt)
11090     return StmtError();
11091 
11092   auto *CS = cast<CapturedStmt>(AStmt);
11093   // 1.2.2 OpenMP Language Terminology
11094   // Structured block - An executable statement with a single entry at the
11095   // top and a single exit at the bottom.
11096   // The point of exit cannot be a branch out of the structured block.
11097   // longjmp() and throw() must not violate the entry/exit criteria.
11098   CS->getCapturedDecl()->setNothrow();
11099 
11100   for (int ThisCaptureLevel =
11101            getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
11102        ThisCaptureLevel > 1; --ThisCaptureLevel) {
11103     CS = cast<CapturedStmt>(CS->getCapturedStmt());
11104     // 1.2.2 OpenMP Language Terminology
11105     // Structured block - An executable statement with a single entry at the
11106     // top and a single exit at the bottom.
11107     // The point of exit cannot be a branch out of the structured block.
11108     // longjmp() and throw() must not violate the entry/exit criteria.
11109     CS->getCapturedDecl()->setNothrow();
11110   }
11111 
11112   OMPLoopDirective::HelperExprs B;
11113   // In presence of clause 'collapse' with number of loops, it will
11114   // define the nested loops number.
11115   unsigned NestedLoopCount = checkOpenMPLoop(
11116       OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
11117       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
11118       VarsWithImplicitDSA, B);
11119 
11120   if (NestedLoopCount == 0)
11121     return StmtError();
11122 
11123   assert((CurContext->isDependentContext() || B.builtAll()) &&
11124          "omp for loop exprs were not built");
11125 
11126   if (!CurContext->isDependentContext()) {
11127     // Finalize the clauses that need pre-built expressions for CodeGen.
11128     for (OMPClause *C : Clauses) {
11129       if (auto *LC = dyn_cast<OMPLinearClause>(C))
11130         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
11131                                      B.NumIterations, *this, CurScope,
11132                                      DSAStack))
11133           return StmtError();
11134     }
11135   }
11136 
11137   if (checkSimdlenSafelenSpecified(*this, Clauses))
11138     return StmtError();
11139 
11140   setFunctionHasBranchProtectedScope();
11141 
11142   DSAStack->setParentTeamsRegionLoc(StartLoc);
11143 
11144   return OMPTeamsDistributeParallelForSimdDirective::Create(
11145       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
11146 }
11147 
11148 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
11149     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11150     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11151   if (!AStmt)
11152     return StmtError();
11153 
11154   auto *CS = cast<CapturedStmt>(AStmt);
11155   // 1.2.2 OpenMP Language Terminology
11156   // Structured block - An executable statement with a single entry at the
11157   // top and a single exit at the bottom.
11158   // The point of exit cannot be a branch out of the structured block.
11159   // longjmp() and throw() must not violate the entry/exit criteria.
11160   CS->getCapturedDecl()->setNothrow();
11161 
11162   for (int ThisCaptureLevel =
11163            getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
11164        ThisCaptureLevel > 1; --ThisCaptureLevel) {
11165     CS = cast<CapturedStmt>(CS->getCapturedStmt());
11166     // 1.2.2 OpenMP Language Terminology
11167     // Structured block - An executable statement with a single entry at the
11168     // top and a single exit at the bottom.
11169     // The point of exit cannot be a branch out of the structured block.
11170     // longjmp() and throw() must not violate the entry/exit criteria.
11171     CS->getCapturedDecl()->setNothrow();
11172   }
11173 
11174   OMPLoopDirective::HelperExprs B;
11175   // In presence of clause 'collapse' with number of loops, it will
11176   // define the nested loops number.
11177   unsigned NestedLoopCount = checkOpenMPLoop(
11178       OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
11179       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
11180       VarsWithImplicitDSA, B);
11181 
11182   if (NestedLoopCount == 0)
11183     return StmtError();
11184 
11185   assert((CurContext->isDependentContext() || B.builtAll()) &&
11186          "omp for loop exprs were not built");
11187 
11188   setFunctionHasBranchProtectedScope();
11189 
11190   DSAStack->setParentTeamsRegionLoc(StartLoc);
11191 
11192   return OMPTeamsDistributeParallelForDirective::Create(
11193       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
11194       DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
11195 }
11196 
11197 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
11198                                                  Stmt *AStmt,
11199                                                  SourceLocation StartLoc,
11200                                                  SourceLocation EndLoc) {
11201   if (!AStmt)
11202     return StmtError();
11203 
11204   auto *CS = cast<CapturedStmt>(AStmt);
11205   // 1.2.2 OpenMP Language Terminology
11206   // Structured block - An executable statement with a single entry at the
11207   // top and a single exit at the bottom.
11208   // The point of exit cannot be a branch out of the structured block.
11209   // longjmp() and throw() must not violate the entry/exit criteria.
11210   CS->getCapturedDecl()->setNothrow();
11211 
11212   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
11213        ThisCaptureLevel > 1; --ThisCaptureLevel) {
11214     CS = cast<CapturedStmt>(CS->getCapturedStmt());
11215     // 1.2.2 OpenMP Language Terminology
11216     // Structured block - An executable statement with a single entry at the
11217     // top and a single exit at the bottom.
11218     // The point of exit cannot be a branch out of the structured block.
11219     // longjmp() and throw() must not violate the entry/exit criteria.
11220     CS->getCapturedDecl()->setNothrow();
11221   }
11222   setFunctionHasBranchProtectedScope();
11223 
11224   return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
11225                                          AStmt);
11226 }
11227 
11228 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
11229     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11230     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11231   if (!AStmt)
11232     return StmtError();
11233 
11234   auto *CS = cast<CapturedStmt>(AStmt);
11235   // 1.2.2 OpenMP Language Terminology
11236   // Structured block - An executable statement with a single entry at the
11237   // top and a single exit at the bottom.
11238   // The point of exit cannot be a branch out of the structured block.
11239   // longjmp() and throw() must not violate the entry/exit criteria.
11240   CS->getCapturedDecl()->setNothrow();
11241   for (int ThisCaptureLevel =
11242            getOpenMPCaptureLevels(OMPD_target_teams_distribute);
11243        ThisCaptureLevel > 1; --ThisCaptureLevel) {
11244     CS = cast<CapturedStmt>(CS->getCapturedStmt());
11245     // 1.2.2 OpenMP Language Terminology
11246     // Structured block - An executable statement with a single entry at the
11247     // top and a single exit at the bottom.
11248     // The point of exit cannot be a branch out of the structured block.
11249     // longjmp() and throw() must not violate the entry/exit criteria.
11250     CS->getCapturedDecl()->setNothrow();
11251   }
11252 
11253   OMPLoopDirective::HelperExprs B;
11254   // In presence of clause 'collapse' with number of loops, it will
11255   // define the nested loops number.
11256   unsigned NestedLoopCount = checkOpenMPLoop(
11257       OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
11258       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
11259       VarsWithImplicitDSA, B);
11260   if (NestedLoopCount == 0)
11261     return StmtError();
11262 
11263   assert((CurContext->isDependentContext() || B.builtAll()) &&
11264          "omp target teams distribute loop exprs were not built");
11265 
11266   setFunctionHasBranchProtectedScope();
11267   return OMPTargetTeamsDistributeDirective::Create(
11268       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
11269 }
11270 
11271 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
11272     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11273     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11274   if (!AStmt)
11275     return StmtError();
11276 
11277   auto *CS = cast<CapturedStmt>(AStmt);
11278   // 1.2.2 OpenMP Language Terminology
11279   // Structured block - An executable statement with a single entry at the
11280   // top and a single exit at the bottom.
11281   // The point of exit cannot be a branch out of the structured block.
11282   // longjmp() and throw() must not violate the entry/exit criteria.
11283   CS->getCapturedDecl()->setNothrow();
11284   for (int ThisCaptureLevel =
11285            getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
11286        ThisCaptureLevel > 1; --ThisCaptureLevel) {
11287     CS = cast<CapturedStmt>(CS->getCapturedStmt());
11288     // 1.2.2 OpenMP Language Terminology
11289     // Structured block - An executable statement with a single entry at the
11290     // top and a single exit at the bottom.
11291     // The point of exit cannot be a branch out of the structured block.
11292     // longjmp() and throw() must not violate the entry/exit criteria.
11293     CS->getCapturedDecl()->setNothrow();
11294   }
11295 
11296   OMPLoopDirective::HelperExprs B;
11297   // In presence of clause 'collapse' with number of loops, it will
11298   // define the nested loops number.
11299   unsigned NestedLoopCount = checkOpenMPLoop(
11300       OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
11301       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
11302       VarsWithImplicitDSA, B);
11303   if (NestedLoopCount == 0)
11304     return StmtError();
11305 
11306   assert((CurContext->isDependentContext() || B.builtAll()) &&
11307          "omp target teams distribute parallel for loop exprs were not built");
11308 
11309   if (!CurContext->isDependentContext()) {
11310     // Finalize the clauses that need pre-built expressions for CodeGen.
11311     for (OMPClause *C : Clauses) {
11312       if (auto *LC = dyn_cast<OMPLinearClause>(C))
11313         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
11314                                      B.NumIterations, *this, CurScope,
11315                                      DSAStack))
11316           return StmtError();
11317     }
11318   }
11319 
11320   setFunctionHasBranchProtectedScope();
11321   return OMPTargetTeamsDistributeParallelForDirective::Create(
11322       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
11323       DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
11324 }
11325 
11326 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
11327     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11328     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11329   if (!AStmt)
11330     return StmtError();
11331 
11332   auto *CS = cast<CapturedStmt>(AStmt);
11333   // 1.2.2 OpenMP Language Terminology
11334   // Structured block - An executable statement with a single entry at the
11335   // top and a single exit at the bottom.
11336   // The point of exit cannot be a branch out of the structured block.
11337   // longjmp() and throw() must not violate the entry/exit criteria.
11338   CS->getCapturedDecl()->setNothrow();
11339   for (int ThisCaptureLevel = getOpenMPCaptureLevels(
11340            OMPD_target_teams_distribute_parallel_for_simd);
11341        ThisCaptureLevel > 1; --ThisCaptureLevel) {
11342     CS = cast<CapturedStmt>(CS->getCapturedStmt());
11343     // 1.2.2 OpenMP Language Terminology
11344     // Structured block - An executable statement with a single entry at the
11345     // top and a single exit at the bottom.
11346     // The point of exit cannot be a branch out of the structured block.
11347     // longjmp() and throw() must not violate the entry/exit criteria.
11348     CS->getCapturedDecl()->setNothrow();
11349   }
11350 
11351   OMPLoopDirective::HelperExprs B;
11352   // In presence of clause 'collapse' with number of loops, it will
11353   // define the nested loops number.
11354   unsigned NestedLoopCount =
11355       checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd,
11356                       getCollapseNumberExpr(Clauses),
11357                       nullptr /*ordered not a clause on distribute*/, CS, *this,
11358                       *DSAStack, VarsWithImplicitDSA, B);
11359   if (NestedLoopCount == 0)
11360     return StmtError();
11361 
11362   assert((CurContext->isDependentContext() || B.builtAll()) &&
11363          "omp target teams distribute parallel for simd loop exprs were not "
11364          "built");
11365 
11366   if (!CurContext->isDependentContext()) {
11367     // Finalize the clauses that need pre-built expressions for CodeGen.
11368     for (OMPClause *C : Clauses) {
11369       if (auto *LC = dyn_cast<OMPLinearClause>(C))
11370         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
11371                                      B.NumIterations, *this, CurScope,
11372                                      DSAStack))
11373           return StmtError();
11374     }
11375   }
11376 
11377   if (checkSimdlenSafelenSpecified(*this, Clauses))
11378     return StmtError();
11379 
11380   setFunctionHasBranchProtectedScope();
11381   return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
11382       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
11383 }
11384 
11385 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
11386     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11387     SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11388   if (!AStmt)
11389     return StmtError();
11390 
11391   auto *CS = cast<CapturedStmt>(AStmt);
11392   // 1.2.2 OpenMP Language Terminology
11393   // Structured block - An executable statement with a single entry at the
11394   // top and a single exit at the bottom.
11395   // The point of exit cannot be a branch out of the structured block.
11396   // longjmp() and throw() must not violate the entry/exit criteria.
11397   CS->getCapturedDecl()->setNothrow();
11398   for (int ThisCaptureLevel =
11399            getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
11400        ThisCaptureLevel > 1; --ThisCaptureLevel) {
11401     CS = cast<CapturedStmt>(CS->getCapturedStmt());
11402     // 1.2.2 OpenMP Language Terminology
11403     // Structured block - An executable statement with a single entry at the
11404     // top and a single exit at the bottom.
11405     // The point of exit cannot be a branch out of the structured block.
11406     // longjmp() and throw() must not violate the entry/exit criteria.
11407     CS->getCapturedDecl()->setNothrow();
11408   }
11409 
11410   OMPLoopDirective::HelperExprs B;
11411   // In presence of clause 'collapse' with number of loops, it will
11412   // define the nested loops number.
11413   unsigned NestedLoopCount = checkOpenMPLoop(
11414       OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
11415       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
11416       VarsWithImplicitDSA, B);
11417   if (NestedLoopCount == 0)
11418     return StmtError();
11419 
11420   assert((CurContext->isDependentContext() || B.builtAll()) &&
11421          "omp target teams distribute simd loop exprs were not built");
11422 
11423   if (!CurContext->isDependentContext()) {
11424     // Finalize the clauses that need pre-built expressions for CodeGen.
11425     for (OMPClause *C : Clauses) {
11426       if (auto *LC = dyn_cast<OMPLinearClause>(C))
11427         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
11428                                      B.NumIterations, *this, CurScope,
11429                                      DSAStack))
11430           return StmtError();
11431     }
11432   }
11433 
11434   if (checkSimdlenSafelenSpecified(*this, Clauses))
11435     return StmtError();
11436 
11437   setFunctionHasBranchProtectedScope();
11438   return OMPTargetTeamsDistributeSimdDirective::Create(
11439       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
11440 }
11441 
11442 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
11443                                              SourceLocation StartLoc,
11444                                              SourceLocation LParenLoc,
11445                                              SourceLocation EndLoc) {
11446   OMPClause *Res = nullptr;
11447   switch (Kind) {
11448   case OMPC_final:
11449     Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
11450     break;
11451   case OMPC_num_threads:
11452     Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
11453     break;
11454   case OMPC_safelen:
11455     Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
11456     break;
11457   case OMPC_simdlen:
11458     Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
11459     break;
11460   case OMPC_allocator:
11461     Res = ActOnOpenMPAllocatorClause(Expr, StartLoc, LParenLoc, EndLoc);
11462     break;
11463   case OMPC_collapse:
11464     Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
11465     break;
11466   case OMPC_ordered:
11467     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
11468     break;
11469   case OMPC_num_teams:
11470     Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
11471     break;
11472   case OMPC_thread_limit:
11473     Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
11474     break;
11475   case OMPC_priority:
11476     Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
11477     break;
11478   case OMPC_grainsize:
11479     Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
11480     break;
11481   case OMPC_num_tasks:
11482     Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
11483     break;
11484   case OMPC_hint:
11485     Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
11486     break;
11487   case OMPC_depobj:
11488     Res = ActOnOpenMPDepobjClause(Expr, StartLoc, LParenLoc, EndLoc);
11489     break;
11490   case OMPC_detach:
11491     Res = ActOnOpenMPDetachClause(Expr, StartLoc, LParenLoc, EndLoc);
11492     break;
11493   case OMPC_device:
11494   case OMPC_if:
11495   case OMPC_default:
11496   case OMPC_proc_bind:
11497   case OMPC_schedule:
11498   case OMPC_private:
11499   case OMPC_firstprivate:
11500   case OMPC_lastprivate:
11501   case OMPC_shared:
11502   case OMPC_reduction:
11503   case OMPC_task_reduction:
11504   case OMPC_in_reduction:
11505   case OMPC_linear:
11506   case OMPC_aligned:
11507   case OMPC_copyin:
11508   case OMPC_copyprivate:
11509   case OMPC_nowait:
11510   case OMPC_untied:
11511   case OMPC_mergeable:
11512   case OMPC_threadprivate:
11513   case OMPC_allocate:
11514   case OMPC_flush:
11515   case OMPC_read:
11516   case OMPC_write:
11517   case OMPC_update:
11518   case OMPC_capture:
11519   case OMPC_seq_cst:
11520   case OMPC_acq_rel:
11521   case OMPC_acquire:
11522   case OMPC_release:
11523   case OMPC_relaxed:
11524   case OMPC_depend:
11525   case OMPC_threads:
11526   case OMPC_simd:
11527   case OMPC_map:
11528   case OMPC_nogroup:
11529   case OMPC_dist_schedule:
11530   case OMPC_defaultmap:
11531   case OMPC_unknown:
11532   case OMPC_uniform:
11533   case OMPC_to:
11534   case OMPC_from:
11535   case OMPC_use_device_ptr:
11536   case OMPC_is_device_ptr:
11537   case OMPC_unified_address:
11538   case OMPC_unified_shared_memory:
11539   case OMPC_reverse_offload:
11540   case OMPC_dynamic_allocators:
11541   case OMPC_atomic_default_mem_order:
11542   case OMPC_device_type:
11543   case OMPC_match:
11544   case OMPC_nontemporal:
11545   case OMPC_order:
11546   case OMPC_destroy:
11547   case OMPC_inclusive:
11548   case OMPC_exclusive:
11549   case OMPC_uses_allocators:
11550     llvm_unreachable("Clause is not allowed.");
11551   }
11552   return Res;
11553 }
11554 
11555 // An OpenMP directive such as 'target parallel' has two captured regions:
11556 // for the 'target' and 'parallel' respectively.  This function returns
11557 // the region in which to capture expressions associated with a clause.
11558 // A return value of OMPD_unknown signifies that the expression should not
11559 // be captured.
11560 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
11561     OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, unsigned OpenMPVersion,
11562     OpenMPDirectiveKind NameModifier = OMPD_unknown) {
11563   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
11564   switch (CKind) {
11565   case OMPC_if:
11566     switch (DKind) {
11567     case OMPD_target_parallel_for_simd:
11568       if (OpenMPVersion >= 50 &&
11569           (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) {
11570         CaptureRegion = OMPD_parallel;
11571         break;
11572       }
11573       LLVM_FALLTHROUGH;
11574     case OMPD_target_parallel:
11575     case OMPD_target_parallel_for:
11576       // If this clause applies to the nested 'parallel' region, capture within
11577       // the 'target' region, otherwise do not capture.
11578       if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
11579         CaptureRegion = OMPD_target;
11580       break;
11581     case OMPD_target_teams_distribute_parallel_for_simd:
11582       if (OpenMPVersion >= 50 &&
11583           (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) {
11584         CaptureRegion = OMPD_parallel;
11585         break;
11586       }
11587       LLVM_FALLTHROUGH;
11588     case OMPD_target_teams_distribute_parallel_for:
11589       // If this clause applies to the nested 'parallel' region, capture within
11590       // the 'teams' region, otherwise do not capture.
11591       if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
11592         CaptureRegion = OMPD_teams;
11593       break;
11594     case OMPD_teams_distribute_parallel_for_simd:
11595       if (OpenMPVersion >= 50 &&
11596           (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)) {
11597         CaptureRegion = OMPD_parallel;
11598         break;
11599       }
11600       LLVM_FALLTHROUGH;
11601     case OMPD_teams_distribute_parallel_for:
11602       CaptureRegion = OMPD_teams;
11603       break;
11604     case OMPD_target_update:
11605     case OMPD_target_enter_data:
11606     case OMPD_target_exit_data:
11607       CaptureRegion = OMPD_task;
11608       break;
11609     case OMPD_parallel_master_taskloop:
11610       if (NameModifier == OMPD_unknown || NameModifier == OMPD_taskloop)
11611         CaptureRegion = OMPD_parallel;
11612       break;
11613     case OMPD_parallel_master_taskloop_simd:
11614       if ((OpenMPVersion <= 45 && NameModifier == OMPD_unknown) ||
11615           NameModifier == OMPD_taskloop) {
11616         CaptureRegion = OMPD_parallel;
11617         break;
11618       }
11619       if (OpenMPVersion <= 45)
11620         break;
11621       if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)
11622         CaptureRegion = OMPD_taskloop;
11623       break;
11624     case OMPD_parallel_for_simd:
11625       if (OpenMPVersion <= 45)
11626         break;
11627       if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)
11628         CaptureRegion = OMPD_parallel;
11629       break;
11630     case OMPD_taskloop_simd:
11631     case OMPD_master_taskloop_simd:
11632       if (OpenMPVersion <= 45)
11633         break;
11634       if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)
11635         CaptureRegion = OMPD_taskloop;
11636       break;
11637     case OMPD_distribute_parallel_for_simd:
11638       if (OpenMPVersion <= 45)
11639         break;
11640       if (NameModifier == OMPD_unknown || NameModifier == OMPD_simd)
11641         CaptureRegion = OMPD_parallel;
11642       break;
11643     case OMPD_target_simd:
11644       if (OpenMPVersion >= 50 &&
11645           (NameModifier == OMPD_unknown || NameModifier == OMPD_simd))
11646         CaptureRegion = OMPD_target;
11647       break;
11648     case OMPD_teams_distribute_simd:
11649     case OMPD_target_teams_distribute_simd:
11650       if (OpenMPVersion >= 50 &&
11651           (NameModifier == OMPD_unknown || NameModifier == OMPD_simd))
11652         CaptureRegion = OMPD_teams;
11653       break;
11654     case OMPD_cancel:
11655     case OMPD_parallel:
11656     case OMPD_parallel_master:
11657     case OMPD_parallel_sections:
11658     case OMPD_parallel_for:
11659     case OMPD_target:
11660     case OMPD_target_teams:
11661     case OMPD_target_teams_distribute:
11662     case OMPD_distribute_parallel_for:
11663     case OMPD_task:
11664     case OMPD_taskloop:
11665     case OMPD_master_taskloop:
11666     case OMPD_target_data:
11667     case OMPD_simd:
11668     case OMPD_for_simd:
11669     case OMPD_distribute_simd:
11670       // Do not capture if-clause expressions.
11671       break;
11672     case OMPD_threadprivate:
11673     case OMPD_allocate:
11674     case OMPD_taskyield:
11675     case OMPD_barrier:
11676     case OMPD_taskwait:
11677     case OMPD_cancellation_point:
11678     case OMPD_flush:
11679     case OMPD_depobj:
11680     case OMPD_scan:
11681     case OMPD_declare_reduction:
11682     case OMPD_declare_mapper:
11683     case OMPD_declare_simd:
11684     case OMPD_declare_variant:
11685     case OMPD_begin_declare_variant:
11686     case OMPD_end_declare_variant:
11687     case OMPD_declare_target:
11688     case OMPD_end_declare_target:
11689     case OMPD_teams:
11690     case OMPD_for:
11691     case OMPD_sections:
11692     case OMPD_section:
11693     case OMPD_single:
11694     case OMPD_master:
11695     case OMPD_critical:
11696     case OMPD_taskgroup:
11697     case OMPD_distribute:
11698     case OMPD_ordered:
11699     case OMPD_atomic:
11700     case OMPD_teams_distribute:
11701     case OMPD_requires:
11702       llvm_unreachable("Unexpected OpenMP directive with if-clause");
11703     case OMPD_unknown:
11704       llvm_unreachable("Unknown OpenMP directive");
11705     }
11706     break;
11707   case OMPC_num_threads:
11708     switch (DKind) {
11709     case OMPD_target_parallel:
11710     case OMPD_target_parallel_for:
11711     case OMPD_target_parallel_for_simd:
11712       CaptureRegion = OMPD_target;
11713       break;
11714     case OMPD_teams_distribute_parallel_for:
11715     case OMPD_teams_distribute_parallel_for_simd:
11716     case OMPD_target_teams_distribute_parallel_for:
11717     case OMPD_target_teams_distribute_parallel_for_simd:
11718       CaptureRegion = OMPD_teams;
11719       break;
11720     case OMPD_parallel:
11721     case OMPD_parallel_master:
11722     case OMPD_parallel_sections:
11723     case OMPD_parallel_for:
11724     case OMPD_parallel_for_simd:
11725     case OMPD_distribute_parallel_for:
11726     case OMPD_distribute_parallel_for_simd:
11727     case OMPD_parallel_master_taskloop:
11728     case OMPD_parallel_master_taskloop_simd:
11729       // Do not capture num_threads-clause expressions.
11730       break;
11731     case OMPD_target_data:
11732     case OMPD_target_enter_data:
11733     case OMPD_target_exit_data:
11734     case OMPD_target_update:
11735     case OMPD_target:
11736     case OMPD_target_simd:
11737     case OMPD_target_teams:
11738     case OMPD_target_teams_distribute:
11739     case OMPD_target_teams_distribute_simd:
11740     case OMPD_cancel:
11741     case OMPD_task:
11742     case OMPD_taskloop:
11743     case OMPD_taskloop_simd:
11744     case OMPD_master_taskloop:
11745     case OMPD_master_taskloop_simd:
11746     case OMPD_threadprivate:
11747     case OMPD_allocate:
11748     case OMPD_taskyield:
11749     case OMPD_barrier:
11750     case OMPD_taskwait:
11751     case OMPD_cancellation_point:
11752     case OMPD_flush:
11753     case OMPD_depobj:
11754     case OMPD_scan:
11755     case OMPD_declare_reduction:
11756     case OMPD_declare_mapper:
11757     case OMPD_declare_simd:
11758     case OMPD_declare_variant:
11759     case OMPD_begin_declare_variant:
11760     case OMPD_end_declare_variant:
11761     case OMPD_declare_target:
11762     case OMPD_end_declare_target:
11763     case OMPD_teams:
11764     case OMPD_simd:
11765     case OMPD_for:
11766     case OMPD_for_simd:
11767     case OMPD_sections:
11768     case OMPD_section:
11769     case OMPD_single:
11770     case OMPD_master:
11771     case OMPD_critical:
11772     case OMPD_taskgroup:
11773     case OMPD_distribute:
11774     case OMPD_ordered:
11775     case OMPD_atomic:
11776     case OMPD_distribute_simd:
11777     case OMPD_teams_distribute:
11778     case OMPD_teams_distribute_simd:
11779     case OMPD_requires:
11780       llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
11781     case OMPD_unknown:
11782       llvm_unreachable("Unknown OpenMP directive");
11783     }
11784     break;
11785   case OMPC_num_teams:
11786     switch (DKind) {
11787     case OMPD_target_teams:
11788     case OMPD_target_teams_distribute:
11789     case OMPD_target_teams_distribute_simd:
11790     case OMPD_target_teams_distribute_parallel_for:
11791     case OMPD_target_teams_distribute_parallel_for_simd:
11792       CaptureRegion = OMPD_target;
11793       break;
11794     case OMPD_teams_distribute_parallel_for:
11795     case OMPD_teams_distribute_parallel_for_simd:
11796     case OMPD_teams:
11797     case OMPD_teams_distribute:
11798     case OMPD_teams_distribute_simd:
11799       // Do not capture num_teams-clause expressions.
11800       break;
11801     case OMPD_distribute_parallel_for:
11802     case OMPD_distribute_parallel_for_simd:
11803     case OMPD_task:
11804     case OMPD_taskloop:
11805     case OMPD_taskloop_simd:
11806     case OMPD_master_taskloop:
11807     case OMPD_master_taskloop_simd:
11808     case OMPD_parallel_master_taskloop:
11809     case OMPD_parallel_master_taskloop_simd:
11810     case OMPD_target_data:
11811     case OMPD_target_enter_data:
11812     case OMPD_target_exit_data:
11813     case OMPD_target_update:
11814     case OMPD_cancel:
11815     case OMPD_parallel:
11816     case OMPD_parallel_master:
11817     case OMPD_parallel_sections:
11818     case OMPD_parallel_for:
11819     case OMPD_parallel_for_simd:
11820     case OMPD_target:
11821     case OMPD_target_simd:
11822     case OMPD_target_parallel:
11823     case OMPD_target_parallel_for:
11824     case OMPD_target_parallel_for_simd:
11825     case OMPD_threadprivate:
11826     case OMPD_allocate:
11827     case OMPD_taskyield:
11828     case OMPD_barrier:
11829     case OMPD_taskwait:
11830     case OMPD_cancellation_point:
11831     case OMPD_flush:
11832     case OMPD_depobj:
11833     case OMPD_scan:
11834     case OMPD_declare_reduction:
11835     case OMPD_declare_mapper:
11836     case OMPD_declare_simd:
11837     case OMPD_declare_variant:
11838     case OMPD_begin_declare_variant:
11839     case OMPD_end_declare_variant:
11840     case OMPD_declare_target:
11841     case OMPD_end_declare_target:
11842     case OMPD_simd:
11843     case OMPD_for:
11844     case OMPD_for_simd:
11845     case OMPD_sections:
11846     case OMPD_section:
11847     case OMPD_single:
11848     case OMPD_master:
11849     case OMPD_critical:
11850     case OMPD_taskgroup:
11851     case OMPD_distribute:
11852     case OMPD_ordered:
11853     case OMPD_atomic:
11854     case OMPD_distribute_simd:
11855     case OMPD_requires:
11856       llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
11857     case OMPD_unknown:
11858       llvm_unreachable("Unknown OpenMP directive");
11859     }
11860     break;
11861   case OMPC_thread_limit:
11862     switch (DKind) {
11863     case OMPD_target_teams:
11864     case OMPD_target_teams_distribute:
11865     case OMPD_target_teams_distribute_simd:
11866     case OMPD_target_teams_distribute_parallel_for:
11867     case OMPD_target_teams_distribute_parallel_for_simd:
11868       CaptureRegion = OMPD_target;
11869       break;
11870     case OMPD_teams_distribute_parallel_for:
11871     case OMPD_teams_distribute_parallel_for_simd:
11872     case OMPD_teams:
11873     case OMPD_teams_distribute:
11874     case OMPD_teams_distribute_simd:
11875       // Do not capture thread_limit-clause expressions.
11876       break;
11877     case OMPD_distribute_parallel_for:
11878     case OMPD_distribute_parallel_for_simd:
11879     case OMPD_task:
11880     case OMPD_taskloop:
11881     case OMPD_taskloop_simd:
11882     case OMPD_master_taskloop:
11883     case OMPD_master_taskloop_simd:
11884     case OMPD_parallel_master_taskloop:
11885     case OMPD_parallel_master_taskloop_simd:
11886     case OMPD_target_data:
11887     case OMPD_target_enter_data:
11888     case OMPD_target_exit_data:
11889     case OMPD_target_update:
11890     case OMPD_cancel:
11891     case OMPD_parallel:
11892     case OMPD_parallel_master:
11893     case OMPD_parallel_sections:
11894     case OMPD_parallel_for:
11895     case OMPD_parallel_for_simd:
11896     case OMPD_target:
11897     case OMPD_target_simd:
11898     case OMPD_target_parallel:
11899     case OMPD_target_parallel_for:
11900     case OMPD_target_parallel_for_simd:
11901     case OMPD_threadprivate:
11902     case OMPD_allocate:
11903     case OMPD_taskyield:
11904     case OMPD_barrier:
11905     case OMPD_taskwait:
11906     case OMPD_cancellation_point:
11907     case OMPD_flush:
11908     case OMPD_depobj:
11909     case OMPD_scan:
11910     case OMPD_declare_reduction:
11911     case OMPD_declare_mapper:
11912     case OMPD_declare_simd:
11913     case OMPD_declare_variant:
11914     case OMPD_begin_declare_variant:
11915     case OMPD_end_declare_variant:
11916     case OMPD_declare_target:
11917     case OMPD_end_declare_target:
11918     case OMPD_simd:
11919     case OMPD_for:
11920     case OMPD_for_simd:
11921     case OMPD_sections:
11922     case OMPD_section:
11923     case OMPD_single:
11924     case OMPD_master:
11925     case OMPD_critical:
11926     case OMPD_taskgroup:
11927     case OMPD_distribute:
11928     case OMPD_ordered:
11929     case OMPD_atomic:
11930     case OMPD_distribute_simd:
11931     case OMPD_requires:
11932       llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
11933     case OMPD_unknown:
11934       llvm_unreachable("Unknown OpenMP directive");
11935     }
11936     break;
11937   case OMPC_schedule:
11938     switch (DKind) {
11939     case OMPD_parallel_for:
11940     case OMPD_parallel_for_simd:
11941     case OMPD_distribute_parallel_for:
11942     case OMPD_distribute_parallel_for_simd:
11943     case OMPD_teams_distribute_parallel_for:
11944     case OMPD_teams_distribute_parallel_for_simd:
11945     case OMPD_target_parallel_for:
11946     case OMPD_target_parallel_for_simd:
11947     case OMPD_target_teams_distribute_parallel_for:
11948     case OMPD_target_teams_distribute_parallel_for_simd:
11949       CaptureRegion = OMPD_parallel;
11950       break;
11951     case OMPD_for:
11952     case OMPD_for_simd:
11953       // Do not capture schedule-clause expressions.
11954       break;
11955     case OMPD_task:
11956     case OMPD_taskloop:
11957     case OMPD_taskloop_simd:
11958     case OMPD_master_taskloop:
11959     case OMPD_master_taskloop_simd:
11960     case OMPD_parallel_master_taskloop:
11961     case OMPD_parallel_master_taskloop_simd:
11962     case OMPD_target_data:
11963     case OMPD_target_enter_data:
11964     case OMPD_target_exit_data:
11965     case OMPD_target_update:
11966     case OMPD_teams:
11967     case OMPD_teams_distribute:
11968     case OMPD_teams_distribute_simd:
11969     case OMPD_target_teams_distribute:
11970     case OMPD_target_teams_distribute_simd:
11971     case OMPD_target:
11972     case OMPD_target_simd:
11973     case OMPD_target_parallel:
11974     case OMPD_cancel:
11975     case OMPD_parallel:
11976     case OMPD_parallel_master:
11977     case OMPD_parallel_sections:
11978     case OMPD_threadprivate:
11979     case OMPD_allocate:
11980     case OMPD_taskyield:
11981     case OMPD_barrier:
11982     case OMPD_taskwait:
11983     case OMPD_cancellation_point:
11984     case OMPD_flush:
11985     case OMPD_depobj:
11986     case OMPD_scan:
11987     case OMPD_declare_reduction:
11988     case OMPD_declare_mapper:
11989     case OMPD_declare_simd:
11990     case OMPD_declare_variant:
11991     case OMPD_begin_declare_variant:
11992     case OMPD_end_declare_variant:
11993     case OMPD_declare_target:
11994     case OMPD_end_declare_target:
11995     case OMPD_simd:
11996     case OMPD_sections:
11997     case OMPD_section:
11998     case OMPD_single:
11999     case OMPD_master:
12000     case OMPD_critical:
12001     case OMPD_taskgroup:
12002     case OMPD_distribute:
12003     case OMPD_ordered:
12004     case OMPD_atomic:
12005     case OMPD_distribute_simd:
12006     case OMPD_target_teams:
12007     case OMPD_requires:
12008       llvm_unreachable("Unexpected OpenMP directive with schedule clause");
12009     case OMPD_unknown:
12010       llvm_unreachable("Unknown OpenMP directive");
12011     }
12012     break;
12013   case OMPC_dist_schedule:
12014     switch (DKind) {
12015     case OMPD_teams_distribute_parallel_for:
12016     case OMPD_teams_distribute_parallel_for_simd:
12017     case OMPD_teams_distribute:
12018     case OMPD_teams_distribute_simd:
12019     case OMPD_target_teams_distribute_parallel_for:
12020     case OMPD_target_teams_distribute_parallel_for_simd:
12021     case OMPD_target_teams_distribute:
12022     case OMPD_target_teams_distribute_simd:
12023       CaptureRegion = OMPD_teams;
12024       break;
12025     case OMPD_distribute_parallel_for:
12026     case OMPD_distribute_parallel_for_simd:
12027     case OMPD_distribute:
12028     case OMPD_distribute_simd:
12029       // Do not capture thread_limit-clause expressions.
12030       break;
12031     case OMPD_parallel_for:
12032     case OMPD_parallel_for_simd:
12033     case OMPD_target_parallel_for_simd:
12034     case OMPD_target_parallel_for:
12035     case OMPD_task:
12036     case OMPD_taskloop:
12037     case OMPD_taskloop_simd:
12038     case OMPD_master_taskloop:
12039     case OMPD_master_taskloop_simd:
12040     case OMPD_parallel_master_taskloop:
12041     case OMPD_parallel_master_taskloop_simd:
12042     case OMPD_target_data:
12043     case OMPD_target_enter_data:
12044     case OMPD_target_exit_data:
12045     case OMPD_target_update:
12046     case OMPD_teams:
12047     case OMPD_target:
12048     case OMPD_target_simd:
12049     case OMPD_target_parallel:
12050     case OMPD_cancel:
12051     case OMPD_parallel:
12052     case OMPD_parallel_master:
12053     case OMPD_parallel_sections:
12054     case OMPD_threadprivate:
12055     case OMPD_allocate:
12056     case OMPD_taskyield:
12057     case OMPD_barrier:
12058     case OMPD_taskwait:
12059     case OMPD_cancellation_point:
12060     case OMPD_flush:
12061     case OMPD_depobj:
12062     case OMPD_scan:
12063     case OMPD_declare_reduction:
12064     case OMPD_declare_mapper:
12065     case OMPD_declare_simd:
12066     case OMPD_declare_variant:
12067     case OMPD_begin_declare_variant:
12068     case OMPD_end_declare_variant:
12069     case OMPD_declare_target:
12070     case OMPD_end_declare_target:
12071     case OMPD_simd:
12072     case OMPD_for:
12073     case OMPD_for_simd:
12074     case OMPD_sections:
12075     case OMPD_section:
12076     case OMPD_single:
12077     case OMPD_master:
12078     case OMPD_critical:
12079     case OMPD_taskgroup:
12080     case OMPD_ordered:
12081     case OMPD_atomic:
12082     case OMPD_target_teams:
12083     case OMPD_requires:
12084       llvm_unreachable("Unexpected OpenMP directive with schedule clause");
12085     case OMPD_unknown:
12086       llvm_unreachable("Unknown OpenMP directive");
12087     }
12088     break;
12089   case OMPC_device:
12090     switch (DKind) {
12091     case OMPD_target_update:
12092     case OMPD_target_enter_data:
12093     case OMPD_target_exit_data:
12094     case OMPD_target:
12095     case OMPD_target_simd:
12096     case OMPD_target_teams:
12097     case OMPD_target_parallel:
12098     case OMPD_target_teams_distribute:
12099     case OMPD_target_teams_distribute_simd:
12100     case OMPD_target_parallel_for:
12101     case OMPD_target_parallel_for_simd:
12102     case OMPD_target_teams_distribute_parallel_for:
12103     case OMPD_target_teams_distribute_parallel_for_simd:
12104       CaptureRegion = OMPD_task;
12105       break;
12106     case OMPD_target_data:
12107       // Do not capture device-clause expressions.
12108       break;
12109     case OMPD_teams_distribute_parallel_for:
12110     case OMPD_teams_distribute_parallel_for_simd:
12111     case OMPD_teams:
12112     case OMPD_teams_distribute:
12113     case OMPD_teams_distribute_simd:
12114     case OMPD_distribute_parallel_for:
12115     case OMPD_distribute_parallel_for_simd:
12116     case OMPD_task:
12117     case OMPD_taskloop:
12118     case OMPD_taskloop_simd:
12119     case OMPD_master_taskloop:
12120     case OMPD_master_taskloop_simd:
12121     case OMPD_parallel_master_taskloop:
12122     case OMPD_parallel_master_taskloop_simd:
12123     case OMPD_cancel:
12124     case OMPD_parallel:
12125     case OMPD_parallel_master:
12126     case OMPD_parallel_sections:
12127     case OMPD_parallel_for:
12128     case OMPD_parallel_for_simd:
12129     case OMPD_threadprivate:
12130     case OMPD_allocate:
12131     case OMPD_taskyield:
12132     case OMPD_barrier:
12133     case OMPD_taskwait:
12134     case OMPD_cancellation_point:
12135     case OMPD_flush:
12136     case OMPD_depobj:
12137     case OMPD_scan:
12138     case OMPD_declare_reduction:
12139     case OMPD_declare_mapper:
12140     case OMPD_declare_simd:
12141     case OMPD_declare_variant:
12142     case OMPD_begin_declare_variant:
12143     case OMPD_end_declare_variant:
12144     case OMPD_declare_target:
12145     case OMPD_end_declare_target:
12146     case OMPD_simd:
12147     case OMPD_for:
12148     case OMPD_for_simd:
12149     case OMPD_sections:
12150     case OMPD_section:
12151     case OMPD_single:
12152     case OMPD_master:
12153     case OMPD_critical:
12154     case OMPD_taskgroup:
12155     case OMPD_distribute:
12156     case OMPD_ordered:
12157     case OMPD_atomic:
12158     case OMPD_distribute_simd:
12159     case OMPD_requires:
12160       llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
12161     case OMPD_unknown:
12162       llvm_unreachable("Unknown OpenMP directive");
12163     }
12164     break;
12165   case OMPC_grainsize:
12166   case OMPC_num_tasks:
12167   case OMPC_final:
12168   case OMPC_priority:
12169     switch (DKind) {
12170     case OMPD_task:
12171     case OMPD_taskloop:
12172     case OMPD_taskloop_simd:
12173     case OMPD_master_taskloop:
12174     case OMPD_master_taskloop_simd:
12175       break;
12176     case OMPD_parallel_master_taskloop:
12177     case OMPD_parallel_master_taskloop_simd:
12178       CaptureRegion = OMPD_parallel;
12179       break;
12180     case OMPD_target_update:
12181     case OMPD_target_enter_data:
12182     case OMPD_target_exit_data:
12183     case OMPD_target:
12184     case OMPD_target_simd:
12185     case OMPD_target_teams:
12186     case OMPD_target_parallel:
12187     case OMPD_target_teams_distribute:
12188     case OMPD_target_teams_distribute_simd:
12189     case OMPD_target_parallel_for:
12190     case OMPD_target_parallel_for_simd:
12191     case OMPD_target_teams_distribute_parallel_for:
12192     case OMPD_target_teams_distribute_parallel_for_simd:
12193     case OMPD_target_data:
12194     case OMPD_teams_distribute_parallel_for:
12195     case OMPD_teams_distribute_parallel_for_simd:
12196     case OMPD_teams:
12197     case OMPD_teams_distribute:
12198     case OMPD_teams_distribute_simd:
12199     case OMPD_distribute_parallel_for:
12200     case OMPD_distribute_parallel_for_simd:
12201     case OMPD_cancel:
12202     case OMPD_parallel:
12203     case OMPD_parallel_master:
12204     case OMPD_parallel_sections:
12205     case OMPD_parallel_for:
12206     case OMPD_parallel_for_simd:
12207     case OMPD_threadprivate:
12208     case OMPD_allocate:
12209     case OMPD_taskyield:
12210     case OMPD_barrier:
12211     case OMPD_taskwait:
12212     case OMPD_cancellation_point:
12213     case OMPD_flush:
12214     case OMPD_depobj:
12215     case OMPD_scan:
12216     case OMPD_declare_reduction:
12217     case OMPD_declare_mapper:
12218     case OMPD_declare_simd:
12219     case OMPD_declare_variant:
12220     case OMPD_begin_declare_variant:
12221     case OMPD_end_declare_variant:
12222     case OMPD_declare_target:
12223     case OMPD_end_declare_target:
12224     case OMPD_simd:
12225     case OMPD_for:
12226     case OMPD_for_simd:
12227     case OMPD_sections:
12228     case OMPD_section:
12229     case OMPD_single:
12230     case OMPD_master:
12231     case OMPD_critical:
12232     case OMPD_taskgroup:
12233     case OMPD_distribute:
12234     case OMPD_ordered:
12235     case OMPD_atomic:
12236     case OMPD_distribute_simd:
12237     case OMPD_requires:
12238       llvm_unreachable("Unexpected OpenMP directive with grainsize-clause");
12239     case OMPD_unknown:
12240       llvm_unreachable("Unknown OpenMP directive");
12241     }
12242     break;
12243   case OMPC_firstprivate:
12244   case OMPC_lastprivate:
12245   case OMPC_reduction:
12246   case OMPC_task_reduction:
12247   case OMPC_in_reduction:
12248   case OMPC_linear:
12249   case OMPC_default:
12250   case OMPC_proc_bind:
12251   case OMPC_safelen:
12252   case OMPC_simdlen:
12253   case OMPC_allocator:
12254   case OMPC_collapse:
12255   case OMPC_private:
12256   case OMPC_shared:
12257   case OMPC_aligned:
12258   case OMPC_copyin:
12259   case OMPC_copyprivate:
12260   case OMPC_ordered:
12261   case OMPC_nowait:
12262   case OMPC_untied:
12263   case OMPC_mergeable:
12264   case OMPC_threadprivate:
12265   case OMPC_allocate:
12266   case OMPC_flush:
12267   case OMPC_depobj:
12268   case OMPC_read:
12269   case OMPC_write:
12270   case OMPC_update:
12271   case OMPC_capture:
12272   case OMPC_seq_cst:
12273   case OMPC_acq_rel:
12274   case OMPC_acquire:
12275   case OMPC_release:
12276   case OMPC_relaxed:
12277   case OMPC_depend:
12278   case OMPC_threads:
12279   case OMPC_simd:
12280   case OMPC_map:
12281   case OMPC_nogroup:
12282   case OMPC_hint:
12283   case OMPC_defaultmap:
12284   case OMPC_unknown:
12285   case OMPC_uniform:
12286   case OMPC_to:
12287   case OMPC_from:
12288   case OMPC_use_device_ptr:
12289   case OMPC_is_device_ptr:
12290   case OMPC_unified_address:
12291   case OMPC_unified_shared_memory:
12292   case OMPC_reverse_offload:
12293   case OMPC_dynamic_allocators:
12294   case OMPC_atomic_default_mem_order:
12295   case OMPC_device_type:
12296   case OMPC_match:
12297   case OMPC_nontemporal:
12298   case OMPC_order:
12299   case OMPC_destroy:
12300   case OMPC_detach:
12301   case OMPC_inclusive:
12302   case OMPC_exclusive:
12303   case OMPC_uses_allocators:
12304     llvm_unreachable("Unexpected OpenMP clause.");
12305   }
12306   return CaptureRegion;
12307 }
12308 
12309 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
12310                                      Expr *Condition, SourceLocation StartLoc,
12311                                      SourceLocation LParenLoc,
12312                                      SourceLocation NameModifierLoc,
12313                                      SourceLocation ColonLoc,
12314                                      SourceLocation EndLoc) {
12315   Expr *ValExpr = Condition;
12316   Stmt *HelperValStmt = nullptr;
12317   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
12318   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
12319       !Condition->isInstantiationDependent() &&
12320       !Condition->containsUnexpandedParameterPack()) {
12321     ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
12322     if (Val.isInvalid())
12323       return nullptr;
12324 
12325     ValExpr = Val.get();
12326 
12327     OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
12328     CaptureRegion = getOpenMPCaptureRegionForClause(
12329         DKind, OMPC_if, LangOpts.OpenMP, NameModifier);
12330     if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
12331       ValExpr = MakeFullExpr(ValExpr).get();
12332       llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
12333       ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12334       HelperValStmt = buildPreInits(Context, Captures);
12335     }
12336   }
12337 
12338   return new (Context)
12339       OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
12340                   LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
12341 }
12342 
12343 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
12344                                         SourceLocation StartLoc,
12345                                         SourceLocation LParenLoc,
12346                                         SourceLocation EndLoc) {
12347   Expr *ValExpr = Condition;
12348   Stmt *HelperValStmt = nullptr;
12349   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
12350   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
12351       !Condition->isInstantiationDependent() &&
12352       !Condition->containsUnexpandedParameterPack()) {
12353     ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
12354     if (Val.isInvalid())
12355       return nullptr;
12356 
12357     ValExpr = MakeFullExpr(Val.get()).get();
12358 
12359     OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
12360     CaptureRegion =
12361         getOpenMPCaptureRegionForClause(DKind, OMPC_final, LangOpts.OpenMP);
12362     if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
12363       ValExpr = MakeFullExpr(ValExpr).get();
12364       llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
12365       ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12366       HelperValStmt = buildPreInits(Context, Captures);
12367     }
12368   }
12369 
12370   return new (Context) OMPFinalClause(ValExpr, HelperValStmt, CaptureRegion,
12371                                       StartLoc, LParenLoc, EndLoc);
12372 }
12373 
12374 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
12375                                                         Expr *Op) {
12376   if (!Op)
12377     return ExprError();
12378 
12379   class IntConvertDiagnoser : public ICEConvertDiagnoser {
12380   public:
12381     IntConvertDiagnoser()
12382         : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
12383     SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
12384                                          QualType T) override {
12385       return S.Diag(Loc, diag::err_omp_not_integral) << T;
12386     }
12387     SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
12388                                              QualType T) override {
12389       return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
12390     }
12391     SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
12392                                                QualType T,
12393                                                QualType ConvTy) override {
12394       return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
12395     }
12396     SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
12397                                            QualType ConvTy) override {
12398       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
12399              << ConvTy->isEnumeralType() << ConvTy;
12400     }
12401     SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
12402                                             QualType T) override {
12403       return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
12404     }
12405     SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
12406                                         QualType ConvTy) override {
12407       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
12408              << ConvTy->isEnumeralType() << ConvTy;
12409     }
12410     SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
12411                                              QualType) override {
12412       llvm_unreachable("conversion functions are permitted");
12413     }
12414   } ConvertDiagnoser;
12415   return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
12416 }
12417 
12418 static bool
12419 isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, OpenMPClauseKind CKind,
12420                           bool StrictlyPositive, bool BuildCapture = false,
12421                           OpenMPDirectiveKind DKind = OMPD_unknown,
12422                           OpenMPDirectiveKind *CaptureRegion = nullptr,
12423                           Stmt **HelperValStmt = nullptr) {
12424   if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
12425       !ValExpr->isInstantiationDependent()) {
12426     SourceLocation Loc = ValExpr->getExprLoc();
12427     ExprResult Value =
12428         SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
12429     if (Value.isInvalid())
12430       return false;
12431 
12432     ValExpr = Value.get();
12433     // The expression must evaluate to a non-negative integer value.
12434     llvm::APSInt Result;
12435     if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
12436         Result.isSigned() &&
12437         !((!StrictlyPositive && Result.isNonNegative()) ||
12438           (StrictlyPositive && Result.isStrictlyPositive()))) {
12439       SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
12440           << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
12441           << ValExpr->getSourceRange();
12442       return false;
12443     }
12444     if (!BuildCapture)
12445       return true;
12446     *CaptureRegion =
12447         getOpenMPCaptureRegionForClause(DKind, CKind, SemaRef.LangOpts.OpenMP);
12448     if (*CaptureRegion != OMPD_unknown &&
12449         !SemaRef.CurContext->isDependentContext()) {
12450       ValExpr = SemaRef.MakeFullExpr(ValExpr).get();
12451       llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
12452       ValExpr = tryBuildCapture(SemaRef, ValExpr, Captures).get();
12453       *HelperValStmt = buildPreInits(SemaRef.Context, Captures);
12454     }
12455   }
12456   return true;
12457 }
12458 
12459 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
12460                                              SourceLocation StartLoc,
12461                                              SourceLocation LParenLoc,
12462                                              SourceLocation EndLoc) {
12463   Expr *ValExpr = NumThreads;
12464   Stmt *HelperValStmt = nullptr;
12465 
12466   // OpenMP [2.5, Restrictions]
12467   //  The num_threads expression must evaluate to a positive integer value.
12468   if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
12469                                  /*StrictlyPositive=*/true))
12470     return nullptr;
12471 
12472   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
12473   OpenMPDirectiveKind CaptureRegion =
12474       getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads, LangOpts.OpenMP);
12475   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
12476     ValExpr = MakeFullExpr(ValExpr).get();
12477     llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
12478     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12479     HelperValStmt = buildPreInits(Context, Captures);
12480   }
12481 
12482   return new (Context) OMPNumThreadsClause(
12483       ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
12484 }
12485 
12486 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
12487                                                        OpenMPClauseKind CKind,
12488                                                        bool StrictlyPositive) {
12489   if (!E)
12490     return ExprError();
12491   if (E->isValueDependent() || E->isTypeDependent() ||
12492       E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
12493     return E;
12494   llvm::APSInt Result;
12495   ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
12496   if (ICE.isInvalid())
12497     return ExprError();
12498   if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
12499       (!StrictlyPositive && !Result.isNonNegative())) {
12500     Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
12501         << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
12502         << E->getSourceRange();
12503     return ExprError();
12504   }
12505   if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
12506     Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
12507         << E->getSourceRange();
12508     return ExprError();
12509   }
12510   if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
12511     DSAStack->setAssociatedLoops(Result.getExtValue());
12512   else if (CKind == OMPC_ordered)
12513     DSAStack->setAssociatedLoops(Result.getExtValue());
12514   return ICE;
12515 }
12516 
12517 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
12518                                           SourceLocation LParenLoc,
12519                                           SourceLocation EndLoc) {
12520   // OpenMP [2.8.1, simd construct, Description]
12521   // The parameter of the safelen clause must be a constant
12522   // positive integer expression.
12523   ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
12524   if (Safelen.isInvalid())
12525     return nullptr;
12526   return new (Context)
12527       OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
12528 }
12529 
12530 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
12531                                           SourceLocation LParenLoc,
12532                                           SourceLocation EndLoc) {
12533   // OpenMP [2.8.1, simd construct, Description]
12534   // The parameter of the simdlen clause must be a constant
12535   // positive integer expression.
12536   ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
12537   if (Simdlen.isInvalid())
12538     return nullptr;
12539   return new (Context)
12540       OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
12541 }
12542 
12543 /// Tries to find omp_allocator_handle_t type.
12544 static bool findOMPAllocatorHandleT(Sema &S, SourceLocation Loc,
12545                                     DSAStackTy *Stack) {
12546   QualType OMPAllocatorHandleT = Stack->getOMPAllocatorHandleT();
12547   if (!OMPAllocatorHandleT.isNull())
12548     return true;
12549   // Build the predefined allocator expressions.
12550   bool ErrorFound = false;
12551   for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
12552     auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
12553     StringRef Allocator =
12554         OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind);
12555     DeclarationName AllocatorName = &S.getASTContext().Idents.get(Allocator);
12556     auto *VD = dyn_cast_or_null<ValueDecl>(
12557         S.LookupSingleName(S.TUScope, AllocatorName, Loc, Sema::LookupAnyName));
12558     if (!VD) {
12559       ErrorFound = true;
12560       break;
12561     }
12562     QualType AllocatorType =
12563         VD->getType().getNonLValueExprType(S.getASTContext());
12564     ExprResult Res = S.BuildDeclRefExpr(VD, AllocatorType, VK_LValue, Loc);
12565     if (!Res.isUsable()) {
12566       ErrorFound = true;
12567       break;
12568     }
12569     if (OMPAllocatorHandleT.isNull())
12570       OMPAllocatorHandleT = AllocatorType;
12571     if (!S.getASTContext().hasSameType(OMPAllocatorHandleT, AllocatorType)) {
12572       ErrorFound = true;
12573       break;
12574     }
12575     Stack->setAllocator(AllocatorKind, Res.get());
12576   }
12577   if (ErrorFound) {
12578     S.Diag(Loc, diag::err_omp_implied_type_not_found)
12579         << "omp_allocator_handle_t";
12580     return false;
12581   }
12582   OMPAllocatorHandleT.addConst();
12583   Stack->setOMPAllocatorHandleT(OMPAllocatorHandleT);
12584   return true;
12585 }
12586 
12587 OMPClause *Sema::ActOnOpenMPAllocatorClause(Expr *A, SourceLocation StartLoc,
12588                                             SourceLocation LParenLoc,
12589                                             SourceLocation EndLoc) {
12590   // OpenMP [2.11.3, allocate Directive, Description]
12591   // allocator is an expression of omp_allocator_handle_t type.
12592   if (!findOMPAllocatorHandleT(*this, A->getExprLoc(), DSAStack))
12593     return nullptr;
12594 
12595   ExprResult Allocator = DefaultLvalueConversion(A);
12596   if (Allocator.isInvalid())
12597     return nullptr;
12598   Allocator = PerformImplicitConversion(Allocator.get(),
12599                                         DSAStack->getOMPAllocatorHandleT(),
12600                                         Sema::AA_Initializing,
12601                                         /*AllowExplicit=*/true);
12602   if (Allocator.isInvalid())
12603     return nullptr;
12604   return new (Context)
12605       OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc);
12606 }
12607 
12608 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
12609                                            SourceLocation StartLoc,
12610                                            SourceLocation LParenLoc,
12611                                            SourceLocation EndLoc) {
12612   // OpenMP [2.7.1, loop construct, Description]
12613   // OpenMP [2.8.1, simd construct, Description]
12614   // OpenMP [2.9.6, distribute construct, Description]
12615   // The parameter of the collapse clause must be a constant
12616   // positive integer expression.
12617   ExprResult NumForLoopsResult =
12618       VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
12619   if (NumForLoopsResult.isInvalid())
12620     return nullptr;
12621   return new (Context)
12622       OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
12623 }
12624 
12625 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
12626                                           SourceLocation EndLoc,
12627                                           SourceLocation LParenLoc,
12628                                           Expr *NumForLoops) {
12629   // OpenMP [2.7.1, loop construct, Description]
12630   // OpenMP [2.8.1, simd construct, Description]
12631   // OpenMP [2.9.6, distribute construct, Description]
12632   // The parameter of the ordered clause must be a constant
12633   // positive integer expression if any.
12634   if (NumForLoops && LParenLoc.isValid()) {
12635     ExprResult NumForLoopsResult =
12636         VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
12637     if (NumForLoopsResult.isInvalid())
12638       return nullptr;
12639     NumForLoops = NumForLoopsResult.get();
12640   } else {
12641     NumForLoops = nullptr;
12642   }
12643   auto *Clause = OMPOrderedClause::Create(
12644       Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0,
12645       StartLoc, LParenLoc, EndLoc);
12646   DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause);
12647   return Clause;
12648 }
12649 
12650 OMPClause *Sema::ActOnOpenMPSimpleClause(
12651     OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
12652     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
12653   OMPClause *Res = nullptr;
12654   switch (Kind) {
12655   case OMPC_default:
12656     Res = ActOnOpenMPDefaultClause(static_cast<DefaultKind>(Argument),
12657                                    ArgumentLoc, StartLoc, LParenLoc, EndLoc);
12658     break;
12659   case OMPC_proc_bind:
12660     Res = ActOnOpenMPProcBindClause(static_cast<ProcBindKind>(Argument),
12661                                     ArgumentLoc, StartLoc, LParenLoc, EndLoc);
12662     break;
12663   case OMPC_atomic_default_mem_order:
12664     Res = ActOnOpenMPAtomicDefaultMemOrderClause(
12665         static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument),
12666         ArgumentLoc, StartLoc, LParenLoc, EndLoc);
12667     break;
12668   case OMPC_order:
12669     Res = ActOnOpenMPOrderClause(static_cast<OpenMPOrderClauseKind>(Argument),
12670                                  ArgumentLoc, StartLoc, LParenLoc, EndLoc);
12671     break;
12672   case OMPC_update:
12673     Res = ActOnOpenMPUpdateClause(static_cast<OpenMPDependClauseKind>(Argument),
12674                                   ArgumentLoc, StartLoc, LParenLoc, EndLoc);
12675     break;
12676   case OMPC_if:
12677   case OMPC_final:
12678   case OMPC_num_threads:
12679   case OMPC_safelen:
12680   case OMPC_simdlen:
12681   case OMPC_allocator:
12682   case OMPC_collapse:
12683   case OMPC_schedule:
12684   case OMPC_private:
12685   case OMPC_firstprivate:
12686   case OMPC_lastprivate:
12687   case OMPC_shared:
12688   case OMPC_reduction:
12689   case OMPC_task_reduction:
12690   case OMPC_in_reduction:
12691   case OMPC_linear:
12692   case OMPC_aligned:
12693   case OMPC_copyin:
12694   case OMPC_copyprivate:
12695   case OMPC_ordered:
12696   case OMPC_nowait:
12697   case OMPC_untied:
12698   case OMPC_mergeable:
12699   case OMPC_threadprivate:
12700   case OMPC_allocate:
12701   case OMPC_flush:
12702   case OMPC_depobj:
12703   case OMPC_read:
12704   case OMPC_write:
12705   case OMPC_capture:
12706   case OMPC_seq_cst:
12707   case OMPC_acq_rel:
12708   case OMPC_acquire:
12709   case OMPC_release:
12710   case OMPC_relaxed:
12711   case OMPC_depend:
12712   case OMPC_device:
12713   case OMPC_threads:
12714   case OMPC_simd:
12715   case OMPC_map:
12716   case OMPC_num_teams:
12717   case OMPC_thread_limit:
12718   case OMPC_priority:
12719   case OMPC_grainsize:
12720   case OMPC_nogroup:
12721   case OMPC_num_tasks:
12722   case OMPC_hint:
12723   case OMPC_dist_schedule:
12724   case OMPC_defaultmap:
12725   case OMPC_unknown:
12726   case OMPC_uniform:
12727   case OMPC_to:
12728   case OMPC_from:
12729   case OMPC_use_device_ptr:
12730   case OMPC_is_device_ptr:
12731   case OMPC_unified_address:
12732   case OMPC_unified_shared_memory:
12733   case OMPC_reverse_offload:
12734   case OMPC_dynamic_allocators:
12735   case OMPC_device_type:
12736   case OMPC_match:
12737   case OMPC_nontemporal:
12738   case OMPC_destroy:
12739   case OMPC_detach:
12740   case OMPC_inclusive:
12741   case OMPC_exclusive:
12742   case OMPC_uses_allocators:
12743     llvm_unreachable("Clause is not allowed.");
12744   }
12745   return Res;
12746 }
12747 
12748 static std::string
12749 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
12750                         ArrayRef<unsigned> Exclude = llvm::None) {
12751   SmallString<256> Buffer;
12752   llvm::raw_svector_ostream Out(Buffer);
12753   unsigned Skipped = Exclude.size();
12754   auto S = Exclude.begin(), E = Exclude.end();
12755   for (unsigned I = First; I < Last; ++I) {
12756     if (std::find(S, E, I) != E) {
12757       --Skipped;
12758       continue;
12759     }
12760     Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'";
12761     if (I + Skipped + 2 == Last)
12762       Out << " or ";
12763     else if (I + Skipped + 1 != Last)
12764       Out << ", ";
12765   }
12766   return std::string(Out.str());
12767 }
12768 
12769 OMPClause *Sema::ActOnOpenMPDefaultClause(DefaultKind Kind,
12770                                           SourceLocation KindKwLoc,
12771                                           SourceLocation StartLoc,
12772                                           SourceLocation LParenLoc,
12773                                           SourceLocation EndLoc) {
12774   if (Kind == OMP_DEFAULT_unknown) {
12775     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
12776         << getListOfPossibleValues(OMPC_default, /*First=*/0,
12777                                    /*Last=*/unsigned(OMP_DEFAULT_unknown))
12778         << getOpenMPClauseName(OMPC_default);
12779     return nullptr;
12780   }
12781   if (Kind == OMP_DEFAULT_none)
12782     DSAStack->setDefaultDSANone(KindKwLoc);
12783   else if (Kind == OMP_DEFAULT_shared)
12784     DSAStack->setDefaultDSAShared(KindKwLoc);
12785 
12786   return new (Context)
12787       OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
12788 }
12789 
12790 OMPClause *Sema::ActOnOpenMPProcBindClause(ProcBindKind Kind,
12791                                            SourceLocation KindKwLoc,
12792                                            SourceLocation StartLoc,
12793                                            SourceLocation LParenLoc,
12794                                            SourceLocation EndLoc) {
12795   if (Kind == OMP_PROC_BIND_unknown) {
12796     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
12797         << getListOfPossibleValues(OMPC_proc_bind,
12798                                    /*First=*/unsigned(OMP_PROC_BIND_master),
12799                                    /*Last=*/5)
12800         << getOpenMPClauseName(OMPC_proc_bind);
12801     return nullptr;
12802   }
12803   return new (Context)
12804       OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
12805 }
12806 
12807 OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause(
12808     OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc,
12809     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
12810   if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) {
12811     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
12812         << getListOfPossibleValues(
12813                OMPC_atomic_default_mem_order, /*First=*/0,
12814                /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown)
12815         << getOpenMPClauseName(OMPC_atomic_default_mem_order);
12816     return nullptr;
12817   }
12818   return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc,
12819                                                       LParenLoc, EndLoc);
12820 }
12821 
12822 OMPClause *Sema::ActOnOpenMPOrderClause(OpenMPOrderClauseKind Kind,
12823                                         SourceLocation KindKwLoc,
12824                                         SourceLocation StartLoc,
12825                                         SourceLocation LParenLoc,
12826                                         SourceLocation EndLoc) {
12827   if (Kind == OMPC_ORDER_unknown) {
12828     static_assert(OMPC_ORDER_unknown > 0,
12829                   "OMPC_ORDER_unknown not greater than 0");
12830     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
12831         << getListOfPossibleValues(OMPC_order, /*First=*/0,
12832                                    /*Last=*/OMPC_ORDER_unknown)
12833         << getOpenMPClauseName(OMPC_order);
12834     return nullptr;
12835   }
12836   return new (Context)
12837       OMPOrderClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
12838 }
12839 
12840 OMPClause *Sema::ActOnOpenMPUpdateClause(OpenMPDependClauseKind Kind,
12841                                          SourceLocation KindKwLoc,
12842                                          SourceLocation StartLoc,
12843                                          SourceLocation LParenLoc,
12844                                          SourceLocation EndLoc) {
12845   if (Kind == OMPC_DEPEND_unknown || Kind == OMPC_DEPEND_source ||
12846       Kind == OMPC_DEPEND_sink || Kind == OMPC_DEPEND_depobj) {
12847     unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink,
12848                          OMPC_DEPEND_depobj};
12849     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
12850         << getListOfPossibleValues(OMPC_depend, /*First=*/0,
12851                                    /*Last=*/OMPC_DEPEND_unknown, Except)
12852         << getOpenMPClauseName(OMPC_update);
12853     return nullptr;
12854   }
12855   return OMPUpdateClause::Create(Context, StartLoc, LParenLoc, KindKwLoc, Kind,
12856                                  EndLoc);
12857 }
12858 
12859 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
12860     OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
12861     SourceLocation StartLoc, SourceLocation LParenLoc,
12862     ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
12863     SourceLocation EndLoc) {
12864   OMPClause *Res = nullptr;
12865   switch (Kind) {
12866   case OMPC_schedule:
12867     enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
12868     assert(Argument.size() == NumberOfElements &&
12869            ArgumentLoc.size() == NumberOfElements);
12870     Res = ActOnOpenMPScheduleClause(
12871         static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
12872         static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
12873         static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
12874         StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
12875         ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
12876     break;
12877   case OMPC_if:
12878     assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
12879     Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
12880                               Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
12881                               DelimLoc, EndLoc);
12882     break;
12883   case OMPC_dist_schedule:
12884     Res = ActOnOpenMPDistScheduleClause(
12885         static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
12886         StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
12887     break;
12888   case OMPC_defaultmap:
12889     enum { Modifier, DefaultmapKind };
12890     Res = ActOnOpenMPDefaultmapClause(
12891         static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
12892         static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
12893         StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
12894         EndLoc);
12895     break;
12896   case OMPC_device:
12897     assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
12898     Res = ActOnOpenMPDeviceClause(
12899         static_cast<OpenMPDeviceClauseModifier>(Argument.back()), Expr,
12900         StartLoc, LParenLoc, ArgumentLoc.back(), EndLoc);
12901     break;
12902   case OMPC_final:
12903   case OMPC_num_threads:
12904   case OMPC_safelen:
12905   case OMPC_simdlen:
12906   case OMPC_allocator:
12907   case OMPC_collapse:
12908   case OMPC_default:
12909   case OMPC_proc_bind:
12910   case OMPC_private:
12911   case OMPC_firstprivate:
12912   case OMPC_lastprivate:
12913   case OMPC_shared:
12914   case OMPC_reduction:
12915   case OMPC_task_reduction:
12916   case OMPC_in_reduction:
12917   case OMPC_linear:
12918   case OMPC_aligned:
12919   case OMPC_copyin:
12920   case OMPC_copyprivate:
12921   case OMPC_ordered:
12922   case OMPC_nowait:
12923   case OMPC_untied:
12924   case OMPC_mergeable:
12925   case OMPC_threadprivate:
12926   case OMPC_allocate:
12927   case OMPC_flush:
12928   case OMPC_depobj:
12929   case OMPC_read:
12930   case OMPC_write:
12931   case OMPC_update:
12932   case OMPC_capture:
12933   case OMPC_seq_cst:
12934   case OMPC_acq_rel:
12935   case OMPC_acquire:
12936   case OMPC_release:
12937   case OMPC_relaxed:
12938   case OMPC_depend:
12939   case OMPC_threads:
12940   case OMPC_simd:
12941   case OMPC_map:
12942   case OMPC_num_teams:
12943   case OMPC_thread_limit:
12944   case OMPC_priority:
12945   case OMPC_grainsize:
12946   case OMPC_nogroup:
12947   case OMPC_num_tasks:
12948   case OMPC_hint:
12949   case OMPC_unknown:
12950   case OMPC_uniform:
12951   case OMPC_to:
12952   case OMPC_from:
12953   case OMPC_use_device_ptr:
12954   case OMPC_is_device_ptr:
12955   case OMPC_unified_address:
12956   case OMPC_unified_shared_memory:
12957   case OMPC_reverse_offload:
12958   case OMPC_dynamic_allocators:
12959   case OMPC_atomic_default_mem_order:
12960   case OMPC_device_type:
12961   case OMPC_match:
12962   case OMPC_nontemporal:
12963   case OMPC_order:
12964   case OMPC_destroy:
12965   case OMPC_detach:
12966   case OMPC_inclusive:
12967   case OMPC_exclusive:
12968   case OMPC_uses_allocators:
12969     llvm_unreachable("Clause is not allowed.");
12970   }
12971   return Res;
12972 }
12973 
12974 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
12975                                    OpenMPScheduleClauseModifier M2,
12976                                    SourceLocation M1Loc, SourceLocation M2Loc) {
12977   if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
12978     SmallVector<unsigned, 2> Excluded;
12979     if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
12980       Excluded.push_back(M2);
12981     if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
12982       Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
12983     if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
12984       Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
12985     S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
12986         << getListOfPossibleValues(OMPC_schedule,
12987                                    /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
12988                                    /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
12989                                    Excluded)
12990         << getOpenMPClauseName(OMPC_schedule);
12991     return true;
12992   }
12993   return false;
12994 }
12995 
12996 OMPClause *Sema::ActOnOpenMPScheduleClause(
12997     OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
12998     OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
12999     SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
13000     SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
13001   if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
13002       checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
13003     return nullptr;
13004   // OpenMP, 2.7.1, Loop Construct, Restrictions
13005   // Either the monotonic modifier or the nonmonotonic modifier can be specified
13006   // but not both.
13007   if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
13008       (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
13009        M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
13010       (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
13011        M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
13012     Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
13013         << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
13014         << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
13015     return nullptr;
13016   }
13017   if (Kind == OMPC_SCHEDULE_unknown) {
13018     std::string Values;
13019     if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
13020       unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
13021       Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
13022                                        /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
13023                                        Exclude);
13024     } else {
13025       Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
13026                                        /*Last=*/OMPC_SCHEDULE_unknown);
13027     }
13028     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
13029         << Values << getOpenMPClauseName(OMPC_schedule);
13030     return nullptr;
13031   }
13032   // OpenMP, 2.7.1, Loop Construct, Restrictions
13033   // The nonmonotonic modifier can only be specified with schedule(dynamic) or
13034   // schedule(guided).
13035   if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
13036        M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
13037       Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
13038     Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
13039          diag::err_omp_schedule_nonmonotonic_static);
13040     return nullptr;
13041   }
13042   Expr *ValExpr = ChunkSize;
13043   Stmt *HelperValStmt = nullptr;
13044   if (ChunkSize) {
13045     if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
13046         !ChunkSize->isInstantiationDependent() &&
13047         !ChunkSize->containsUnexpandedParameterPack()) {
13048       SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
13049       ExprResult Val =
13050           PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
13051       if (Val.isInvalid())
13052         return nullptr;
13053 
13054       ValExpr = Val.get();
13055 
13056       // OpenMP [2.7.1, Restrictions]
13057       //  chunk_size must be a loop invariant integer expression with a positive
13058       //  value.
13059       llvm::APSInt Result;
13060       if (ValExpr->isIntegerConstantExpr(Result, Context)) {
13061         if (Result.isSigned() && !Result.isStrictlyPositive()) {
13062           Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
13063               << "schedule" << 1 << ChunkSize->getSourceRange();
13064           return nullptr;
13065         }
13066       } else if (getOpenMPCaptureRegionForClause(
13067                      DSAStack->getCurrentDirective(), OMPC_schedule,
13068                      LangOpts.OpenMP) != OMPD_unknown &&
13069                  !CurContext->isDependentContext()) {
13070         ValExpr = MakeFullExpr(ValExpr).get();
13071         llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
13072         ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13073         HelperValStmt = buildPreInits(Context, Captures);
13074       }
13075     }
13076   }
13077 
13078   return new (Context)
13079       OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
13080                         ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
13081 }
13082 
13083 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
13084                                    SourceLocation StartLoc,
13085                                    SourceLocation EndLoc) {
13086   OMPClause *Res = nullptr;
13087   switch (Kind) {
13088   case OMPC_ordered:
13089     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
13090     break;
13091   case OMPC_nowait:
13092     Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
13093     break;
13094   case OMPC_untied:
13095     Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
13096     break;
13097   case OMPC_mergeable:
13098     Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
13099     break;
13100   case OMPC_read:
13101     Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
13102     break;
13103   case OMPC_write:
13104     Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
13105     break;
13106   case OMPC_update:
13107     Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
13108     break;
13109   case OMPC_capture:
13110     Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
13111     break;
13112   case OMPC_seq_cst:
13113     Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
13114     break;
13115   case OMPC_acq_rel:
13116     Res = ActOnOpenMPAcqRelClause(StartLoc, EndLoc);
13117     break;
13118   case OMPC_acquire:
13119     Res = ActOnOpenMPAcquireClause(StartLoc, EndLoc);
13120     break;
13121   case OMPC_release:
13122     Res = ActOnOpenMPReleaseClause(StartLoc, EndLoc);
13123     break;
13124   case OMPC_relaxed:
13125     Res = ActOnOpenMPRelaxedClause(StartLoc, EndLoc);
13126     break;
13127   case OMPC_threads:
13128     Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
13129     break;
13130   case OMPC_simd:
13131     Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
13132     break;
13133   case OMPC_nogroup:
13134     Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
13135     break;
13136   case OMPC_unified_address:
13137     Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc);
13138     break;
13139   case OMPC_unified_shared_memory:
13140     Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
13141     break;
13142   case OMPC_reverse_offload:
13143     Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc);
13144     break;
13145   case OMPC_dynamic_allocators:
13146     Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc);
13147     break;
13148   case OMPC_destroy:
13149     Res = ActOnOpenMPDestroyClause(StartLoc, EndLoc);
13150     break;
13151   case OMPC_if:
13152   case OMPC_final:
13153   case OMPC_num_threads:
13154   case OMPC_safelen:
13155   case OMPC_simdlen:
13156   case OMPC_allocator:
13157   case OMPC_collapse:
13158   case OMPC_schedule:
13159   case OMPC_private:
13160   case OMPC_firstprivate:
13161   case OMPC_lastprivate:
13162   case OMPC_shared:
13163   case OMPC_reduction:
13164   case OMPC_task_reduction:
13165   case OMPC_in_reduction:
13166   case OMPC_linear:
13167   case OMPC_aligned:
13168   case OMPC_copyin:
13169   case OMPC_copyprivate:
13170   case OMPC_default:
13171   case OMPC_proc_bind:
13172   case OMPC_threadprivate:
13173   case OMPC_allocate:
13174   case OMPC_flush:
13175   case OMPC_depobj:
13176   case OMPC_depend:
13177   case OMPC_device:
13178   case OMPC_map:
13179   case OMPC_num_teams:
13180   case OMPC_thread_limit:
13181   case OMPC_priority:
13182   case OMPC_grainsize:
13183   case OMPC_num_tasks:
13184   case OMPC_hint:
13185   case OMPC_dist_schedule:
13186   case OMPC_defaultmap:
13187   case OMPC_unknown:
13188   case OMPC_uniform:
13189   case OMPC_to:
13190   case OMPC_from:
13191   case OMPC_use_device_ptr:
13192   case OMPC_is_device_ptr:
13193   case OMPC_atomic_default_mem_order:
13194   case OMPC_device_type:
13195   case OMPC_match:
13196   case OMPC_nontemporal:
13197   case OMPC_order:
13198   case OMPC_detach:
13199   case OMPC_inclusive:
13200   case OMPC_exclusive:
13201   case OMPC_uses_allocators:
13202     llvm_unreachable("Clause is not allowed.");
13203   }
13204   return Res;
13205 }
13206 
13207 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
13208                                          SourceLocation EndLoc) {
13209   DSAStack->setNowaitRegion();
13210   return new (Context) OMPNowaitClause(StartLoc, EndLoc);
13211 }
13212 
13213 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
13214                                          SourceLocation EndLoc) {
13215   return new (Context) OMPUntiedClause(StartLoc, EndLoc);
13216 }
13217 
13218 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
13219                                             SourceLocation EndLoc) {
13220   return new (Context) OMPMergeableClause(StartLoc, EndLoc);
13221 }
13222 
13223 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
13224                                        SourceLocation EndLoc) {
13225   return new (Context) OMPReadClause(StartLoc, EndLoc);
13226 }
13227 
13228 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
13229                                         SourceLocation EndLoc) {
13230   return new (Context) OMPWriteClause(StartLoc, EndLoc);
13231 }
13232 
13233 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
13234                                          SourceLocation EndLoc) {
13235   return OMPUpdateClause::Create(Context, StartLoc, EndLoc);
13236 }
13237 
13238 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
13239                                           SourceLocation EndLoc) {
13240   return new (Context) OMPCaptureClause(StartLoc, EndLoc);
13241 }
13242 
13243 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
13244                                          SourceLocation EndLoc) {
13245   return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
13246 }
13247 
13248 OMPClause *Sema::ActOnOpenMPAcqRelClause(SourceLocation StartLoc,
13249                                          SourceLocation EndLoc) {
13250   return new (Context) OMPAcqRelClause(StartLoc, EndLoc);
13251 }
13252 
13253 OMPClause *Sema::ActOnOpenMPAcquireClause(SourceLocation StartLoc,
13254                                           SourceLocation EndLoc) {
13255   return new (Context) OMPAcquireClause(StartLoc, EndLoc);
13256 }
13257 
13258 OMPClause *Sema::ActOnOpenMPReleaseClause(SourceLocation StartLoc,
13259                                           SourceLocation EndLoc) {
13260   return new (Context) OMPReleaseClause(StartLoc, EndLoc);
13261 }
13262 
13263 OMPClause *Sema::ActOnOpenMPRelaxedClause(SourceLocation StartLoc,
13264                                           SourceLocation EndLoc) {
13265   return new (Context) OMPRelaxedClause(StartLoc, EndLoc);
13266 }
13267 
13268 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
13269                                           SourceLocation EndLoc) {
13270   return new (Context) OMPThreadsClause(StartLoc, EndLoc);
13271 }
13272 
13273 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
13274                                        SourceLocation EndLoc) {
13275   return new (Context) OMPSIMDClause(StartLoc, EndLoc);
13276 }
13277 
13278 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
13279                                           SourceLocation EndLoc) {
13280   return new (Context) OMPNogroupClause(StartLoc, EndLoc);
13281 }
13282 
13283 OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc,
13284                                                  SourceLocation EndLoc) {
13285   return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc);
13286 }
13287 
13288 OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc,
13289                                                       SourceLocation EndLoc) {
13290   return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
13291 }
13292 
13293 OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc,
13294                                                  SourceLocation EndLoc) {
13295   return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc);
13296 }
13297 
13298 OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc,
13299                                                     SourceLocation EndLoc) {
13300   return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc);
13301 }
13302 
13303 OMPClause *Sema::ActOnOpenMPDestroyClause(SourceLocation StartLoc,
13304                                           SourceLocation EndLoc) {
13305   return new (Context) OMPDestroyClause(StartLoc, EndLoc);
13306 }
13307 
13308 OMPClause *Sema::ActOnOpenMPVarListClause(
13309     OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *DepModOrTailExpr,
13310     const OMPVarListLocTy &Locs, SourceLocation ColonLoc,
13311     CXXScopeSpec &ReductionOrMapperIdScopeSpec,
13312     DeclarationNameInfo &ReductionOrMapperId, int ExtraModifier,
13313     ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
13314     ArrayRef<SourceLocation> MapTypeModifiersLoc, bool IsMapTypeImplicit,
13315     SourceLocation ExtraModifierLoc) {
13316   SourceLocation StartLoc = Locs.StartLoc;
13317   SourceLocation LParenLoc = Locs.LParenLoc;
13318   SourceLocation EndLoc = Locs.EndLoc;
13319   OMPClause *Res = nullptr;
13320   switch (Kind) {
13321   case OMPC_private:
13322     Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
13323     break;
13324   case OMPC_firstprivate:
13325     Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
13326     break;
13327   case OMPC_lastprivate:
13328     assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LASTPRIVATE_unknown &&
13329            "Unexpected lastprivate modifier.");
13330     Res = ActOnOpenMPLastprivateClause(
13331         VarList, static_cast<OpenMPLastprivateModifier>(ExtraModifier),
13332         ExtraModifierLoc, ColonLoc, StartLoc, LParenLoc, EndLoc);
13333     break;
13334   case OMPC_shared:
13335     Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
13336     break;
13337   case OMPC_reduction:
13338     assert(0 <= ExtraModifier && ExtraModifier <= OMPC_REDUCTION_unknown &&
13339            "Unexpected lastprivate modifier.");
13340     Res = ActOnOpenMPReductionClause(
13341         VarList, static_cast<OpenMPReductionClauseModifier>(ExtraModifier),
13342         StartLoc, LParenLoc, ExtraModifierLoc, ColonLoc, EndLoc,
13343         ReductionOrMapperIdScopeSpec, ReductionOrMapperId);
13344     break;
13345   case OMPC_task_reduction:
13346     Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
13347                                          EndLoc, ReductionOrMapperIdScopeSpec,
13348                                          ReductionOrMapperId);
13349     break;
13350   case OMPC_in_reduction:
13351     Res = ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
13352                                        EndLoc, ReductionOrMapperIdScopeSpec,
13353                                        ReductionOrMapperId);
13354     break;
13355   case OMPC_linear:
13356     assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LINEAR_unknown &&
13357            "Unexpected linear modifier.");
13358     Res = ActOnOpenMPLinearClause(
13359         VarList, DepModOrTailExpr, StartLoc, LParenLoc,
13360         static_cast<OpenMPLinearClauseKind>(ExtraModifier), ExtraModifierLoc,
13361         ColonLoc, EndLoc);
13362     break;
13363   case OMPC_aligned:
13364     Res = ActOnOpenMPAlignedClause(VarList, DepModOrTailExpr, StartLoc,
13365                                    LParenLoc, ColonLoc, EndLoc);
13366     break;
13367   case OMPC_copyin:
13368     Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
13369     break;
13370   case OMPC_copyprivate:
13371     Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
13372     break;
13373   case OMPC_flush:
13374     Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
13375     break;
13376   case OMPC_depend:
13377     assert(0 <= ExtraModifier && ExtraModifier <= OMPC_DEPEND_unknown &&
13378            "Unexpected depend modifier.");
13379     Res = ActOnOpenMPDependClause(
13380         DepModOrTailExpr, static_cast<OpenMPDependClauseKind>(ExtraModifier),
13381         ExtraModifierLoc, ColonLoc, VarList, StartLoc, LParenLoc, EndLoc);
13382     break;
13383   case OMPC_map:
13384     assert(0 <= ExtraModifier && ExtraModifier <= OMPC_MAP_unknown &&
13385            "Unexpected map modifier.");
13386     Res = ActOnOpenMPMapClause(
13387         MapTypeModifiers, MapTypeModifiersLoc, ReductionOrMapperIdScopeSpec,
13388         ReductionOrMapperId, static_cast<OpenMPMapClauseKind>(ExtraModifier),
13389         IsMapTypeImplicit, ExtraModifierLoc, ColonLoc, VarList, Locs);
13390     break;
13391   case OMPC_to:
13392     Res = ActOnOpenMPToClause(VarList, ReductionOrMapperIdScopeSpec,
13393                               ReductionOrMapperId, Locs);
13394     break;
13395   case OMPC_from:
13396     Res = ActOnOpenMPFromClause(VarList, ReductionOrMapperIdScopeSpec,
13397                                 ReductionOrMapperId, Locs);
13398     break;
13399   case OMPC_use_device_ptr:
13400     Res = ActOnOpenMPUseDevicePtrClause(VarList, Locs);
13401     break;
13402   case OMPC_is_device_ptr:
13403     Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs);
13404     break;
13405   case OMPC_allocate:
13406     Res = ActOnOpenMPAllocateClause(DepModOrTailExpr, VarList, StartLoc,
13407                                     LParenLoc, ColonLoc, EndLoc);
13408     break;
13409   case OMPC_nontemporal:
13410     Res = ActOnOpenMPNontemporalClause(VarList, StartLoc, LParenLoc, EndLoc);
13411     break;
13412   case OMPC_inclusive:
13413     Res = ActOnOpenMPInclusiveClause(VarList, StartLoc, LParenLoc, EndLoc);
13414     break;
13415   case OMPC_exclusive:
13416     Res = ActOnOpenMPExclusiveClause(VarList, StartLoc, LParenLoc, EndLoc);
13417     break;
13418   case OMPC_if:
13419   case OMPC_depobj:
13420   case OMPC_final:
13421   case OMPC_num_threads:
13422   case OMPC_safelen:
13423   case OMPC_simdlen:
13424   case OMPC_allocator:
13425   case OMPC_collapse:
13426   case OMPC_default:
13427   case OMPC_proc_bind:
13428   case OMPC_schedule:
13429   case OMPC_ordered:
13430   case OMPC_nowait:
13431   case OMPC_untied:
13432   case OMPC_mergeable:
13433   case OMPC_threadprivate:
13434   case OMPC_read:
13435   case OMPC_write:
13436   case OMPC_update:
13437   case OMPC_capture:
13438   case OMPC_seq_cst:
13439   case OMPC_acq_rel:
13440   case OMPC_acquire:
13441   case OMPC_release:
13442   case OMPC_relaxed:
13443   case OMPC_device:
13444   case OMPC_threads:
13445   case OMPC_simd:
13446   case OMPC_num_teams:
13447   case OMPC_thread_limit:
13448   case OMPC_priority:
13449   case OMPC_grainsize:
13450   case OMPC_nogroup:
13451   case OMPC_num_tasks:
13452   case OMPC_hint:
13453   case OMPC_dist_schedule:
13454   case OMPC_defaultmap:
13455   case OMPC_unknown:
13456   case OMPC_uniform:
13457   case OMPC_unified_address:
13458   case OMPC_unified_shared_memory:
13459   case OMPC_reverse_offload:
13460   case OMPC_dynamic_allocators:
13461   case OMPC_atomic_default_mem_order:
13462   case OMPC_device_type:
13463   case OMPC_match:
13464   case OMPC_order:
13465   case OMPC_destroy:
13466   case OMPC_detach:
13467   case OMPC_uses_allocators:
13468     llvm_unreachable("Clause is not allowed.");
13469   }
13470   return Res;
13471 }
13472 
13473 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
13474                                        ExprObjectKind OK, SourceLocation Loc) {
13475   ExprResult Res = BuildDeclRefExpr(
13476       Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
13477   if (!Res.isUsable())
13478     return ExprError();
13479   if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
13480     Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
13481     if (!Res.isUsable())
13482       return ExprError();
13483   }
13484   if (VK != VK_LValue && Res.get()->isGLValue()) {
13485     Res = DefaultLvalueConversion(Res.get());
13486     if (!Res.isUsable())
13487       return ExprError();
13488   }
13489   return Res;
13490 }
13491 
13492 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
13493                                           SourceLocation StartLoc,
13494                                           SourceLocation LParenLoc,
13495                                           SourceLocation EndLoc) {
13496   SmallVector<Expr *, 8> Vars;
13497   SmallVector<Expr *, 8> PrivateCopies;
13498   for (Expr *RefExpr : VarList) {
13499     assert(RefExpr && "NULL expr in OpenMP private clause.");
13500     SourceLocation ELoc;
13501     SourceRange ERange;
13502     Expr *SimpleRefExpr = RefExpr;
13503     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13504     if (Res.second) {
13505       // It will be analyzed later.
13506       Vars.push_back(RefExpr);
13507       PrivateCopies.push_back(nullptr);
13508     }
13509     ValueDecl *D = Res.first;
13510     if (!D)
13511       continue;
13512 
13513     QualType Type = D->getType();
13514     auto *VD = dyn_cast<VarDecl>(D);
13515 
13516     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
13517     //  A variable that appears in a private clause must not have an incomplete
13518     //  type or a reference type.
13519     if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
13520       continue;
13521     Type = Type.getNonReferenceType();
13522 
13523     // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
13524     // A variable that is privatized must not have a const-qualified type
13525     // unless it is of class type with a mutable member. This restriction does
13526     // not apply to the firstprivate clause.
13527     //
13528     // OpenMP 3.1 [2.9.3.3, private clause, Restrictions]
13529     // A variable that appears in a private clause must not have a
13530     // const-qualified type unless it is of class type with a mutable member.
13531     if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc))
13532       continue;
13533 
13534     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
13535     // in a Construct]
13536     //  Variables with the predetermined data-sharing attributes may not be
13537     //  listed in data-sharing attributes clauses, except for the cases
13538     //  listed below. For these exceptions only, listing a predetermined
13539     //  variable in a data-sharing attribute clause is allowed and overrides
13540     //  the variable's predetermined data-sharing attributes.
13541     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
13542     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
13543       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
13544                                           << getOpenMPClauseName(OMPC_private);
13545       reportOriginalDsa(*this, DSAStack, D, DVar);
13546       continue;
13547     }
13548 
13549     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
13550     // Variably modified types are not supported for tasks.
13551     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
13552         isOpenMPTaskingDirective(CurrDir)) {
13553       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
13554           << getOpenMPClauseName(OMPC_private) << Type
13555           << getOpenMPDirectiveName(CurrDir);
13556       bool IsDecl =
13557           !VD ||
13558           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
13559       Diag(D->getLocation(),
13560            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
13561           << D;
13562       continue;
13563     }
13564 
13565     // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
13566     // A list item cannot appear in both a map clause and a data-sharing
13567     // attribute clause on the same construct
13568     //
13569     // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
13570     // A list item cannot appear in both a map clause and a data-sharing
13571     // attribute clause on the same construct unless the construct is a
13572     // combined construct.
13573     if ((LangOpts.OpenMP <= 45 && isOpenMPTargetExecutionDirective(CurrDir)) ||
13574         CurrDir == OMPD_target) {
13575       OpenMPClauseKind ConflictKind;
13576       if (DSAStack->checkMappableExprComponentListsForDecl(
13577               VD, /*CurrentRegionOnly=*/true,
13578               [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
13579                   OpenMPClauseKind WhereFoundClauseKind) -> bool {
13580                 ConflictKind = WhereFoundClauseKind;
13581                 return true;
13582               })) {
13583         Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
13584             << getOpenMPClauseName(OMPC_private)
13585             << getOpenMPClauseName(ConflictKind)
13586             << getOpenMPDirectiveName(CurrDir);
13587         reportOriginalDsa(*this, DSAStack, D, DVar);
13588         continue;
13589       }
13590     }
13591 
13592     // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
13593     //  A variable of class type (or array thereof) that appears in a private
13594     //  clause requires an accessible, unambiguous default constructor for the
13595     //  class type.
13596     // Generate helper private variable and initialize it with the default
13597     // value. The address of the original variable is replaced by the address of
13598     // the new private variable in CodeGen. This new variable is not added to
13599     // IdResolver, so the code in the OpenMP region uses original variable for
13600     // proper diagnostics.
13601     Type = Type.getUnqualifiedType();
13602     VarDecl *VDPrivate =
13603         buildVarDecl(*this, ELoc, Type, D->getName(),
13604                      D->hasAttrs() ? &D->getAttrs() : nullptr,
13605                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
13606     ActOnUninitializedDecl(VDPrivate);
13607     if (VDPrivate->isInvalidDecl())
13608       continue;
13609     DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
13610         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
13611 
13612     DeclRefExpr *Ref = nullptr;
13613     if (!VD && !CurContext->isDependentContext())
13614       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
13615     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
13616     Vars.push_back((VD || CurContext->isDependentContext())
13617                        ? RefExpr->IgnoreParens()
13618                        : Ref);
13619     PrivateCopies.push_back(VDPrivateRefExpr);
13620   }
13621 
13622   if (Vars.empty())
13623     return nullptr;
13624 
13625   return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
13626                                   PrivateCopies);
13627 }
13628 
13629 namespace {
13630 class DiagsUninitializedSeveretyRAII {
13631 private:
13632   DiagnosticsEngine &Diags;
13633   SourceLocation SavedLoc;
13634   bool IsIgnored = false;
13635 
13636 public:
13637   DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
13638                                  bool IsIgnored)
13639       : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
13640     if (!IsIgnored) {
13641       Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
13642                         /*Map*/ diag::Severity::Ignored, Loc);
13643     }
13644   }
13645   ~DiagsUninitializedSeveretyRAII() {
13646     if (!IsIgnored)
13647       Diags.popMappings(SavedLoc);
13648   }
13649 };
13650 }
13651 
13652 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
13653                                                SourceLocation StartLoc,
13654                                                SourceLocation LParenLoc,
13655                                                SourceLocation EndLoc) {
13656   SmallVector<Expr *, 8> Vars;
13657   SmallVector<Expr *, 8> PrivateCopies;
13658   SmallVector<Expr *, 8> Inits;
13659   SmallVector<Decl *, 4> ExprCaptures;
13660   bool IsImplicitClause =
13661       StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
13662   SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
13663 
13664   for (Expr *RefExpr : VarList) {
13665     assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
13666     SourceLocation ELoc;
13667     SourceRange ERange;
13668     Expr *SimpleRefExpr = RefExpr;
13669     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13670     if (Res.second) {
13671       // It will be analyzed later.
13672       Vars.push_back(RefExpr);
13673       PrivateCopies.push_back(nullptr);
13674       Inits.push_back(nullptr);
13675     }
13676     ValueDecl *D = Res.first;
13677     if (!D)
13678       continue;
13679 
13680     ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
13681     QualType Type = D->getType();
13682     auto *VD = dyn_cast<VarDecl>(D);
13683 
13684     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
13685     //  A variable that appears in a private clause must not have an incomplete
13686     //  type or a reference type.
13687     if (RequireCompleteType(ELoc, Type,
13688                             diag::err_omp_firstprivate_incomplete_type))
13689       continue;
13690     Type = Type.getNonReferenceType();
13691 
13692     // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
13693     //  A variable of class type (or array thereof) that appears in a private
13694     //  clause requires an accessible, unambiguous copy constructor for the
13695     //  class type.
13696     QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
13697 
13698     // If an implicit firstprivate variable found it was checked already.
13699     DSAStackTy::DSAVarData TopDVar;
13700     if (!IsImplicitClause) {
13701       DSAStackTy::DSAVarData DVar =
13702           DSAStack->getTopDSA(D, /*FromParent=*/false);
13703       TopDVar = DVar;
13704       OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
13705       bool IsConstant = ElemType.isConstant(Context);
13706       // OpenMP [2.4.13, Data-sharing Attribute Clauses]
13707       //  A list item that specifies a given variable may not appear in more
13708       // than one clause on the same directive, except that a variable may be
13709       //  specified in both firstprivate and lastprivate clauses.
13710       // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
13711       // A list item may appear in a firstprivate or lastprivate clause but not
13712       // both.
13713       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
13714           (isOpenMPDistributeDirective(CurrDir) ||
13715            DVar.CKind != OMPC_lastprivate) &&
13716           DVar.RefExpr) {
13717         Diag(ELoc, diag::err_omp_wrong_dsa)
13718             << getOpenMPClauseName(DVar.CKind)
13719             << getOpenMPClauseName(OMPC_firstprivate);
13720         reportOriginalDsa(*this, DSAStack, D, DVar);
13721         continue;
13722       }
13723 
13724       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
13725       // in a Construct]
13726       //  Variables with the predetermined data-sharing attributes may not be
13727       //  listed in data-sharing attributes clauses, except for the cases
13728       //  listed below. For these exceptions only, listing a predetermined
13729       //  variable in a data-sharing attribute clause is allowed and overrides
13730       //  the variable's predetermined data-sharing attributes.
13731       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
13732       // in a Construct, C/C++, p.2]
13733       //  Variables with const-qualified type having no mutable member may be
13734       //  listed in a firstprivate clause, even if they are static data members.
13735       if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
13736           DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
13737         Diag(ELoc, diag::err_omp_wrong_dsa)
13738             << getOpenMPClauseName(DVar.CKind)
13739             << getOpenMPClauseName(OMPC_firstprivate);
13740         reportOriginalDsa(*this, DSAStack, D, DVar);
13741         continue;
13742       }
13743 
13744       // OpenMP [2.9.3.4, Restrictions, p.2]
13745       //  A list item that is private within a parallel region must not appear
13746       //  in a firstprivate clause on a worksharing construct if any of the
13747       //  worksharing regions arising from the worksharing construct ever bind
13748       //  to any of the parallel regions arising from the parallel construct.
13749       // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
13750       // A list item that is private within a teams region must not appear in a
13751       // firstprivate clause on a distribute construct if any of the distribute
13752       // regions arising from the distribute construct ever bind to any of the
13753       // teams regions arising from the teams construct.
13754       // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
13755       // A list item that appears in a reduction clause of a teams construct
13756       // must not appear in a firstprivate clause on a distribute construct if
13757       // any of the distribute regions arising from the distribute construct
13758       // ever bind to any of the teams regions arising from the teams construct.
13759       if ((isOpenMPWorksharingDirective(CurrDir) ||
13760            isOpenMPDistributeDirective(CurrDir)) &&
13761           !isOpenMPParallelDirective(CurrDir) &&
13762           !isOpenMPTeamsDirective(CurrDir)) {
13763         DVar = DSAStack->getImplicitDSA(D, true);
13764         if (DVar.CKind != OMPC_shared &&
13765             (isOpenMPParallelDirective(DVar.DKind) ||
13766              isOpenMPTeamsDirective(DVar.DKind) ||
13767              DVar.DKind == OMPD_unknown)) {
13768           Diag(ELoc, diag::err_omp_required_access)
13769               << getOpenMPClauseName(OMPC_firstprivate)
13770               << getOpenMPClauseName(OMPC_shared);
13771           reportOriginalDsa(*this, DSAStack, D, DVar);
13772           continue;
13773         }
13774       }
13775       // OpenMP [2.9.3.4, Restrictions, p.3]
13776       //  A list item that appears in a reduction clause of a parallel construct
13777       //  must not appear in a firstprivate clause on a worksharing or task
13778       //  construct if any of the worksharing or task regions arising from the
13779       //  worksharing or task construct ever bind to any of the parallel regions
13780       //  arising from the parallel construct.
13781       // OpenMP [2.9.3.4, Restrictions, p.4]
13782       //  A list item that appears in a reduction clause in worksharing
13783       //  construct must not appear in a firstprivate clause in a task construct
13784       //  encountered during execution of any of the worksharing regions arising
13785       //  from the worksharing construct.
13786       if (isOpenMPTaskingDirective(CurrDir)) {
13787         DVar = DSAStack->hasInnermostDSA(
13788             D, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
13789             [](OpenMPDirectiveKind K) {
13790               return isOpenMPParallelDirective(K) ||
13791                      isOpenMPWorksharingDirective(K) ||
13792                      isOpenMPTeamsDirective(K);
13793             },
13794             /*FromParent=*/true);
13795         if (DVar.CKind == OMPC_reduction &&
13796             (isOpenMPParallelDirective(DVar.DKind) ||
13797              isOpenMPWorksharingDirective(DVar.DKind) ||
13798              isOpenMPTeamsDirective(DVar.DKind))) {
13799           Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
13800               << getOpenMPDirectiveName(DVar.DKind);
13801           reportOriginalDsa(*this, DSAStack, D, DVar);
13802           continue;
13803         }
13804       }
13805 
13806       // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
13807       // A list item cannot appear in both a map clause and a data-sharing
13808       // attribute clause on the same construct
13809       //
13810       // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
13811       // A list item cannot appear in both a map clause and a data-sharing
13812       // attribute clause on the same construct unless the construct is a
13813       // combined construct.
13814       if ((LangOpts.OpenMP <= 45 &&
13815            isOpenMPTargetExecutionDirective(CurrDir)) ||
13816           CurrDir == OMPD_target) {
13817         OpenMPClauseKind ConflictKind;
13818         if (DSAStack->checkMappableExprComponentListsForDecl(
13819                 VD, /*CurrentRegionOnly=*/true,
13820                 [&ConflictKind](
13821                     OMPClauseMappableExprCommon::MappableExprComponentListRef,
13822                     OpenMPClauseKind WhereFoundClauseKind) {
13823                   ConflictKind = WhereFoundClauseKind;
13824                   return true;
13825                 })) {
13826           Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
13827               << getOpenMPClauseName(OMPC_firstprivate)
13828               << getOpenMPClauseName(ConflictKind)
13829               << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
13830           reportOriginalDsa(*this, DSAStack, D, DVar);
13831           continue;
13832         }
13833       }
13834     }
13835 
13836     // Variably modified types are not supported for tasks.
13837     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
13838         isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
13839       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
13840           << getOpenMPClauseName(OMPC_firstprivate) << Type
13841           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
13842       bool IsDecl =
13843           !VD ||
13844           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
13845       Diag(D->getLocation(),
13846            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
13847           << D;
13848       continue;
13849     }
13850 
13851     Type = Type.getUnqualifiedType();
13852     VarDecl *VDPrivate =
13853         buildVarDecl(*this, ELoc, Type, D->getName(),
13854                      D->hasAttrs() ? &D->getAttrs() : nullptr,
13855                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
13856     // Generate helper private variable and initialize it with the value of the
13857     // original variable. The address of the original variable is replaced by
13858     // the address of the new private variable in the CodeGen. This new variable
13859     // is not added to IdResolver, so the code in the OpenMP region uses
13860     // original variable for proper diagnostics and variable capturing.
13861     Expr *VDInitRefExpr = nullptr;
13862     // For arrays generate initializer for single element and replace it by the
13863     // original array element in CodeGen.
13864     if (Type->isArrayType()) {
13865       VarDecl *VDInit =
13866           buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
13867       VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
13868       Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get();
13869       ElemType = ElemType.getUnqualifiedType();
13870       VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
13871                                          ".firstprivate.temp");
13872       InitializedEntity Entity =
13873           InitializedEntity::InitializeVariable(VDInitTemp);
13874       InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
13875 
13876       InitializationSequence InitSeq(*this, Entity, Kind, Init);
13877       ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
13878       if (Result.isInvalid())
13879         VDPrivate->setInvalidDecl();
13880       else
13881         VDPrivate->setInit(Result.getAs<Expr>());
13882       // Remove temp variable declaration.
13883       Context.Deallocate(VDInitTemp);
13884     } else {
13885       VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
13886                                      ".firstprivate.temp");
13887       VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
13888                                        RefExpr->getExprLoc());
13889       AddInitializerToDecl(VDPrivate,
13890                            DefaultLvalueConversion(VDInitRefExpr).get(),
13891                            /*DirectInit=*/false);
13892     }
13893     if (VDPrivate->isInvalidDecl()) {
13894       if (IsImplicitClause) {
13895         Diag(RefExpr->getExprLoc(),
13896              diag::note_omp_task_predetermined_firstprivate_here);
13897       }
13898       continue;
13899     }
13900     CurContext->addDecl(VDPrivate);
13901     DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
13902         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
13903         RefExpr->getExprLoc());
13904     DeclRefExpr *Ref = nullptr;
13905     if (!VD && !CurContext->isDependentContext()) {
13906       if (TopDVar.CKind == OMPC_lastprivate) {
13907         Ref = TopDVar.PrivateCopy;
13908       } else {
13909         Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
13910         if (!isOpenMPCapturedDecl(D))
13911           ExprCaptures.push_back(Ref->getDecl());
13912       }
13913     }
13914     if (!IsImplicitClause)
13915       DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
13916     Vars.push_back((VD || CurContext->isDependentContext())
13917                        ? RefExpr->IgnoreParens()
13918                        : Ref);
13919     PrivateCopies.push_back(VDPrivateRefExpr);
13920     Inits.push_back(VDInitRefExpr);
13921   }
13922 
13923   if (Vars.empty())
13924     return nullptr;
13925 
13926   return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13927                                        Vars, PrivateCopies, Inits,
13928                                        buildPreInits(Context, ExprCaptures));
13929 }
13930 
13931 OMPClause *Sema::ActOnOpenMPLastprivateClause(
13932     ArrayRef<Expr *> VarList, OpenMPLastprivateModifier LPKind,
13933     SourceLocation LPKindLoc, SourceLocation ColonLoc, SourceLocation StartLoc,
13934     SourceLocation LParenLoc, SourceLocation EndLoc) {
13935   if (LPKind == OMPC_LASTPRIVATE_unknown && LPKindLoc.isValid()) {
13936     assert(ColonLoc.isValid() && "Colon location must be valid.");
13937     Diag(LPKindLoc, diag::err_omp_unexpected_clause_value)
13938         << getListOfPossibleValues(OMPC_lastprivate, /*First=*/0,
13939                                    /*Last=*/OMPC_LASTPRIVATE_unknown)
13940         << getOpenMPClauseName(OMPC_lastprivate);
13941     return nullptr;
13942   }
13943 
13944   SmallVector<Expr *, 8> Vars;
13945   SmallVector<Expr *, 8> SrcExprs;
13946   SmallVector<Expr *, 8> DstExprs;
13947   SmallVector<Expr *, 8> AssignmentOps;
13948   SmallVector<Decl *, 4> ExprCaptures;
13949   SmallVector<Expr *, 4> ExprPostUpdates;
13950   for (Expr *RefExpr : VarList) {
13951     assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
13952     SourceLocation ELoc;
13953     SourceRange ERange;
13954     Expr *SimpleRefExpr = RefExpr;
13955     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13956     if (Res.second) {
13957       // It will be analyzed later.
13958       Vars.push_back(RefExpr);
13959       SrcExprs.push_back(nullptr);
13960       DstExprs.push_back(nullptr);
13961       AssignmentOps.push_back(nullptr);
13962     }
13963     ValueDecl *D = Res.first;
13964     if (!D)
13965       continue;
13966 
13967     QualType Type = D->getType();
13968     auto *VD = dyn_cast<VarDecl>(D);
13969 
13970     // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
13971     //  A variable that appears in a lastprivate clause must not have an
13972     //  incomplete type or a reference type.
13973     if (RequireCompleteType(ELoc, Type,
13974                             diag::err_omp_lastprivate_incomplete_type))
13975       continue;
13976     Type = Type.getNonReferenceType();
13977 
13978     // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
13979     // A variable that is privatized must not have a const-qualified type
13980     // unless it is of class type with a mutable member. This restriction does
13981     // not apply to the firstprivate clause.
13982     //
13983     // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions]
13984     // A variable that appears in a lastprivate clause must not have a
13985     // const-qualified type unless it is of class type with a mutable member.
13986     if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc))
13987       continue;
13988 
13989     // OpenMP 5.0 [2.19.4.5 lastprivate Clause, Restrictions]
13990     // A list item that appears in a lastprivate clause with the conditional
13991     // modifier must be a scalar variable.
13992     if (LPKind == OMPC_LASTPRIVATE_conditional && !Type->isScalarType()) {
13993       Diag(ELoc, diag::err_omp_lastprivate_conditional_non_scalar);
13994       bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
13995                                VarDecl::DeclarationOnly;
13996       Diag(D->getLocation(),
13997            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
13998           << D;
13999       continue;
14000     }
14001 
14002     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
14003     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
14004     // in a Construct]
14005     //  Variables with the predetermined data-sharing attributes may not be
14006     //  listed in data-sharing attributes clauses, except for the cases
14007     //  listed below.
14008     // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
14009     // A list item may appear in a firstprivate or lastprivate clause but not
14010     // both.
14011     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
14012     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
14013         (isOpenMPDistributeDirective(CurrDir) ||
14014          DVar.CKind != OMPC_firstprivate) &&
14015         (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
14016       Diag(ELoc, diag::err_omp_wrong_dsa)
14017           << getOpenMPClauseName(DVar.CKind)
14018           << getOpenMPClauseName(OMPC_lastprivate);
14019       reportOriginalDsa(*this, DSAStack, D, DVar);
14020       continue;
14021     }
14022 
14023     // OpenMP [2.14.3.5, Restrictions, p.2]
14024     // A list item that is private within a parallel region, or that appears in
14025     // the reduction clause of a parallel construct, must not appear in a
14026     // lastprivate clause on a worksharing construct if any of the corresponding
14027     // worksharing regions ever binds to any of the corresponding parallel
14028     // regions.
14029     DSAStackTy::DSAVarData TopDVar = DVar;
14030     if (isOpenMPWorksharingDirective(CurrDir) &&
14031         !isOpenMPParallelDirective(CurrDir) &&
14032         !isOpenMPTeamsDirective(CurrDir)) {
14033       DVar = DSAStack->getImplicitDSA(D, true);
14034       if (DVar.CKind != OMPC_shared) {
14035         Diag(ELoc, diag::err_omp_required_access)
14036             << getOpenMPClauseName(OMPC_lastprivate)
14037             << getOpenMPClauseName(OMPC_shared);
14038         reportOriginalDsa(*this, DSAStack, D, DVar);
14039         continue;
14040       }
14041     }
14042 
14043     // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
14044     //  A variable of class type (or array thereof) that appears in a
14045     //  lastprivate clause requires an accessible, unambiguous default
14046     //  constructor for the class type, unless the list item is also specified
14047     //  in a firstprivate clause.
14048     //  A variable of class type (or array thereof) that appears in a
14049     //  lastprivate clause requires an accessible, unambiguous copy assignment
14050     //  operator for the class type.
14051     Type = Context.getBaseElementType(Type).getNonReferenceType();
14052     VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(),
14053                                   Type.getUnqualifiedType(), ".lastprivate.src",
14054                                   D->hasAttrs() ? &D->getAttrs() : nullptr);
14055     DeclRefExpr *PseudoSrcExpr =
14056         buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
14057     VarDecl *DstVD =
14058         buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
14059                      D->hasAttrs() ? &D->getAttrs() : nullptr);
14060     DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
14061     // For arrays generate assignment operation for single element and replace
14062     // it by the original array element in CodeGen.
14063     ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
14064                                          PseudoDstExpr, PseudoSrcExpr);
14065     if (AssignmentOp.isInvalid())
14066       continue;
14067     AssignmentOp =
14068         ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
14069     if (AssignmentOp.isInvalid())
14070       continue;
14071 
14072     DeclRefExpr *Ref = nullptr;
14073     if (!VD && !CurContext->isDependentContext()) {
14074       if (TopDVar.CKind == OMPC_firstprivate) {
14075         Ref = TopDVar.PrivateCopy;
14076       } else {
14077         Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
14078         if (!isOpenMPCapturedDecl(D))
14079           ExprCaptures.push_back(Ref->getDecl());
14080       }
14081       if (TopDVar.CKind == OMPC_firstprivate ||
14082           (!isOpenMPCapturedDecl(D) &&
14083            Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
14084         ExprResult RefRes = DefaultLvalueConversion(Ref);
14085         if (!RefRes.isUsable())
14086           continue;
14087         ExprResult PostUpdateRes =
14088             BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
14089                        RefRes.get());
14090         if (!PostUpdateRes.isUsable())
14091           continue;
14092         ExprPostUpdates.push_back(
14093             IgnoredValueConversions(PostUpdateRes.get()).get());
14094       }
14095     }
14096     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
14097     Vars.push_back((VD || CurContext->isDependentContext())
14098                        ? RefExpr->IgnoreParens()
14099                        : Ref);
14100     SrcExprs.push_back(PseudoSrcExpr);
14101     DstExprs.push_back(PseudoDstExpr);
14102     AssignmentOps.push_back(AssignmentOp.get());
14103   }
14104 
14105   if (Vars.empty())
14106     return nullptr;
14107 
14108   return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
14109                                       Vars, SrcExprs, DstExprs, AssignmentOps,
14110                                       LPKind, LPKindLoc, ColonLoc,
14111                                       buildPreInits(Context, ExprCaptures),
14112                                       buildPostUpdate(*this, ExprPostUpdates));
14113 }
14114 
14115 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
14116                                          SourceLocation StartLoc,
14117                                          SourceLocation LParenLoc,
14118                                          SourceLocation EndLoc) {
14119   SmallVector<Expr *, 8> Vars;
14120   for (Expr *RefExpr : VarList) {
14121     assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
14122     SourceLocation ELoc;
14123     SourceRange ERange;
14124     Expr *SimpleRefExpr = RefExpr;
14125     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
14126     if (Res.second) {
14127       // It will be analyzed later.
14128       Vars.push_back(RefExpr);
14129     }
14130     ValueDecl *D = Res.first;
14131     if (!D)
14132       continue;
14133 
14134     auto *VD = dyn_cast<VarDecl>(D);
14135     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
14136     // in a Construct]
14137     //  Variables with the predetermined data-sharing attributes may not be
14138     //  listed in data-sharing attributes clauses, except for the cases
14139     //  listed below. For these exceptions only, listing a predetermined
14140     //  variable in a data-sharing attribute clause is allowed and overrides
14141     //  the variable's predetermined data-sharing attributes.
14142     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
14143     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
14144         DVar.RefExpr) {
14145       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
14146                                           << getOpenMPClauseName(OMPC_shared);
14147       reportOriginalDsa(*this, DSAStack, D, DVar);
14148       continue;
14149     }
14150 
14151     DeclRefExpr *Ref = nullptr;
14152     if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
14153       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
14154     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
14155     Vars.push_back((VD || !Ref || CurContext->isDependentContext())
14156                        ? RefExpr->IgnoreParens()
14157                        : Ref);
14158   }
14159 
14160   if (Vars.empty())
14161     return nullptr;
14162 
14163   return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
14164 }
14165 
14166 namespace {
14167 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
14168   DSAStackTy *Stack;
14169 
14170 public:
14171   bool VisitDeclRefExpr(DeclRefExpr *E) {
14172     if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
14173       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
14174       if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
14175         return false;
14176       if (DVar.CKind != OMPC_unknown)
14177         return true;
14178       DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
14179           VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; },
14180           /*FromParent=*/true);
14181       return DVarPrivate.CKind != OMPC_unknown;
14182     }
14183     return false;
14184   }
14185   bool VisitStmt(Stmt *S) {
14186     for (Stmt *Child : S->children()) {
14187       if (Child && Visit(Child))
14188         return true;
14189     }
14190     return false;
14191   }
14192   explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
14193 };
14194 } // namespace
14195 
14196 namespace {
14197 // Transform MemberExpression for specified FieldDecl of current class to
14198 // DeclRefExpr to specified OMPCapturedExprDecl.
14199 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
14200   typedef TreeTransform<TransformExprToCaptures> BaseTransform;
14201   ValueDecl *Field = nullptr;
14202   DeclRefExpr *CapturedExpr = nullptr;
14203 
14204 public:
14205   TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
14206       : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
14207 
14208   ExprResult TransformMemberExpr(MemberExpr *E) {
14209     if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
14210         E->getMemberDecl() == Field) {
14211       CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
14212       return CapturedExpr;
14213     }
14214     return BaseTransform::TransformMemberExpr(E);
14215   }
14216   DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
14217 };
14218 } // namespace
14219 
14220 template <typename T, typename U>
14221 static T filterLookupForUDReductionAndMapper(
14222     SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) {
14223   for (U &Set : Lookups) {
14224     for (auto *D : Set) {
14225       if (T Res = Gen(cast<ValueDecl>(D)))
14226         return Res;
14227     }
14228   }
14229   return T();
14230 }
14231 
14232 static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
14233   assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
14234 
14235   for (auto RD : D->redecls()) {
14236     // Don't bother with extra checks if we already know this one isn't visible.
14237     if (RD == D)
14238       continue;
14239 
14240     auto ND = cast<NamedDecl>(RD);
14241     if (LookupResult::isVisible(SemaRef, ND))
14242       return ND;
14243   }
14244 
14245   return nullptr;
14246 }
14247 
14248 static void
14249 argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id,
14250                         SourceLocation Loc, QualType Ty,
14251                         SmallVectorImpl<UnresolvedSet<8>> &Lookups) {
14252   // Find all of the associated namespaces and classes based on the
14253   // arguments we have.
14254   Sema::AssociatedNamespaceSet AssociatedNamespaces;
14255   Sema::AssociatedClassSet AssociatedClasses;
14256   OpaqueValueExpr OVE(Loc, Ty, VK_LValue);
14257   SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces,
14258                                              AssociatedClasses);
14259 
14260   // C++ [basic.lookup.argdep]p3:
14261   //   Let X be the lookup set produced by unqualified lookup (3.4.1)
14262   //   and let Y be the lookup set produced by argument dependent
14263   //   lookup (defined as follows). If X contains [...] then Y is
14264   //   empty. Otherwise Y is the set of declarations found in the
14265   //   namespaces associated with the argument types as described
14266   //   below. The set of declarations found by the lookup of the name
14267   //   is the union of X and Y.
14268   //
14269   // Here, we compute Y and add its members to the overloaded
14270   // candidate set.
14271   for (auto *NS : AssociatedNamespaces) {
14272     //   When considering an associated namespace, the lookup is the
14273     //   same as the lookup performed when the associated namespace is
14274     //   used as a qualifier (3.4.3.2) except that:
14275     //
14276     //     -- Any using-directives in the associated namespace are
14277     //        ignored.
14278     //
14279     //     -- Any namespace-scope friend functions declared in
14280     //        associated classes are visible within their respective
14281     //        namespaces even if they are not visible during an ordinary
14282     //        lookup (11.4).
14283     DeclContext::lookup_result R = NS->lookup(Id.getName());
14284     for (auto *D : R) {
14285       auto *Underlying = D;
14286       if (auto *USD = dyn_cast<UsingShadowDecl>(D))
14287         Underlying = USD->getTargetDecl();
14288 
14289       if (!isa<OMPDeclareReductionDecl>(Underlying) &&
14290           !isa<OMPDeclareMapperDecl>(Underlying))
14291         continue;
14292 
14293       if (!SemaRef.isVisible(D)) {
14294         D = findAcceptableDecl(SemaRef, D);
14295         if (!D)
14296           continue;
14297         if (auto *USD = dyn_cast<UsingShadowDecl>(D))
14298           Underlying = USD->getTargetDecl();
14299       }
14300       Lookups.emplace_back();
14301       Lookups.back().addDecl(Underlying);
14302     }
14303   }
14304 }
14305 
14306 static ExprResult
14307 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
14308                          Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
14309                          const DeclarationNameInfo &ReductionId, QualType Ty,
14310                          CXXCastPath &BasePath, Expr *UnresolvedReduction) {
14311   if (ReductionIdScopeSpec.isInvalid())
14312     return ExprError();
14313   SmallVector<UnresolvedSet<8>, 4> Lookups;
14314   if (S) {
14315     LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
14316     Lookup.suppressDiagnostics();
14317     while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
14318       NamedDecl *D = Lookup.getRepresentativeDecl();
14319       do {
14320         S = S->getParent();
14321       } while (S && !S->isDeclScope(D));
14322       if (S)
14323         S = S->getParent();
14324       Lookups.emplace_back();
14325       Lookups.back().append(Lookup.begin(), Lookup.end());
14326       Lookup.clear();
14327     }
14328   } else if (auto *ULE =
14329                  cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
14330     Lookups.push_back(UnresolvedSet<8>());
14331     Decl *PrevD = nullptr;
14332     for (NamedDecl *D : ULE->decls()) {
14333       if (D == PrevD)
14334         Lookups.push_back(UnresolvedSet<8>());
14335       else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(D))
14336         Lookups.back().addDecl(DRD);
14337       PrevD = D;
14338     }
14339   }
14340   if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
14341       Ty->isInstantiationDependentType() ||
14342       Ty->containsUnexpandedParameterPack() ||
14343       filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) {
14344         return !D->isInvalidDecl() &&
14345                (D->getType()->isDependentType() ||
14346                 D->getType()->isInstantiationDependentType() ||
14347                 D->getType()->containsUnexpandedParameterPack());
14348       })) {
14349     UnresolvedSet<8> ResSet;
14350     for (const UnresolvedSet<8> &Set : Lookups) {
14351       if (Set.empty())
14352         continue;
14353       ResSet.append(Set.begin(), Set.end());
14354       // The last item marks the end of all declarations at the specified scope.
14355       ResSet.addDecl(Set[Set.size() - 1]);
14356     }
14357     return UnresolvedLookupExpr::Create(
14358         SemaRef.Context, /*NamingClass=*/nullptr,
14359         ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
14360         /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
14361   }
14362   // Lookup inside the classes.
14363   // C++ [over.match.oper]p3:
14364   //   For a unary operator @ with an operand of a type whose
14365   //   cv-unqualified version is T1, and for a binary operator @ with
14366   //   a left operand of a type whose cv-unqualified version is T1 and
14367   //   a right operand of a type whose cv-unqualified version is T2,
14368   //   three sets of candidate functions, designated member
14369   //   candidates, non-member candidates and built-in candidates, are
14370   //   constructed as follows:
14371   //     -- If T1 is a complete class type or a class currently being
14372   //        defined, the set of member candidates is the result of the
14373   //        qualified lookup of T1::operator@ (13.3.1.1.1); otherwise,
14374   //        the set of member candidates is empty.
14375   LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
14376   Lookup.suppressDiagnostics();
14377   if (const auto *TyRec = Ty->getAs<RecordType>()) {
14378     // Complete the type if it can be completed.
14379     // If the type is neither complete nor being defined, bail out now.
14380     if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() ||
14381         TyRec->getDecl()->getDefinition()) {
14382       Lookup.clear();
14383       SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl());
14384       if (Lookup.empty()) {
14385         Lookups.emplace_back();
14386         Lookups.back().append(Lookup.begin(), Lookup.end());
14387       }
14388     }
14389   }
14390   // Perform ADL.
14391   if (SemaRef.getLangOpts().CPlusPlus)
14392     argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups);
14393   if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
14394           Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
14395             if (!D->isInvalidDecl() &&
14396                 SemaRef.Context.hasSameType(D->getType(), Ty))
14397               return D;
14398             return nullptr;
14399           }))
14400     return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(),
14401                                     VK_LValue, Loc);
14402   if (SemaRef.getLangOpts().CPlusPlus) {
14403     if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
14404             Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
14405               if (!D->isInvalidDecl() &&
14406                   SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
14407                   !Ty.isMoreQualifiedThan(D->getType()))
14408                 return D;
14409               return nullptr;
14410             })) {
14411       CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
14412                          /*DetectVirtual=*/false);
14413       if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
14414         if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
14415                 VD->getType().getUnqualifiedType()))) {
14416           if (SemaRef.CheckBaseClassAccess(
14417                   Loc, VD->getType(), Ty, Paths.front(),
14418                   /*DiagID=*/0) != Sema::AR_inaccessible) {
14419             SemaRef.BuildBasePathArray(Paths, BasePath);
14420             return SemaRef.BuildDeclRefExpr(
14421                 VD, VD->getType().getNonReferenceType(), VK_LValue, Loc);
14422           }
14423         }
14424       }
14425     }
14426   }
14427   if (ReductionIdScopeSpec.isSet()) {
14428     SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier)
14429         << Ty << Range;
14430     return ExprError();
14431   }
14432   return ExprEmpty();
14433 }
14434 
14435 namespace {
14436 /// Data for the reduction-based clauses.
14437 struct ReductionData {
14438   /// List of original reduction items.
14439   SmallVector<Expr *, 8> Vars;
14440   /// List of private copies of the reduction items.
14441   SmallVector<Expr *, 8> Privates;
14442   /// LHS expressions for the reduction_op expressions.
14443   SmallVector<Expr *, 8> LHSs;
14444   /// RHS expressions for the reduction_op expressions.
14445   SmallVector<Expr *, 8> RHSs;
14446   /// Reduction operation expression.
14447   SmallVector<Expr *, 8> ReductionOps;
14448   /// Taskgroup descriptors for the corresponding reduction items in
14449   /// in_reduction clauses.
14450   SmallVector<Expr *, 8> TaskgroupDescriptors;
14451   /// List of captures for clause.
14452   SmallVector<Decl *, 4> ExprCaptures;
14453   /// List of postupdate expressions.
14454   SmallVector<Expr *, 4> ExprPostUpdates;
14455   /// Reduction modifier.
14456   unsigned RedModifier = 0;
14457   ReductionData() = delete;
14458   /// Reserves required memory for the reduction data.
14459   ReductionData(unsigned Size, unsigned Modifier = 0) : RedModifier(Modifier) {
14460     Vars.reserve(Size);
14461     Privates.reserve(Size);
14462     LHSs.reserve(Size);
14463     RHSs.reserve(Size);
14464     ReductionOps.reserve(Size);
14465     TaskgroupDescriptors.reserve(Size);
14466     ExprCaptures.reserve(Size);
14467     ExprPostUpdates.reserve(Size);
14468   }
14469   /// Stores reduction item and reduction operation only (required for dependent
14470   /// reduction item).
14471   void push(Expr *Item, Expr *ReductionOp) {
14472     Vars.emplace_back(Item);
14473     Privates.emplace_back(nullptr);
14474     LHSs.emplace_back(nullptr);
14475     RHSs.emplace_back(nullptr);
14476     ReductionOps.emplace_back(ReductionOp);
14477     TaskgroupDescriptors.emplace_back(nullptr);
14478   }
14479   /// Stores reduction data.
14480   void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
14481             Expr *TaskgroupDescriptor) {
14482     Vars.emplace_back(Item);
14483     Privates.emplace_back(Private);
14484     LHSs.emplace_back(LHS);
14485     RHSs.emplace_back(RHS);
14486     ReductionOps.emplace_back(ReductionOp);
14487     TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
14488   }
14489 };
14490 } // namespace
14491 
14492 static bool checkOMPArraySectionConstantForReduction(
14493     ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
14494     SmallVectorImpl<llvm::APSInt> &ArraySizes) {
14495   const Expr *Length = OASE->getLength();
14496   if (Length == nullptr) {
14497     // For array sections of the form [1:] or [:], we would need to analyze
14498     // the lower bound...
14499     if (OASE->getColonLoc().isValid())
14500       return false;
14501 
14502     // This is an array subscript which has implicit length 1!
14503     SingleElement = true;
14504     ArraySizes.push_back(llvm::APSInt::get(1));
14505   } else {
14506     Expr::EvalResult Result;
14507     if (!Length->EvaluateAsInt(Result, Context))
14508       return false;
14509 
14510     llvm::APSInt ConstantLengthValue = Result.Val.getInt();
14511     SingleElement = (ConstantLengthValue.getSExtValue() == 1);
14512     ArraySizes.push_back(ConstantLengthValue);
14513   }
14514 
14515   // Get the base of this array section and walk up from there.
14516   const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
14517 
14518   // We require length = 1 for all array sections except the right-most to
14519   // guarantee that the memory region is contiguous and has no holes in it.
14520   while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
14521     Length = TempOASE->getLength();
14522     if (Length == nullptr) {
14523       // For array sections of the form [1:] or [:], we would need to analyze
14524       // the lower bound...
14525       if (OASE->getColonLoc().isValid())
14526         return false;
14527 
14528       // This is an array subscript which has implicit length 1!
14529       ArraySizes.push_back(llvm::APSInt::get(1));
14530     } else {
14531       Expr::EvalResult Result;
14532       if (!Length->EvaluateAsInt(Result, Context))
14533         return false;
14534 
14535       llvm::APSInt ConstantLengthValue = Result.Val.getInt();
14536       if (ConstantLengthValue.getSExtValue() != 1)
14537         return false;
14538 
14539       ArraySizes.push_back(ConstantLengthValue);
14540     }
14541     Base = TempOASE->getBase()->IgnoreParenImpCasts();
14542   }
14543 
14544   // If we have a single element, we don't need to add the implicit lengths.
14545   if (!SingleElement) {
14546     while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
14547       // Has implicit length 1!
14548       ArraySizes.push_back(llvm::APSInt::get(1));
14549       Base = TempASE->getBase()->IgnoreParenImpCasts();
14550     }
14551   }
14552 
14553   // This array section can be privatized as a single value or as a constant
14554   // sized array.
14555   return true;
14556 }
14557 
14558 static bool actOnOMPReductionKindClause(
14559     Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
14560     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
14561     SourceLocation ColonLoc, SourceLocation EndLoc,
14562     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
14563     ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
14564   DeclarationName DN = ReductionId.getName();
14565   OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
14566   BinaryOperatorKind BOK = BO_Comma;
14567 
14568   ASTContext &Context = S.Context;
14569   // OpenMP [2.14.3.6, reduction clause]
14570   // C
14571   // reduction-identifier is either an identifier or one of the following
14572   // operators: +, -, *,  &, |, ^, && and ||
14573   // C++
14574   // reduction-identifier is either an id-expression or one of the following
14575   // operators: +, -, *, &, |, ^, && and ||
14576   switch (OOK) {
14577   case OO_Plus:
14578   case OO_Minus:
14579     BOK = BO_Add;
14580     break;
14581   case OO_Star:
14582     BOK = BO_Mul;
14583     break;
14584   case OO_Amp:
14585     BOK = BO_And;
14586     break;
14587   case OO_Pipe:
14588     BOK = BO_Or;
14589     break;
14590   case OO_Caret:
14591     BOK = BO_Xor;
14592     break;
14593   case OO_AmpAmp:
14594     BOK = BO_LAnd;
14595     break;
14596   case OO_PipePipe:
14597     BOK = BO_LOr;
14598     break;
14599   case OO_New:
14600   case OO_Delete:
14601   case OO_Array_New:
14602   case OO_Array_Delete:
14603   case OO_Slash:
14604   case OO_Percent:
14605   case OO_Tilde:
14606   case OO_Exclaim:
14607   case OO_Equal:
14608   case OO_Less:
14609   case OO_Greater:
14610   case OO_LessEqual:
14611   case OO_GreaterEqual:
14612   case OO_PlusEqual:
14613   case OO_MinusEqual:
14614   case OO_StarEqual:
14615   case OO_SlashEqual:
14616   case OO_PercentEqual:
14617   case OO_CaretEqual:
14618   case OO_AmpEqual:
14619   case OO_PipeEqual:
14620   case OO_LessLess:
14621   case OO_GreaterGreater:
14622   case OO_LessLessEqual:
14623   case OO_GreaterGreaterEqual:
14624   case OO_EqualEqual:
14625   case OO_ExclaimEqual:
14626   case OO_Spaceship:
14627   case OO_PlusPlus:
14628   case OO_MinusMinus:
14629   case OO_Comma:
14630   case OO_ArrowStar:
14631   case OO_Arrow:
14632   case OO_Call:
14633   case OO_Subscript:
14634   case OO_Conditional:
14635   case OO_Coawait:
14636   case NUM_OVERLOADED_OPERATORS:
14637     llvm_unreachable("Unexpected reduction identifier");
14638   case OO_None:
14639     if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
14640       if (II->isStr("max"))
14641         BOK = BO_GT;
14642       else if (II->isStr("min"))
14643         BOK = BO_LT;
14644     }
14645     break;
14646   }
14647   SourceRange ReductionIdRange;
14648   if (ReductionIdScopeSpec.isValid())
14649     ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
14650   else
14651     ReductionIdRange.setBegin(ReductionId.getBeginLoc());
14652   ReductionIdRange.setEnd(ReductionId.getEndLoc());
14653 
14654   auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
14655   bool FirstIter = true;
14656   for (Expr *RefExpr : VarList) {
14657     assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
14658     // OpenMP [2.1, C/C++]
14659     //  A list item is a variable or array section, subject to the restrictions
14660     //  specified in Section 2.4 on page 42 and in each of the sections
14661     // describing clauses and directives for which a list appears.
14662     // OpenMP  [2.14.3.3, Restrictions, p.1]
14663     //  A variable that is part of another variable (as an array or
14664     //  structure element) cannot appear in a private clause.
14665     if (!FirstIter && IR != ER)
14666       ++IR;
14667     FirstIter = false;
14668     SourceLocation ELoc;
14669     SourceRange ERange;
14670     Expr *SimpleRefExpr = RefExpr;
14671     auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
14672                               /*AllowArraySection=*/true);
14673     if (Res.second) {
14674       // Try to find 'declare reduction' corresponding construct before using
14675       // builtin/overloaded operators.
14676       QualType Type = Context.DependentTy;
14677       CXXCastPath BasePath;
14678       ExprResult DeclareReductionRef = buildDeclareReductionRef(
14679           S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
14680           ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
14681       Expr *ReductionOp = nullptr;
14682       if (S.CurContext->isDependentContext() &&
14683           (DeclareReductionRef.isUnset() ||
14684            isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
14685         ReductionOp = DeclareReductionRef.get();
14686       // It will be analyzed later.
14687       RD.push(RefExpr, ReductionOp);
14688     }
14689     ValueDecl *D = Res.first;
14690     if (!D)
14691       continue;
14692 
14693     Expr *TaskgroupDescriptor = nullptr;
14694     QualType Type;
14695     auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
14696     auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
14697     if (ASE) {
14698       Type = ASE->getType().getNonReferenceType();
14699     } else if (OASE) {
14700       QualType BaseType =
14701           OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
14702       if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
14703         Type = ATy->getElementType();
14704       else
14705         Type = BaseType->getPointeeType();
14706       Type = Type.getNonReferenceType();
14707     } else {
14708       Type = Context.getBaseElementType(D->getType().getNonReferenceType());
14709     }
14710     auto *VD = dyn_cast<VarDecl>(D);
14711 
14712     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
14713     //  A variable that appears in a private clause must not have an incomplete
14714     //  type or a reference type.
14715     if (S.RequireCompleteType(ELoc, D->getType(),
14716                               diag::err_omp_reduction_incomplete_type))
14717       continue;
14718     // OpenMP [2.14.3.6, reduction clause, Restrictions]
14719     // A list item that appears in a reduction clause must not be
14720     // const-qualified.
14721     if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc,
14722                                   /*AcceptIfMutable*/ false, ASE || OASE))
14723       continue;
14724 
14725     OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
14726     // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
14727     //  If a list-item is a reference type then it must bind to the same object
14728     //  for all threads of the team.
14729     if (!ASE && !OASE) {
14730       if (VD) {
14731         VarDecl *VDDef = VD->getDefinition();
14732         if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
14733           DSARefChecker Check(Stack);
14734           if (Check.Visit(VDDef->getInit())) {
14735             S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
14736                 << getOpenMPClauseName(ClauseKind) << ERange;
14737             S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
14738             continue;
14739           }
14740         }
14741       }
14742 
14743       // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
14744       // in a Construct]
14745       //  Variables with the predetermined data-sharing attributes may not be
14746       //  listed in data-sharing attributes clauses, except for the cases
14747       //  listed below. For these exceptions only, listing a predetermined
14748       //  variable in a data-sharing attribute clause is allowed and overrides
14749       //  the variable's predetermined data-sharing attributes.
14750       // OpenMP [2.14.3.6, Restrictions, p.3]
14751       //  Any number of reduction clauses can be specified on the directive,
14752       //  but a list item can appear only once in the reduction clauses for that
14753       //  directive.
14754       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
14755       if (DVar.CKind == OMPC_reduction) {
14756         S.Diag(ELoc, diag::err_omp_once_referenced)
14757             << getOpenMPClauseName(ClauseKind);
14758         if (DVar.RefExpr)
14759           S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
14760         continue;
14761       }
14762       if (DVar.CKind != OMPC_unknown) {
14763         S.Diag(ELoc, diag::err_omp_wrong_dsa)
14764             << getOpenMPClauseName(DVar.CKind)
14765             << getOpenMPClauseName(OMPC_reduction);
14766         reportOriginalDsa(S, Stack, D, DVar);
14767         continue;
14768       }
14769 
14770       // OpenMP [2.14.3.6, Restrictions, p.1]
14771       //  A list item that appears in a reduction clause of a worksharing
14772       //  construct must be shared in the parallel regions to which any of the
14773       //  worksharing regions arising from the worksharing construct bind.
14774       if (isOpenMPWorksharingDirective(CurrDir) &&
14775           !isOpenMPParallelDirective(CurrDir) &&
14776           !isOpenMPTeamsDirective(CurrDir)) {
14777         DVar = Stack->getImplicitDSA(D, true);
14778         if (DVar.CKind != OMPC_shared) {
14779           S.Diag(ELoc, diag::err_omp_required_access)
14780               << getOpenMPClauseName(OMPC_reduction)
14781               << getOpenMPClauseName(OMPC_shared);
14782           reportOriginalDsa(S, Stack, D, DVar);
14783           continue;
14784         }
14785       }
14786     }
14787 
14788     // Try to find 'declare reduction' corresponding construct before using
14789     // builtin/overloaded operators.
14790     CXXCastPath BasePath;
14791     ExprResult DeclareReductionRef = buildDeclareReductionRef(
14792         S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
14793         ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
14794     if (DeclareReductionRef.isInvalid())
14795       continue;
14796     if (S.CurContext->isDependentContext() &&
14797         (DeclareReductionRef.isUnset() ||
14798          isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
14799       RD.push(RefExpr, DeclareReductionRef.get());
14800       continue;
14801     }
14802     if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
14803       // Not allowed reduction identifier is found.
14804       S.Diag(ReductionId.getBeginLoc(),
14805              diag::err_omp_unknown_reduction_identifier)
14806           << Type << ReductionIdRange;
14807       continue;
14808     }
14809 
14810     // OpenMP [2.14.3.6, reduction clause, Restrictions]
14811     // The type of a list item that appears in a reduction clause must be valid
14812     // for the reduction-identifier. For a max or min reduction in C, the type
14813     // of the list item must be an allowed arithmetic data type: char, int,
14814     // float, double, or _Bool, possibly modified with long, short, signed, or
14815     // unsigned. For a max or min reduction in C++, the type of the list item
14816     // must be an allowed arithmetic data type: char, wchar_t, int, float,
14817     // double, or bool, possibly modified with long, short, signed, or unsigned.
14818     if (DeclareReductionRef.isUnset()) {
14819       if ((BOK == BO_GT || BOK == BO_LT) &&
14820           !(Type->isScalarType() ||
14821             (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
14822         S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
14823             << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
14824         if (!ASE && !OASE) {
14825           bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
14826                                    VarDecl::DeclarationOnly;
14827           S.Diag(D->getLocation(),
14828                  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
14829               << D;
14830         }
14831         continue;
14832       }
14833       if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
14834           !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
14835         S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
14836             << getOpenMPClauseName(ClauseKind);
14837         if (!ASE && !OASE) {
14838           bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
14839                                    VarDecl::DeclarationOnly;
14840           S.Diag(D->getLocation(),
14841                  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
14842               << D;
14843         }
14844         continue;
14845       }
14846     }
14847 
14848     Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
14849     VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
14850                                   D->hasAttrs() ? &D->getAttrs() : nullptr);
14851     VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
14852                                   D->hasAttrs() ? &D->getAttrs() : nullptr);
14853     QualType PrivateTy = Type;
14854 
14855     // Try if we can determine constant lengths for all array sections and avoid
14856     // the VLA.
14857     bool ConstantLengthOASE = false;
14858     if (OASE) {
14859       bool SingleElement;
14860       llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
14861       ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
14862           Context, OASE, SingleElement, ArraySizes);
14863 
14864       // If we don't have a single element, we must emit a constant array type.
14865       if (ConstantLengthOASE && !SingleElement) {
14866         for (llvm::APSInt &Size : ArraySizes)
14867           PrivateTy = Context.getConstantArrayType(PrivateTy, Size, nullptr,
14868                                                    ArrayType::Normal,
14869                                                    /*IndexTypeQuals=*/0);
14870       }
14871     }
14872 
14873     if ((OASE && !ConstantLengthOASE) ||
14874         (!OASE && !ASE &&
14875          D->getType().getNonReferenceType()->isVariablyModifiedType())) {
14876       if (!Context.getTargetInfo().isVLASupported()) {
14877         if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective())) {
14878           S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
14879           S.Diag(ELoc, diag::note_vla_unsupported);
14880         } else {
14881           S.targetDiag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
14882           S.targetDiag(ELoc, diag::note_vla_unsupported);
14883         }
14884         continue;
14885       }
14886       // For arrays/array sections only:
14887       // Create pseudo array type for private copy. The size for this array will
14888       // be generated during codegen.
14889       // For array subscripts or single variables Private Ty is the same as Type
14890       // (type of the variable or single array element).
14891       PrivateTy = Context.getVariableArrayType(
14892           Type,
14893           new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue),
14894           ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
14895     } else if (!ASE && !OASE &&
14896                Context.getAsArrayType(D->getType().getNonReferenceType())) {
14897       PrivateTy = D->getType().getNonReferenceType();
14898     }
14899     // Private copy.
14900     VarDecl *PrivateVD =
14901         buildVarDecl(S, ELoc, PrivateTy, D->getName(),
14902                      D->hasAttrs() ? &D->getAttrs() : nullptr,
14903                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
14904     // Add initializer for private variable.
14905     Expr *Init = nullptr;
14906     DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
14907     DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
14908     if (DeclareReductionRef.isUsable()) {
14909       auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
14910       auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
14911       if (DRD->getInitializer()) {
14912         Init = DRDRef;
14913         RHSVD->setInit(DRDRef);
14914         RHSVD->setInitStyle(VarDecl::CallInit);
14915       }
14916     } else {
14917       switch (BOK) {
14918       case BO_Add:
14919       case BO_Xor:
14920       case BO_Or:
14921       case BO_LOr:
14922         // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
14923         if (Type->isScalarType() || Type->isAnyComplexType())
14924           Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
14925         break;
14926       case BO_Mul:
14927       case BO_LAnd:
14928         if (Type->isScalarType() || Type->isAnyComplexType()) {
14929           // '*' and '&&' reduction ops - initializer is '1'.
14930           Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
14931         }
14932         break;
14933       case BO_And: {
14934         // '&' reduction op - initializer is '~0'.
14935         QualType OrigType = Type;
14936         if (auto *ComplexTy = OrigType->getAs<ComplexType>())
14937           Type = ComplexTy->getElementType();
14938         if (Type->isRealFloatingType()) {
14939           llvm::APFloat InitValue =
14940               llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
14941                                              /*isIEEE=*/true);
14942           Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
14943                                          Type, ELoc);
14944         } else if (Type->isScalarType()) {
14945           uint64_t Size = Context.getTypeSize(Type);
14946           QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
14947           llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
14948           Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
14949         }
14950         if (Init && OrigType->isAnyComplexType()) {
14951           // Init = 0xFFFF + 0xFFFFi;
14952           auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
14953           Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
14954         }
14955         Type = OrigType;
14956         break;
14957       }
14958       case BO_LT:
14959       case BO_GT: {
14960         // 'min' reduction op - initializer is 'Largest representable number in
14961         // the reduction list item type'.
14962         // 'max' reduction op - initializer is 'Least representable number in
14963         // the reduction list item type'.
14964         if (Type->isIntegerType() || Type->isPointerType()) {
14965           bool IsSigned = Type->hasSignedIntegerRepresentation();
14966           uint64_t Size = Context.getTypeSize(Type);
14967           QualType IntTy =
14968               Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
14969           llvm::APInt InitValue =
14970               (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
14971                                         : llvm::APInt::getMinValue(Size)
14972                              : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
14973                                         : llvm::APInt::getMaxValue(Size);
14974           Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
14975           if (Type->isPointerType()) {
14976             // Cast to pointer type.
14977             ExprResult CastExpr = S.BuildCStyleCastExpr(
14978                 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
14979             if (CastExpr.isInvalid())
14980               continue;
14981             Init = CastExpr.get();
14982           }
14983         } else if (Type->isRealFloatingType()) {
14984           llvm::APFloat InitValue = llvm::APFloat::getLargest(
14985               Context.getFloatTypeSemantics(Type), BOK != BO_LT);
14986           Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
14987                                          Type, ELoc);
14988         }
14989         break;
14990       }
14991       case BO_PtrMemD:
14992       case BO_PtrMemI:
14993       case BO_MulAssign:
14994       case BO_Div:
14995       case BO_Rem:
14996       case BO_Sub:
14997       case BO_Shl:
14998       case BO_Shr:
14999       case BO_LE:
15000       case BO_GE:
15001       case BO_EQ:
15002       case BO_NE:
15003       case BO_Cmp:
15004       case BO_AndAssign:
15005       case BO_XorAssign:
15006       case BO_OrAssign:
15007       case BO_Assign:
15008       case BO_AddAssign:
15009       case BO_SubAssign:
15010       case BO_DivAssign:
15011       case BO_RemAssign:
15012       case BO_ShlAssign:
15013       case BO_ShrAssign:
15014       case BO_Comma:
15015         llvm_unreachable("Unexpected reduction operation");
15016       }
15017     }
15018     if (Init && DeclareReductionRef.isUnset())
15019       S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
15020     else if (!Init)
15021       S.ActOnUninitializedDecl(RHSVD);
15022     if (RHSVD->isInvalidDecl())
15023       continue;
15024     if (!RHSVD->hasInit() &&
15025         (DeclareReductionRef.isUnset() || !S.LangOpts.CPlusPlus)) {
15026       S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
15027           << Type << ReductionIdRange;
15028       bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
15029                                VarDecl::DeclarationOnly;
15030       S.Diag(D->getLocation(),
15031              IsDecl ? diag::note_previous_decl : diag::note_defined_here)
15032           << D;
15033       continue;
15034     }
15035     // Store initializer for single element in private copy. Will be used during
15036     // codegen.
15037     PrivateVD->setInit(RHSVD->getInit());
15038     PrivateVD->setInitStyle(RHSVD->getInitStyle());
15039     DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
15040     ExprResult ReductionOp;
15041     if (DeclareReductionRef.isUsable()) {
15042       QualType RedTy = DeclareReductionRef.get()->getType();
15043       QualType PtrRedTy = Context.getPointerType(RedTy);
15044       ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
15045       ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
15046       if (!BasePath.empty()) {
15047         LHS = S.DefaultLvalueConversion(LHS.get());
15048         RHS = S.DefaultLvalueConversion(RHS.get());
15049         LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
15050                                        CK_UncheckedDerivedToBase, LHS.get(),
15051                                        &BasePath, LHS.get()->getValueKind());
15052         RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
15053                                        CK_UncheckedDerivedToBase, RHS.get(),
15054                                        &BasePath, RHS.get()->getValueKind());
15055       }
15056       FunctionProtoType::ExtProtoInfo EPI;
15057       QualType Params[] = {PtrRedTy, PtrRedTy};
15058       QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
15059       auto *OVE = new (Context) OpaqueValueExpr(
15060           ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
15061           S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
15062       Expr *Args[] = {LHS.get(), RHS.get()};
15063       ReductionOp =
15064           CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
15065     } else {
15066       ReductionOp = S.BuildBinOp(
15067           Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE);
15068       if (ReductionOp.isUsable()) {
15069         if (BOK != BO_LT && BOK != BO_GT) {
15070           ReductionOp =
15071               S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
15072                            BO_Assign, LHSDRE, ReductionOp.get());
15073         } else {
15074           auto *ConditionalOp = new (Context)
15075               ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
15076                                   Type, VK_LValue, OK_Ordinary);
15077           ReductionOp =
15078               S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
15079                            BO_Assign, LHSDRE, ConditionalOp);
15080         }
15081         if (ReductionOp.isUsable())
15082           ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(),
15083                                               /*DiscardedValue*/ false);
15084       }
15085       if (!ReductionOp.isUsable())
15086         continue;
15087     }
15088 
15089     // OpenMP [2.15.4.6, Restrictions, p.2]
15090     // A list item that appears in an in_reduction clause of a task construct
15091     // must appear in a task_reduction clause of a construct associated with a
15092     // taskgroup region that includes the participating task in its taskgroup
15093     // set. The construct associated with the innermost region that meets this
15094     // condition must specify the same reduction-identifier as the in_reduction
15095     // clause.
15096     if (ClauseKind == OMPC_in_reduction) {
15097       SourceRange ParentSR;
15098       BinaryOperatorKind ParentBOK;
15099       const Expr *ParentReductionOp = nullptr;
15100       Expr *ParentBOKTD = nullptr, *ParentReductionOpTD = nullptr;
15101       DSAStackTy::DSAVarData ParentBOKDSA =
15102           Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
15103                                                   ParentBOKTD);
15104       DSAStackTy::DSAVarData ParentReductionOpDSA =
15105           Stack->getTopMostTaskgroupReductionData(
15106               D, ParentSR, ParentReductionOp, ParentReductionOpTD);
15107       bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
15108       bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
15109       if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
15110           (DeclareReductionRef.isUsable() && IsParentBOK) ||
15111           (IsParentBOK && BOK != ParentBOK) || IsParentReductionOp) {
15112         bool EmitError = true;
15113         if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
15114           llvm::FoldingSetNodeID RedId, ParentRedId;
15115           ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
15116           DeclareReductionRef.get()->Profile(RedId, Context,
15117                                              /*Canonical=*/true);
15118           EmitError = RedId != ParentRedId;
15119         }
15120         if (EmitError) {
15121           S.Diag(ReductionId.getBeginLoc(),
15122                  diag::err_omp_reduction_identifier_mismatch)
15123               << ReductionIdRange << RefExpr->getSourceRange();
15124           S.Diag(ParentSR.getBegin(),
15125                  diag::note_omp_previous_reduction_identifier)
15126               << ParentSR
15127               << (IsParentBOK ? ParentBOKDSA.RefExpr
15128                               : ParentReductionOpDSA.RefExpr)
15129                      ->getSourceRange();
15130           continue;
15131         }
15132       }
15133       TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
15134     }
15135 
15136     DeclRefExpr *Ref = nullptr;
15137     Expr *VarsExpr = RefExpr->IgnoreParens();
15138     if (!VD && !S.CurContext->isDependentContext()) {
15139       if (ASE || OASE) {
15140         TransformExprToCaptures RebuildToCapture(S, D);
15141         VarsExpr =
15142             RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
15143         Ref = RebuildToCapture.getCapturedExpr();
15144       } else {
15145         VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
15146       }
15147       if (!S.isOpenMPCapturedDecl(D)) {
15148         RD.ExprCaptures.emplace_back(Ref->getDecl());
15149         if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
15150           ExprResult RefRes = S.DefaultLvalueConversion(Ref);
15151           if (!RefRes.isUsable())
15152             continue;
15153           ExprResult PostUpdateRes =
15154               S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
15155                            RefRes.get());
15156           if (!PostUpdateRes.isUsable())
15157             continue;
15158           if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
15159               Stack->getCurrentDirective() == OMPD_taskgroup) {
15160             S.Diag(RefExpr->getExprLoc(),
15161                    diag::err_omp_reduction_non_addressable_expression)
15162                 << RefExpr->getSourceRange();
15163             continue;
15164           }
15165           RD.ExprPostUpdates.emplace_back(
15166               S.IgnoredValueConversions(PostUpdateRes.get()).get());
15167         }
15168       }
15169     }
15170     // All reduction items are still marked as reduction (to do not increase
15171     // code base size).
15172     unsigned Modifier = RD.RedModifier;
15173     // Consider task_reductions as reductions with task modifier. Required for
15174     // correct analysis of in_reduction clauses.
15175     if (CurrDir == OMPD_taskgroup && ClauseKind == OMPC_task_reduction)
15176       Modifier = OMPC_REDUCTION_task;
15177     Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref, Modifier);
15178     if (Modifier == OMPC_REDUCTION_task &&
15179         (CurrDir == OMPD_taskgroup ||
15180          ((isOpenMPParallelDirective(CurrDir) ||
15181            isOpenMPWorksharingDirective(CurrDir)) &&
15182           !isOpenMPSimdDirective(CurrDir)))) {
15183       if (DeclareReductionRef.isUsable())
15184         Stack->addTaskgroupReductionData(D, ReductionIdRange,
15185                                          DeclareReductionRef.get());
15186       else
15187         Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
15188     }
15189     RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
15190             TaskgroupDescriptor);
15191   }
15192   return RD.Vars.empty();
15193 }
15194 
15195 OMPClause *Sema::ActOnOpenMPReductionClause(
15196     ArrayRef<Expr *> VarList, OpenMPReductionClauseModifier Modifier,
15197     SourceLocation StartLoc, SourceLocation LParenLoc,
15198     SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc,
15199     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
15200     ArrayRef<Expr *> UnresolvedReductions) {
15201   if (ModifierLoc.isValid() && Modifier == OMPC_REDUCTION_unknown) {
15202     Diag(LParenLoc, diag::err_omp_unexpected_clause_value)
15203         << getListOfPossibleValues(OMPC_reduction, /*First=*/0,
15204                                    /*Last=*/OMPC_REDUCTION_unknown)
15205         << getOpenMPClauseName(OMPC_reduction);
15206     return nullptr;
15207   }
15208   // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions
15209   // A reduction clause with the inscan reduction-modifier may only appear on a
15210   // worksharing-loop construct, a worksharing-loop SIMD construct, a simd
15211   // construct, a parallel worksharing-loop construct or a parallel
15212   // worksharing-loop SIMD construct.
15213   if (Modifier == OMPC_REDUCTION_inscan &&
15214       (DSAStack->getCurrentDirective() != OMPD_for &&
15215        DSAStack->getCurrentDirective() != OMPD_for_simd &&
15216        DSAStack->getCurrentDirective() != OMPD_simd &&
15217        DSAStack->getCurrentDirective() != OMPD_parallel_for &&
15218        DSAStack->getCurrentDirective() != OMPD_parallel_for_simd)) {
15219     Diag(ModifierLoc, diag::err_omp_wrong_inscan_reduction);
15220     return nullptr;
15221   }
15222 
15223   ReductionData RD(VarList.size(), Modifier);
15224   if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
15225                                   StartLoc, LParenLoc, ColonLoc, EndLoc,
15226                                   ReductionIdScopeSpec, ReductionId,
15227                                   UnresolvedReductions, RD))
15228     return nullptr;
15229 
15230   return OMPReductionClause::Create(
15231       Context, StartLoc, LParenLoc, ModifierLoc, ColonLoc, EndLoc, Modifier,
15232       RD.Vars, ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
15233       RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
15234       buildPreInits(Context, RD.ExprCaptures),
15235       buildPostUpdate(*this, RD.ExprPostUpdates));
15236 }
15237 
15238 OMPClause *Sema::ActOnOpenMPTaskReductionClause(
15239     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
15240     SourceLocation ColonLoc, SourceLocation EndLoc,
15241     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
15242     ArrayRef<Expr *> UnresolvedReductions) {
15243   ReductionData RD(VarList.size());
15244   if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList,
15245                                   StartLoc, LParenLoc, ColonLoc, EndLoc,
15246                                   ReductionIdScopeSpec, ReductionId,
15247                                   UnresolvedReductions, RD))
15248     return nullptr;
15249 
15250   return OMPTaskReductionClause::Create(
15251       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
15252       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
15253       RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
15254       buildPreInits(Context, RD.ExprCaptures),
15255       buildPostUpdate(*this, RD.ExprPostUpdates));
15256 }
15257 
15258 OMPClause *Sema::ActOnOpenMPInReductionClause(
15259     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
15260     SourceLocation ColonLoc, SourceLocation EndLoc,
15261     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
15262     ArrayRef<Expr *> UnresolvedReductions) {
15263   ReductionData RD(VarList.size());
15264   if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
15265                                   StartLoc, LParenLoc, ColonLoc, EndLoc,
15266                                   ReductionIdScopeSpec, ReductionId,
15267                                   UnresolvedReductions, RD))
15268     return nullptr;
15269 
15270   return OMPInReductionClause::Create(
15271       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
15272       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
15273       RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
15274       buildPreInits(Context, RD.ExprCaptures),
15275       buildPostUpdate(*this, RD.ExprPostUpdates));
15276 }
15277 
15278 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
15279                                      SourceLocation LinLoc) {
15280   if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
15281       LinKind == OMPC_LINEAR_unknown) {
15282     Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
15283     return true;
15284   }
15285   return false;
15286 }
15287 
15288 bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
15289                                  OpenMPLinearClauseKind LinKind, QualType Type,
15290                                  bool IsDeclareSimd) {
15291   const auto *VD = dyn_cast_or_null<VarDecl>(D);
15292   // A variable must not have an incomplete type or a reference type.
15293   if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
15294     return true;
15295   if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
15296       !Type->isReferenceType()) {
15297     Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
15298         << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
15299     return true;
15300   }
15301   Type = Type.getNonReferenceType();
15302 
15303   // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
15304   // A variable that is privatized must not have a const-qualified type
15305   // unless it is of class type with a mutable member. This restriction does
15306   // not apply to the firstprivate clause, nor to the linear clause on
15307   // declarative directives (like declare simd).
15308   if (!IsDeclareSimd &&
15309       rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc))
15310     return true;
15311 
15312   // A list item must be of integral or pointer type.
15313   Type = Type.getUnqualifiedType().getCanonicalType();
15314   const auto *Ty = Type.getTypePtrOrNull();
15315   if (!Ty || (LinKind != OMPC_LINEAR_ref && !Ty->isDependentType() &&
15316               !Ty->isIntegralType(Context) && !Ty->isPointerType())) {
15317     Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
15318     if (D) {
15319       bool IsDecl =
15320           !VD ||
15321           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
15322       Diag(D->getLocation(),
15323            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
15324           << D;
15325     }
15326     return true;
15327   }
15328   return false;
15329 }
15330 
15331 OMPClause *Sema::ActOnOpenMPLinearClause(
15332     ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
15333     SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
15334     SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
15335   SmallVector<Expr *, 8> Vars;
15336   SmallVector<Expr *, 8> Privates;
15337   SmallVector<Expr *, 8> Inits;
15338   SmallVector<Decl *, 4> ExprCaptures;
15339   SmallVector<Expr *, 4> ExprPostUpdates;
15340   if (CheckOpenMPLinearModifier(LinKind, LinLoc))
15341     LinKind = OMPC_LINEAR_val;
15342   for (Expr *RefExpr : VarList) {
15343     assert(RefExpr && "NULL expr in OpenMP linear clause.");
15344     SourceLocation ELoc;
15345     SourceRange ERange;
15346     Expr *SimpleRefExpr = RefExpr;
15347     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
15348     if (Res.second) {
15349       // It will be analyzed later.
15350       Vars.push_back(RefExpr);
15351       Privates.push_back(nullptr);
15352       Inits.push_back(nullptr);
15353     }
15354     ValueDecl *D = Res.first;
15355     if (!D)
15356       continue;
15357 
15358     QualType Type = D->getType();
15359     auto *VD = dyn_cast<VarDecl>(D);
15360 
15361     // OpenMP [2.14.3.7, linear clause]
15362     //  A list-item cannot appear in more than one linear clause.
15363     //  A list-item that appears in a linear clause cannot appear in any
15364     //  other data-sharing attribute clause.
15365     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
15366     if (DVar.RefExpr) {
15367       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
15368                                           << getOpenMPClauseName(OMPC_linear);
15369       reportOriginalDsa(*this, DSAStack, D, DVar);
15370       continue;
15371     }
15372 
15373     if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
15374       continue;
15375     Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
15376 
15377     // Build private copy of original var.
15378     VarDecl *Private =
15379         buildVarDecl(*this, ELoc, Type, D->getName(),
15380                      D->hasAttrs() ? &D->getAttrs() : nullptr,
15381                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
15382     DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
15383     // Build var to save initial value.
15384     VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
15385     Expr *InitExpr;
15386     DeclRefExpr *Ref = nullptr;
15387     if (!VD && !CurContext->isDependentContext()) {
15388       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
15389       if (!isOpenMPCapturedDecl(D)) {
15390         ExprCaptures.push_back(Ref->getDecl());
15391         if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
15392           ExprResult RefRes = DefaultLvalueConversion(Ref);
15393           if (!RefRes.isUsable())
15394             continue;
15395           ExprResult PostUpdateRes =
15396               BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
15397                          SimpleRefExpr, RefRes.get());
15398           if (!PostUpdateRes.isUsable())
15399             continue;
15400           ExprPostUpdates.push_back(
15401               IgnoredValueConversions(PostUpdateRes.get()).get());
15402         }
15403       }
15404     }
15405     if (LinKind == OMPC_LINEAR_uval)
15406       InitExpr = VD ? VD->getInit() : SimpleRefExpr;
15407     else
15408       InitExpr = VD ? SimpleRefExpr : Ref;
15409     AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
15410                          /*DirectInit=*/false);
15411     DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
15412 
15413     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
15414     Vars.push_back((VD || CurContext->isDependentContext())
15415                        ? RefExpr->IgnoreParens()
15416                        : Ref);
15417     Privates.push_back(PrivateRef);
15418     Inits.push_back(InitRef);
15419   }
15420 
15421   if (Vars.empty())
15422     return nullptr;
15423 
15424   Expr *StepExpr = Step;
15425   Expr *CalcStepExpr = nullptr;
15426   if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
15427       !Step->isInstantiationDependent() &&
15428       !Step->containsUnexpandedParameterPack()) {
15429     SourceLocation StepLoc = Step->getBeginLoc();
15430     ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
15431     if (Val.isInvalid())
15432       return nullptr;
15433     StepExpr = Val.get();
15434 
15435     // Build var to save the step value.
15436     VarDecl *SaveVar =
15437         buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
15438     ExprResult SaveRef =
15439         buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
15440     ExprResult CalcStep =
15441         BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
15442     CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false);
15443 
15444     // Warn about zero linear step (it would be probably better specified as
15445     // making corresponding variables 'const').
15446     llvm::APSInt Result;
15447     bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
15448     if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
15449       Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
15450                                                      << (Vars.size() > 1);
15451     if (!IsConstant && CalcStep.isUsable()) {
15452       // Calculate the step beforehand instead of doing this on each iteration.
15453       // (This is not used if the number of iterations may be kfold-ed).
15454       CalcStepExpr = CalcStep.get();
15455     }
15456   }
15457 
15458   return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
15459                                  ColonLoc, EndLoc, Vars, Privates, Inits,
15460                                  StepExpr, CalcStepExpr,
15461                                  buildPreInits(Context, ExprCaptures),
15462                                  buildPostUpdate(*this, ExprPostUpdates));
15463 }
15464 
15465 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
15466                                      Expr *NumIterations, Sema &SemaRef,
15467                                      Scope *S, DSAStackTy *Stack) {
15468   // Walk the vars and build update/final expressions for the CodeGen.
15469   SmallVector<Expr *, 8> Updates;
15470   SmallVector<Expr *, 8> Finals;
15471   SmallVector<Expr *, 8> UsedExprs;
15472   Expr *Step = Clause.getStep();
15473   Expr *CalcStep = Clause.getCalcStep();
15474   // OpenMP [2.14.3.7, linear clause]
15475   // If linear-step is not specified it is assumed to be 1.
15476   if (!Step)
15477     Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
15478   else if (CalcStep)
15479     Step = cast<BinaryOperator>(CalcStep)->getLHS();
15480   bool HasErrors = false;
15481   auto CurInit = Clause.inits().begin();
15482   auto CurPrivate = Clause.privates().begin();
15483   OpenMPLinearClauseKind LinKind = Clause.getModifier();
15484   for (Expr *RefExpr : Clause.varlists()) {
15485     SourceLocation ELoc;
15486     SourceRange ERange;
15487     Expr *SimpleRefExpr = RefExpr;
15488     auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange);
15489     ValueDecl *D = Res.first;
15490     if (Res.second || !D) {
15491       Updates.push_back(nullptr);
15492       Finals.push_back(nullptr);
15493       HasErrors = true;
15494       continue;
15495     }
15496     auto &&Info = Stack->isLoopControlVariable(D);
15497     // OpenMP [2.15.11, distribute simd Construct]
15498     // A list item may not appear in a linear clause, unless it is the loop
15499     // iteration variable.
15500     if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
15501         isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
15502       SemaRef.Diag(ELoc,
15503                    diag::err_omp_linear_distribute_var_non_loop_iteration);
15504       Updates.push_back(nullptr);
15505       Finals.push_back(nullptr);
15506       HasErrors = true;
15507       continue;
15508     }
15509     Expr *InitExpr = *CurInit;
15510 
15511     // Build privatized reference to the current linear var.
15512     auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
15513     Expr *CapturedRef;
15514     if (LinKind == OMPC_LINEAR_uval)
15515       CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
15516     else
15517       CapturedRef =
15518           buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
15519                            DE->getType().getUnqualifiedType(), DE->getExprLoc(),
15520                            /*RefersToCapture=*/true);
15521 
15522     // Build update: Var = InitExpr + IV * Step
15523     ExprResult Update;
15524     if (!Info.first)
15525       Update = buildCounterUpdate(
15526           SemaRef, S, RefExpr->getExprLoc(), *CurPrivate, InitExpr, IV, Step,
15527           /*Subtract=*/false, /*IsNonRectangularLB=*/false);
15528     else
15529       Update = *CurPrivate;
15530     Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(),
15531                                          /*DiscardedValue*/ false);
15532 
15533     // Build final: Var = InitExpr + NumIterations * Step
15534     ExprResult Final;
15535     if (!Info.first)
15536       Final =
15537           buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
15538                              InitExpr, NumIterations, Step, /*Subtract=*/false,
15539                              /*IsNonRectangularLB=*/false);
15540     else
15541       Final = *CurPrivate;
15542     Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(),
15543                                         /*DiscardedValue*/ false);
15544 
15545     if (!Update.isUsable() || !Final.isUsable()) {
15546       Updates.push_back(nullptr);
15547       Finals.push_back(nullptr);
15548       UsedExprs.push_back(nullptr);
15549       HasErrors = true;
15550     } else {
15551       Updates.push_back(Update.get());
15552       Finals.push_back(Final.get());
15553       if (!Info.first)
15554         UsedExprs.push_back(SimpleRefExpr);
15555     }
15556     ++CurInit;
15557     ++CurPrivate;
15558   }
15559   if (Expr *S = Clause.getStep())
15560     UsedExprs.push_back(S);
15561   // Fill the remaining part with the nullptr.
15562   UsedExprs.append(Clause.varlist_size() + 1 - UsedExprs.size(), nullptr);
15563   Clause.setUpdates(Updates);
15564   Clause.setFinals(Finals);
15565   Clause.setUsedExprs(UsedExprs);
15566   return HasErrors;
15567 }
15568 
15569 OMPClause *Sema::ActOnOpenMPAlignedClause(
15570     ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
15571     SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
15572   SmallVector<Expr *, 8> Vars;
15573   for (Expr *RefExpr : VarList) {
15574     assert(RefExpr && "NULL expr in OpenMP linear clause.");
15575     SourceLocation ELoc;
15576     SourceRange ERange;
15577     Expr *SimpleRefExpr = RefExpr;
15578     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
15579     if (Res.second) {
15580       // It will be analyzed later.
15581       Vars.push_back(RefExpr);
15582     }
15583     ValueDecl *D = Res.first;
15584     if (!D)
15585       continue;
15586 
15587     QualType QType = D->getType();
15588     auto *VD = dyn_cast<VarDecl>(D);
15589 
15590     // OpenMP  [2.8.1, simd construct, Restrictions]
15591     // The type of list items appearing in the aligned clause must be
15592     // array, pointer, reference to array, or reference to pointer.
15593     QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
15594     const Type *Ty = QType.getTypePtrOrNull();
15595     if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
15596       Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
15597           << QType << getLangOpts().CPlusPlus << ERange;
15598       bool IsDecl =
15599           !VD ||
15600           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
15601       Diag(D->getLocation(),
15602            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
15603           << D;
15604       continue;
15605     }
15606 
15607     // OpenMP  [2.8.1, simd construct, Restrictions]
15608     // A list-item cannot appear in more than one aligned clause.
15609     if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
15610       Diag(ELoc, diag::err_omp_used_in_clause_twice)
15611           << 0 << getOpenMPClauseName(OMPC_aligned) << ERange;
15612       Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
15613           << getOpenMPClauseName(OMPC_aligned);
15614       continue;
15615     }
15616 
15617     DeclRefExpr *Ref = nullptr;
15618     if (!VD && isOpenMPCapturedDecl(D))
15619       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
15620     Vars.push_back(DefaultFunctionArrayConversion(
15621                        (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
15622                        .get());
15623   }
15624 
15625   // OpenMP [2.8.1, simd construct, Description]
15626   // The parameter of the aligned clause, alignment, must be a constant
15627   // positive integer expression.
15628   // If no optional parameter is specified, implementation-defined default
15629   // alignments for SIMD instructions on the target platforms are assumed.
15630   if (Alignment != nullptr) {
15631     ExprResult AlignResult =
15632         VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
15633     if (AlignResult.isInvalid())
15634       return nullptr;
15635     Alignment = AlignResult.get();
15636   }
15637   if (Vars.empty())
15638     return nullptr;
15639 
15640   return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
15641                                   EndLoc, Vars, Alignment);
15642 }
15643 
15644 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
15645                                          SourceLocation StartLoc,
15646                                          SourceLocation LParenLoc,
15647                                          SourceLocation EndLoc) {
15648   SmallVector<Expr *, 8> Vars;
15649   SmallVector<Expr *, 8> SrcExprs;
15650   SmallVector<Expr *, 8> DstExprs;
15651   SmallVector<Expr *, 8> AssignmentOps;
15652   for (Expr *RefExpr : VarList) {
15653     assert(RefExpr && "NULL expr in OpenMP copyin clause.");
15654     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
15655       // It will be analyzed later.
15656       Vars.push_back(RefExpr);
15657       SrcExprs.push_back(nullptr);
15658       DstExprs.push_back(nullptr);
15659       AssignmentOps.push_back(nullptr);
15660       continue;
15661     }
15662 
15663     SourceLocation ELoc = RefExpr->getExprLoc();
15664     // OpenMP [2.1, C/C++]
15665     //  A list item is a variable name.
15666     // OpenMP  [2.14.4.1, Restrictions, p.1]
15667     //  A list item that appears in a copyin clause must be threadprivate.
15668     auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
15669     if (!DE || !isa<VarDecl>(DE->getDecl())) {
15670       Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
15671           << 0 << RefExpr->getSourceRange();
15672       continue;
15673     }
15674 
15675     Decl *D = DE->getDecl();
15676     auto *VD = cast<VarDecl>(D);
15677 
15678     QualType Type = VD->getType();
15679     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
15680       // It will be analyzed later.
15681       Vars.push_back(DE);
15682       SrcExprs.push_back(nullptr);
15683       DstExprs.push_back(nullptr);
15684       AssignmentOps.push_back(nullptr);
15685       continue;
15686     }
15687 
15688     // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
15689     //  A list item that appears in a copyin clause must be threadprivate.
15690     if (!DSAStack->isThreadPrivate(VD)) {
15691       Diag(ELoc, diag::err_omp_required_access)
15692           << getOpenMPClauseName(OMPC_copyin)
15693           << getOpenMPDirectiveName(OMPD_threadprivate);
15694       continue;
15695     }
15696 
15697     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
15698     //  A variable of class type (or array thereof) that appears in a
15699     //  copyin clause requires an accessible, unambiguous copy assignment
15700     //  operator for the class type.
15701     QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
15702     VarDecl *SrcVD =
15703         buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(),
15704                      ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
15705     DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
15706         *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
15707     VarDecl *DstVD =
15708         buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst",
15709                      VD->hasAttrs() ? &VD->getAttrs() : nullptr);
15710     DeclRefExpr *PseudoDstExpr =
15711         buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
15712     // For arrays generate assignment operation for single element and replace
15713     // it by the original array element in CodeGen.
15714     ExprResult AssignmentOp =
15715         BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr,
15716                    PseudoSrcExpr);
15717     if (AssignmentOp.isInvalid())
15718       continue;
15719     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
15720                                        /*DiscardedValue*/ false);
15721     if (AssignmentOp.isInvalid())
15722       continue;
15723 
15724     DSAStack->addDSA(VD, DE, OMPC_copyin);
15725     Vars.push_back(DE);
15726     SrcExprs.push_back(PseudoSrcExpr);
15727     DstExprs.push_back(PseudoDstExpr);
15728     AssignmentOps.push_back(AssignmentOp.get());
15729   }
15730 
15731   if (Vars.empty())
15732     return nullptr;
15733 
15734   return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
15735                                  SrcExprs, DstExprs, AssignmentOps);
15736 }
15737 
15738 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
15739                                               SourceLocation StartLoc,
15740                                               SourceLocation LParenLoc,
15741                                               SourceLocation EndLoc) {
15742   SmallVector<Expr *, 8> Vars;
15743   SmallVector<Expr *, 8> SrcExprs;
15744   SmallVector<Expr *, 8> DstExprs;
15745   SmallVector<Expr *, 8> AssignmentOps;
15746   for (Expr *RefExpr : VarList) {
15747     assert(RefExpr && "NULL expr in OpenMP linear clause.");
15748     SourceLocation ELoc;
15749     SourceRange ERange;
15750     Expr *SimpleRefExpr = RefExpr;
15751     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
15752     if (Res.second) {
15753       // It will be analyzed later.
15754       Vars.push_back(RefExpr);
15755       SrcExprs.push_back(nullptr);
15756       DstExprs.push_back(nullptr);
15757       AssignmentOps.push_back(nullptr);
15758     }
15759     ValueDecl *D = Res.first;
15760     if (!D)
15761       continue;
15762 
15763     QualType Type = D->getType();
15764     auto *VD = dyn_cast<VarDecl>(D);
15765 
15766     // OpenMP [2.14.4.2, Restrictions, p.2]
15767     //  A list item that appears in a copyprivate clause may not appear in a
15768     //  private or firstprivate clause on the single construct.
15769     if (!VD || !DSAStack->isThreadPrivate(VD)) {
15770       DSAStackTy::DSAVarData DVar =
15771           DSAStack->getTopDSA(D, /*FromParent=*/false);
15772       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
15773           DVar.RefExpr) {
15774         Diag(ELoc, diag::err_omp_wrong_dsa)
15775             << getOpenMPClauseName(DVar.CKind)
15776             << getOpenMPClauseName(OMPC_copyprivate);
15777         reportOriginalDsa(*this, DSAStack, D, DVar);
15778         continue;
15779       }
15780 
15781       // OpenMP [2.11.4.2, Restrictions, p.1]
15782       //  All list items that appear in a copyprivate clause must be either
15783       //  threadprivate or private in the enclosing context.
15784       if (DVar.CKind == OMPC_unknown) {
15785         DVar = DSAStack->getImplicitDSA(D, false);
15786         if (DVar.CKind == OMPC_shared) {
15787           Diag(ELoc, diag::err_omp_required_access)
15788               << getOpenMPClauseName(OMPC_copyprivate)
15789               << "threadprivate or private in the enclosing context";
15790           reportOriginalDsa(*this, DSAStack, D, DVar);
15791           continue;
15792         }
15793       }
15794     }
15795 
15796     // Variably modified types are not supported.
15797     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
15798       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
15799           << getOpenMPClauseName(OMPC_copyprivate) << Type
15800           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
15801       bool IsDecl =
15802           !VD ||
15803           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
15804       Diag(D->getLocation(),
15805            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
15806           << D;
15807       continue;
15808     }
15809 
15810     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
15811     //  A variable of class type (or array thereof) that appears in a
15812     //  copyin clause requires an accessible, unambiguous copy assignment
15813     //  operator for the class type.
15814     Type = Context.getBaseElementType(Type.getNonReferenceType())
15815                .getUnqualifiedType();
15816     VarDecl *SrcVD =
15817         buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src",
15818                      D->hasAttrs() ? &D->getAttrs() : nullptr);
15819     DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
15820     VarDecl *DstVD =
15821         buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst",
15822                      D->hasAttrs() ? &D->getAttrs() : nullptr);
15823     DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
15824     ExprResult AssignmentOp = BuildBinOp(
15825         DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr);
15826     if (AssignmentOp.isInvalid())
15827       continue;
15828     AssignmentOp =
15829         ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
15830     if (AssignmentOp.isInvalid())
15831       continue;
15832 
15833     // No need to mark vars as copyprivate, they are already threadprivate or
15834     // implicitly private.
15835     assert(VD || isOpenMPCapturedDecl(D));
15836     Vars.push_back(
15837         VD ? RefExpr->IgnoreParens()
15838            : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
15839     SrcExprs.push_back(PseudoSrcExpr);
15840     DstExprs.push_back(PseudoDstExpr);
15841     AssignmentOps.push_back(AssignmentOp.get());
15842   }
15843 
15844   if (Vars.empty())
15845     return nullptr;
15846 
15847   return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
15848                                       Vars, SrcExprs, DstExprs, AssignmentOps);
15849 }
15850 
15851 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
15852                                         SourceLocation StartLoc,
15853                                         SourceLocation LParenLoc,
15854                                         SourceLocation EndLoc) {
15855   if (VarList.empty())
15856     return nullptr;
15857 
15858   return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
15859 }
15860 
15861 /// Tries to find omp_depend_t. type.
15862 static bool findOMPDependT(Sema &S, SourceLocation Loc, DSAStackTy *Stack,
15863                            bool Diagnose = true) {
15864   QualType OMPDependT = Stack->getOMPDependT();
15865   if (!OMPDependT.isNull())
15866     return true;
15867   IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_depend_t");
15868   ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope());
15869   if (!PT.getAsOpaquePtr() || PT.get().isNull()) {
15870     if (Diagnose)
15871       S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_depend_t";
15872     return false;
15873   }
15874   Stack->setOMPDependT(PT.get());
15875   return true;
15876 }
15877 
15878 OMPClause *Sema::ActOnOpenMPDepobjClause(Expr *Depobj, SourceLocation StartLoc,
15879                                          SourceLocation LParenLoc,
15880                                          SourceLocation EndLoc) {
15881   if (!Depobj)
15882     return nullptr;
15883 
15884   bool OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack);
15885 
15886   // OpenMP 5.0, 2.17.10.1 depobj Construct
15887   // depobj is an lvalue expression of type omp_depend_t.
15888   if (!Depobj->isTypeDependent() && !Depobj->isValueDependent() &&
15889       !Depobj->isInstantiationDependent() &&
15890       !Depobj->containsUnexpandedParameterPack() &&
15891       (OMPDependTFound &&
15892        !Context.typesAreCompatible(DSAStack->getOMPDependT(), Depobj->getType(),
15893                                    /*CompareUnqualified=*/true))) {
15894     Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue)
15895         << 0 << Depobj->getType() << Depobj->getSourceRange();
15896   }
15897 
15898   if (!Depobj->isLValue()) {
15899     Diag(Depobj->getExprLoc(), diag::err_omp_expected_omp_depend_t_lvalue)
15900         << 1 << Depobj->getSourceRange();
15901   }
15902 
15903   return OMPDepobjClause::Create(Context, StartLoc, LParenLoc, EndLoc, Depobj);
15904 }
15905 
15906 OMPClause *
15907 Sema::ActOnOpenMPDependClause(Expr *DepModifier, OpenMPDependClauseKind DepKind,
15908                               SourceLocation DepLoc, SourceLocation ColonLoc,
15909                               ArrayRef<Expr *> VarList, SourceLocation StartLoc,
15910                               SourceLocation LParenLoc, SourceLocation EndLoc) {
15911   if (DSAStack->getCurrentDirective() == OMPD_ordered &&
15912       DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
15913     Diag(DepLoc, diag::err_omp_unexpected_clause_value)
15914         << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
15915     return nullptr;
15916   }
15917   if ((DSAStack->getCurrentDirective() != OMPD_ordered ||
15918        DSAStack->getCurrentDirective() == OMPD_depobj) &&
15919       (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
15920        DepKind == OMPC_DEPEND_sink ||
15921        ((LangOpts.OpenMP < 50 ||
15922          DSAStack->getCurrentDirective() == OMPD_depobj) &&
15923         DepKind == OMPC_DEPEND_depobj))) {
15924     SmallVector<unsigned, 3> Except;
15925     Except.push_back(OMPC_DEPEND_source);
15926     Except.push_back(OMPC_DEPEND_sink);
15927     if (LangOpts.OpenMP < 50 || DSAStack->getCurrentDirective() == OMPD_depobj)
15928       Except.push_back(OMPC_DEPEND_depobj);
15929     std::string Expected = (LangOpts.OpenMP >= 50 && !DepModifier)
15930                                ? "depend modifier(iterator) or "
15931                                : "";
15932     Diag(DepLoc, diag::err_omp_unexpected_clause_value)
15933         << Expected + getListOfPossibleValues(OMPC_depend, /*First=*/0,
15934                                               /*Last=*/OMPC_DEPEND_unknown,
15935                                               Except)
15936         << getOpenMPClauseName(OMPC_depend);
15937     return nullptr;
15938   }
15939   if (DepModifier &&
15940       (DepKind == OMPC_DEPEND_source || DepKind == OMPC_DEPEND_sink)) {
15941     Diag(DepModifier->getExprLoc(),
15942          diag::err_omp_depend_sink_source_with_modifier);
15943     return nullptr;
15944   }
15945   if (DepModifier &&
15946       !DepModifier->getType()->isSpecificBuiltinType(BuiltinType::OMPIterator))
15947     Diag(DepModifier->getExprLoc(), diag::err_omp_depend_modifier_not_iterator);
15948 
15949   SmallVector<Expr *, 8> Vars;
15950   DSAStackTy::OperatorOffsetTy OpsOffs;
15951   llvm::APSInt DepCounter(/*BitWidth=*/32);
15952   llvm::APSInt TotalDepCount(/*BitWidth=*/32);
15953   if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
15954     if (const Expr *OrderedCountExpr =
15955             DSAStack->getParentOrderedRegionParam().first) {
15956       TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
15957       TotalDepCount.setIsUnsigned(/*Val=*/true);
15958     }
15959   }
15960   for (Expr *RefExpr : VarList) {
15961     assert(RefExpr && "NULL expr in OpenMP shared clause.");
15962     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
15963       // It will be analyzed later.
15964       Vars.push_back(RefExpr);
15965       continue;
15966     }
15967 
15968     SourceLocation ELoc = RefExpr->getExprLoc();
15969     Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
15970     if (DepKind == OMPC_DEPEND_sink) {
15971       if (DSAStack->getParentOrderedRegionParam().first &&
15972           DepCounter >= TotalDepCount) {
15973         Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
15974         continue;
15975       }
15976       ++DepCounter;
15977       // OpenMP  [2.13.9, Summary]
15978       // depend(dependence-type : vec), where dependence-type is:
15979       // 'sink' and where vec is the iteration vector, which has the form:
15980       //  x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
15981       // where n is the value specified by the ordered clause in the loop
15982       // directive, xi denotes the loop iteration variable of the i-th nested
15983       // loop associated with the loop directive, and di is a constant
15984       // non-negative integer.
15985       if (CurContext->isDependentContext()) {
15986         // It will be analyzed later.
15987         Vars.push_back(RefExpr);
15988         continue;
15989       }
15990       SimpleExpr = SimpleExpr->IgnoreImplicit();
15991       OverloadedOperatorKind OOK = OO_None;
15992       SourceLocation OOLoc;
15993       Expr *LHS = SimpleExpr;
15994       Expr *RHS = nullptr;
15995       if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
15996         OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
15997         OOLoc = BO->getOperatorLoc();
15998         LHS = BO->getLHS()->IgnoreParenImpCasts();
15999         RHS = BO->getRHS()->IgnoreParenImpCasts();
16000       } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
16001         OOK = OCE->getOperator();
16002         OOLoc = OCE->getOperatorLoc();
16003         LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
16004         RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
16005       } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
16006         OOK = MCE->getMethodDecl()
16007                   ->getNameInfo()
16008                   .getName()
16009                   .getCXXOverloadedOperator();
16010         OOLoc = MCE->getCallee()->getExprLoc();
16011         LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
16012         RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
16013       }
16014       SourceLocation ELoc;
16015       SourceRange ERange;
16016       auto Res = getPrivateItem(*this, LHS, ELoc, ERange);
16017       if (Res.second) {
16018         // It will be analyzed later.
16019         Vars.push_back(RefExpr);
16020       }
16021       ValueDecl *D = Res.first;
16022       if (!D)
16023         continue;
16024 
16025       if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
16026         Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
16027         continue;
16028       }
16029       if (RHS) {
16030         ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
16031             RHS, OMPC_depend, /*StrictlyPositive=*/false);
16032         if (RHSRes.isInvalid())
16033           continue;
16034       }
16035       if (!CurContext->isDependentContext() &&
16036           DSAStack->getParentOrderedRegionParam().first &&
16037           DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
16038         const ValueDecl *VD =
16039             DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue());
16040         if (VD)
16041           Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
16042               << 1 << VD;
16043         else
16044           Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
16045         continue;
16046       }
16047       OpsOffs.emplace_back(RHS, OOK);
16048     } else {
16049       bool OMPDependTFound = LangOpts.OpenMP >= 50;
16050       if (OMPDependTFound)
16051         OMPDependTFound = findOMPDependT(*this, StartLoc, DSAStack,
16052                                          DepKind == OMPC_DEPEND_depobj);
16053       if (DepKind == OMPC_DEPEND_depobj) {
16054         // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++
16055         // List items used in depend clauses with the depobj dependence type
16056         // must be expressions of the omp_depend_t type.
16057         if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() &&
16058             !RefExpr->isInstantiationDependent() &&
16059             !RefExpr->containsUnexpandedParameterPack() &&
16060             (OMPDependTFound &&
16061              !Context.hasSameUnqualifiedType(DSAStack->getOMPDependT(),
16062                                              RefExpr->getType()))) {
16063           Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue)
16064               << 0 << RefExpr->getType() << RefExpr->getSourceRange();
16065           continue;
16066         }
16067         if (!RefExpr->isLValue()) {
16068           Diag(ELoc, diag::err_omp_expected_omp_depend_t_lvalue)
16069               << 1 << RefExpr->getType() << RefExpr->getSourceRange();
16070           continue;
16071         }
16072       } else {
16073         // OpenMP 5.0 [2.17.11, Restrictions]
16074         // List items used in depend clauses cannot be zero-length array
16075         // sections.
16076         QualType ExprTy = RefExpr->getType().getNonReferenceType();
16077         const auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
16078         if (OASE) {
16079           QualType BaseType =
16080               OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
16081           if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
16082             ExprTy = ATy->getElementType();
16083           else
16084             ExprTy = BaseType->getPointeeType();
16085           ExprTy = ExprTy.getNonReferenceType();
16086           const Expr *Length = OASE->getLength();
16087           Expr::EvalResult Result;
16088           if (Length && !Length->isValueDependent() &&
16089               Length->EvaluateAsInt(Result, Context) &&
16090               Result.Val.getInt().isNullValue()) {
16091             Diag(ELoc,
16092                  diag::err_omp_depend_zero_length_array_section_not_allowed)
16093                 << SimpleExpr->getSourceRange();
16094             continue;
16095           }
16096         }
16097 
16098         // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++
16099         // List items used in depend clauses with the in, out, inout or
16100         // mutexinoutset dependence types cannot be expressions of the
16101         // omp_depend_t type.
16102         if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() &&
16103             !RefExpr->isInstantiationDependent() &&
16104             !RefExpr->containsUnexpandedParameterPack() &&
16105             (OMPDependTFound &&
16106              DSAStack->getOMPDependT().getTypePtr() == ExprTy.getTypePtr())) {
16107           Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
16108               << (LangOpts.OpenMP >= 50 ? 1 : 0) << 1
16109               << RefExpr->getSourceRange();
16110           continue;
16111         }
16112 
16113         auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
16114         if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
16115             (ASE &&
16116              !ASE->getBase()
16117                   ->getType()
16118                   .getNonReferenceType()
16119                   ->isPointerType() &&
16120              !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
16121           Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
16122               << (LangOpts.OpenMP >= 50 ? 1 : 0)
16123               << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange();
16124           continue;
16125         }
16126 
16127         ExprResult Res;
16128         {
16129           Sema::TentativeAnalysisScope Trap(*this);
16130           Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf,
16131                                      RefExpr->IgnoreParenImpCasts());
16132         }
16133         if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr) &&
16134             !isa<OMPArrayShapingExpr>(SimpleExpr)) {
16135           Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
16136               << (LangOpts.OpenMP >= 50 ? 1 : 0)
16137               << (LangOpts.OpenMP >= 50 ? 1 : 0) << RefExpr->getSourceRange();
16138           continue;
16139         }
16140       }
16141     }
16142     Vars.push_back(RefExpr->IgnoreParenImpCasts());
16143   }
16144 
16145   if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
16146       TotalDepCount > VarList.size() &&
16147       DSAStack->getParentOrderedRegionParam().first &&
16148       DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
16149     Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
16150         << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
16151   }
16152   if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
16153       Vars.empty())
16154     return nullptr;
16155 
16156   auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
16157                                     DepModifier, DepKind, DepLoc, ColonLoc,
16158                                     Vars, TotalDepCount.getZExtValue());
16159   if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
16160       DSAStack->isParentOrderedRegion())
16161     DSAStack->addDoacrossDependClause(C, OpsOffs);
16162   return C;
16163 }
16164 
16165 OMPClause *Sema::ActOnOpenMPDeviceClause(OpenMPDeviceClauseModifier Modifier,
16166                                          Expr *Device, SourceLocation StartLoc,
16167                                          SourceLocation LParenLoc,
16168                                          SourceLocation ModifierLoc,
16169                                          SourceLocation EndLoc) {
16170   assert((ModifierLoc.isInvalid() || LangOpts.OpenMP >= 50) &&
16171          "Unexpected device modifier in OpenMP < 50.");
16172 
16173   bool ErrorFound = false;
16174   if (ModifierLoc.isValid() && Modifier == OMPC_DEVICE_unknown) {
16175     std::string Values =
16176         getListOfPossibleValues(OMPC_device, /*First=*/0, OMPC_DEVICE_unknown);
16177     Diag(ModifierLoc, diag::err_omp_unexpected_clause_value)
16178         << Values << getOpenMPClauseName(OMPC_device);
16179     ErrorFound = true;
16180   }
16181 
16182   Expr *ValExpr = Device;
16183   Stmt *HelperValStmt = nullptr;
16184 
16185   // OpenMP [2.9.1, Restrictions]
16186   // The device expression must evaluate to a non-negative integer value.
16187   ErrorFound = !isNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
16188                                           /*StrictlyPositive=*/false) ||
16189                ErrorFound;
16190   if (ErrorFound)
16191     return nullptr;
16192 
16193   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
16194   OpenMPDirectiveKind CaptureRegion =
16195       getOpenMPCaptureRegionForClause(DKind, OMPC_device, LangOpts.OpenMP);
16196   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
16197     ValExpr = MakeFullExpr(ValExpr).get();
16198     llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
16199     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
16200     HelperValStmt = buildPreInits(Context, Captures);
16201   }
16202 
16203   return new (Context)
16204       OMPDeviceClause(Modifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
16205                       LParenLoc, ModifierLoc, EndLoc);
16206 }
16207 
16208 static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
16209                               DSAStackTy *Stack, QualType QTy,
16210                               bool FullCheck = true) {
16211   NamedDecl *ND;
16212   if (QTy->isIncompleteType(&ND)) {
16213     SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
16214     return false;
16215   }
16216   if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
16217       !QTy.isTriviallyCopyableType(SemaRef.Context))
16218     SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
16219   return true;
16220 }
16221 
16222 /// Return true if it can be proven that the provided array expression
16223 /// (array section or array subscript) does NOT specify the whole size of the
16224 /// array whose base type is \a BaseQTy.
16225 static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
16226                                                         const Expr *E,
16227                                                         QualType BaseQTy) {
16228   const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
16229 
16230   // If this is an array subscript, it refers to the whole size if the size of
16231   // the dimension is constant and equals 1. Also, an array section assumes the
16232   // format of an array subscript if no colon is used.
16233   if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
16234     if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
16235       return ATy->getSize().getSExtValue() != 1;
16236     // Size can't be evaluated statically.
16237     return false;
16238   }
16239 
16240   assert(OASE && "Expecting array section if not an array subscript.");
16241   const Expr *LowerBound = OASE->getLowerBound();
16242   const Expr *Length = OASE->getLength();
16243 
16244   // If there is a lower bound that does not evaluates to zero, we are not
16245   // covering the whole dimension.
16246   if (LowerBound) {
16247     Expr::EvalResult Result;
16248     if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext()))
16249       return false; // Can't get the integer value as a constant.
16250 
16251     llvm::APSInt ConstLowerBound = Result.Val.getInt();
16252     if (ConstLowerBound.getSExtValue())
16253       return true;
16254   }
16255 
16256   // If we don't have a length we covering the whole dimension.
16257   if (!Length)
16258     return false;
16259 
16260   // If the base is a pointer, we don't have a way to get the size of the
16261   // pointee.
16262   if (BaseQTy->isPointerType())
16263     return false;
16264 
16265   // We can only check if the length is the same as the size of the dimension
16266   // if we have a constant array.
16267   const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
16268   if (!CATy)
16269     return false;
16270 
16271   Expr::EvalResult Result;
16272   if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
16273     return false; // Can't get the integer value as a constant.
16274 
16275   llvm::APSInt ConstLength = Result.Val.getInt();
16276   return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
16277 }
16278 
16279 // Return true if it can be proven that the provided array expression (array
16280 // section or array subscript) does NOT specify a single element of the array
16281 // whose base type is \a BaseQTy.
16282 static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
16283                                                         const Expr *E,
16284                                                         QualType BaseQTy) {
16285   const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
16286 
16287   // An array subscript always refer to a single element. Also, an array section
16288   // assumes the format of an array subscript if no colon is used.
16289   if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
16290     return false;
16291 
16292   assert(OASE && "Expecting array section if not an array subscript.");
16293   const Expr *Length = OASE->getLength();
16294 
16295   // If we don't have a length we have to check if the array has unitary size
16296   // for this dimension. Also, we should always expect a length if the base type
16297   // is pointer.
16298   if (!Length) {
16299     if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
16300       return ATy->getSize().getSExtValue() != 1;
16301     // We cannot assume anything.
16302     return false;
16303   }
16304 
16305   // Check if the length evaluates to 1.
16306   Expr::EvalResult Result;
16307   if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
16308     return false; // Can't get the integer value as a constant.
16309 
16310   llvm::APSInt ConstLength = Result.Val.getInt();
16311   return ConstLength.getSExtValue() != 1;
16312 }
16313 
16314 // The base of elements of list in a map clause have to be either:
16315 //  - a reference to variable or field.
16316 //  - a member expression.
16317 //  - an array expression.
16318 //
16319 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
16320 // reference to 'r'.
16321 //
16322 // If we have:
16323 //
16324 // struct SS {
16325 //   Bla S;
16326 //   foo() {
16327 //     #pragma omp target map (S.Arr[:12]);
16328 //   }
16329 // }
16330 //
16331 // We want to retrieve the member expression 'this->S';
16332 
16333 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
16334 //  If a list item is an array section, it must specify contiguous storage.
16335 //
16336 // For this restriction it is sufficient that we make sure only references
16337 // to variables or fields and array expressions, and that no array sections
16338 // exist except in the rightmost expression (unless they cover the whole
16339 // dimension of the array). E.g. these would be invalid:
16340 //
16341 //   r.ArrS[3:5].Arr[6:7]
16342 //
16343 //   r.ArrS[3:5].x
16344 //
16345 // but these would be valid:
16346 //   r.ArrS[3].Arr[6:7]
16347 //
16348 //   r.ArrS[3].x
16349 namespace {
16350 class MapBaseChecker final : public StmtVisitor<MapBaseChecker, bool> {
16351   Sema &SemaRef;
16352   OpenMPClauseKind CKind = OMPC_unknown;
16353   OMPClauseMappableExprCommon::MappableExprComponentList &Components;
16354   bool NoDiagnose = false;
16355   const Expr *RelevantExpr = nullptr;
16356   bool AllowUnitySizeArraySection = true;
16357   bool AllowWholeSizeArraySection = true;
16358   SourceLocation ELoc;
16359   SourceRange ERange;
16360 
16361   void emitErrorMsg() {
16362     // If nothing else worked, this is not a valid map clause expression.
16363     if (SemaRef.getLangOpts().OpenMP < 50) {
16364       SemaRef.Diag(ELoc,
16365                    diag::err_omp_expected_named_var_member_or_array_expression)
16366           << ERange;
16367     } else {
16368       SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses)
16369           << getOpenMPClauseName(CKind) << ERange;
16370     }
16371   }
16372 
16373 public:
16374   bool VisitDeclRefExpr(DeclRefExpr *DRE) {
16375     if (!isa<VarDecl>(DRE->getDecl())) {
16376       emitErrorMsg();
16377       return false;
16378     }
16379     assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
16380     RelevantExpr = DRE;
16381     // Record the component.
16382     Components.emplace_back(DRE, DRE->getDecl());
16383     return true;
16384   }
16385 
16386   bool VisitMemberExpr(MemberExpr *ME) {
16387     Expr *E = ME;
16388     Expr *BaseE = ME->getBase()->IgnoreParenCasts();
16389 
16390     if (isa<CXXThisExpr>(BaseE)) {
16391       assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
16392       // We found a base expression: this->Val.
16393       RelevantExpr = ME;
16394     } else {
16395       E = BaseE;
16396     }
16397 
16398     if (!isa<FieldDecl>(ME->getMemberDecl())) {
16399       if (!NoDiagnose) {
16400         SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
16401           << ME->getSourceRange();
16402         return false;
16403       }
16404       if (RelevantExpr)
16405         return false;
16406       return Visit(E);
16407     }
16408 
16409     auto *FD = cast<FieldDecl>(ME->getMemberDecl());
16410 
16411     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
16412     //  A bit-field cannot appear in a map clause.
16413     //
16414     if (FD->isBitField()) {
16415       if (!NoDiagnose) {
16416         SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
16417           << ME->getSourceRange() << getOpenMPClauseName(CKind);
16418         return false;
16419       }
16420       if (RelevantExpr)
16421         return false;
16422       return Visit(E);
16423     }
16424 
16425     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
16426     //  If the type of a list item is a reference to a type T then the type
16427     //  will be considered to be T for all purposes of this clause.
16428     QualType CurType = BaseE->getType().getNonReferenceType();
16429 
16430     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
16431     //  A list item cannot be a variable that is a member of a structure with
16432     //  a union type.
16433     //
16434     if (CurType->isUnionType()) {
16435       if (!NoDiagnose) {
16436         SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
16437           << ME->getSourceRange();
16438         return false;
16439       }
16440       return RelevantExpr || Visit(E);
16441     }
16442 
16443     // If we got a member expression, we should not expect any array section
16444     // before that:
16445     //
16446     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
16447     //  If a list item is an element of a structure, only the rightmost symbol
16448     //  of the variable reference can be an array section.
16449     //
16450     AllowUnitySizeArraySection = false;
16451     AllowWholeSizeArraySection = false;
16452 
16453     // Record the component.
16454     Components.emplace_back(ME, FD);
16455     return RelevantExpr || Visit(E);
16456   }
16457 
16458   bool VisitArraySubscriptExpr(ArraySubscriptExpr *AE) {
16459     Expr *E = AE->getBase()->IgnoreParenImpCasts();
16460 
16461     if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
16462       if (!NoDiagnose) {
16463         SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
16464           << 0 << AE->getSourceRange();
16465         return false;
16466       }
16467       return RelevantExpr || Visit(E);
16468     }
16469 
16470     // If we got an array subscript that express the whole dimension we
16471     // can have any array expressions before. If it only expressing part of
16472     // the dimension, we can only have unitary-size array expressions.
16473     if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, AE,
16474                                                     E->getType()))
16475       AllowWholeSizeArraySection = false;
16476 
16477     if (const auto *TE = dyn_cast<CXXThisExpr>(E->IgnoreParenCasts())) {
16478       Expr::EvalResult Result;
16479       if (!AE->getIdx()->isValueDependent() &&
16480           AE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext()) &&
16481           !Result.Val.getInt().isNullValue()) {
16482         SemaRef.Diag(AE->getIdx()->getExprLoc(),
16483                      diag::err_omp_invalid_map_this_expr);
16484         SemaRef.Diag(AE->getIdx()->getExprLoc(),
16485                      diag::note_omp_invalid_subscript_on_this_ptr_map);
16486       }
16487       assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
16488       RelevantExpr = TE;
16489     }
16490 
16491     // Record the component - we don't have any declaration associated.
16492     Components.emplace_back(AE, nullptr);
16493 
16494     return RelevantExpr || Visit(E);
16495   }
16496 
16497   bool VisitOMPArraySectionExpr(OMPArraySectionExpr *OASE) {
16498     assert(!NoDiagnose && "Array sections cannot be implicitly mapped.");
16499     Expr *E = OASE->getBase()->IgnoreParenImpCasts();
16500     QualType CurType =
16501       OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
16502 
16503     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
16504     //  If the type of a list item is a reference to a type T then the type
16505     //  will be considered to be T for all purposes of this clause.
16506     if (CurType->isReferenceType())
16507       CurType = CurType->getPointeeType();
16508 
16509     bool IsPointer = CurType->isAnyPointerType();
16510 
16511     if (!IsPointer && !CurType->isArrayType()) {
16512       SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
16513         << 0 << OASE->getSourceRange();
16514       return false;
16515     }
16516 
16517     bool NotWhole =
16518       checkArrayExpressionDoesNotReferToWholeSize(SemaRef, OASE, CurType);
16519     bool NotUnity =
16520       checkArrayExpressionDoesNotReferToUnitySize(SemaRef, OASE, CurType);
16521 
16522     if (AllowWholeSizeArraySection) {
16523       // Any array section is currently allowed. Allowing a whole size array
16524       // section implies allowing a unity array section as well.
16525       //
16526       // If this array section refers to the whole dimension we can still
16527       // accept other array sections before this one, except if the base is a
16528       // pointer. Otherwise, only unitary sections are accepted.
16529       if (NotWhole || IsPointer)
16530         AllowWholeSizeArraySection = false;
16531     } else if (AllowUnitySizeArraySection && NotUnity) {
16532       // A unity or whole array section is not allowed and that is not
16533       // compatible with the properties of the current array section.
16534       SemaRef.Diag(
16535         ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
16536         << OASE->getSourceRange();
16537       return false;
16538     }
16539 
16540     if (const auto *TE = dyn_cast<CXXThisExpr>(E)) {
16541       Expr::EvalResult ResultR;
16542       Expr::EvalResult ResultL;
16543       if (!OASE->getLength()->isValueDependent() &&
16544           OASE->getLength()->EvaluateAsInt(ResultR, SemaRef.getASTContext()) &&
16545           !ResultR.Val.getInt().isOneValue()) {
16546         SemaRef.Diag(OASE->getLength()->getExprLoc(),
16547                      diag::err_omp_invalid_map_this_expr);
16548         SemaRef.Diag(OASE->getLength()->getExprLoc(),
16549                      diag::note_omp_invalid_length_on_this_ptr_mapping);
16550       }
16551       if (OASE->getLowerBound() && !OASE->getLowerBound()->isValueDependent() &&
16552           OASE->getLowerBound()->EvaluateAsInt(ResultL,
16553                                                SemaRef.getASTContext()) &&
16554           !ResultL.Val.getInt().isNullValue()) {
16555         SemaRef.Diag(OASE->getLowerBound()->getExprLoc(),
16556                      diag::err_omp_invalid_map_this_expr);
16557         SemaRef.Diag(OASE->getLowerBound()->getExprLoc(),
16558                      diag::note_omp_invalid_lower_bound_on_this_ptr_mapping);
16559       }
16560       assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
16561       RelevantExpr = TE;
16562     }
16563 
16564     // Record the component - we don't have any declaration associated.
16565     Components.emplace_back(OASE, nullptr);
16566     return RelevantExpr || Visit(E);
16567   }
16568   bool VisitOMPArrayShapingExpr(OMPArrayShapingExpr *E) {
16569     Expr *Base = E->getBase();
16570 
16571     // Record the component - we don't have any declaration associated.
16572     Components.emplace_back(E, nullptr);
16573 
16574     return Visit(Base->IgnoreParenImpCasts());
16575   }
16576 
16577   bool VisitUnaryOperator(UnaryOperator *UO) {
16578     if (SemaRef.getLangOpts().OpenMP < 50 || !UO->isLValue() ||
16579         UO->getOpcode() != UO_Deref) {
16580       emitErrorMsg();
16581       return false;
16582     }
16583     if (!RelevantExpr) {
16584       // Record the component if haven't found base decl.
16585       Components.emplace_back(UO, nullptr);
16586     }
16587     return RelevantExpr || Visit(UO->getSubExpr()->IgnoreParenImpCasts());
16588   }
16589   bool VisitBinaryOperator(BinaryOperator *BO) {
16590     if (SemaRef.getLangOpts().OpenMP < 50 || !BO->getType()->isPointerType()) {
16591       emitErrorMsg();
16592       return false;
16593     }
16594 
16595     // Pointer arithmetic is the only thing we expect to happen here so after we
16596     // make sure the binary operator is a pointer type, the we only thing need
16597     // to to is to visit the subtree that has the same type as root (so that we
16598     // know the other subtree is just an offset)
16599     Expr *LE = BO->getLHS()->IgnoreParenImpCasts();
16600     Expr *RE = BO->getRHS()->IgnoreParenImpCasts();
16601     Components.emplace_back(BO, nullptr);
16602     assert((LE->getType().getTypePtr() == BO->getType().getTypePtr() ||
16603             RE->getType().getTypePtr() == BO->getType().getTypePtr()) &&
16604            "Either LHS or RHS have base decl inside");
16605     if (BO->getType().getTypePtr() == LE->getType().getTypePtr())
16606       return RelevantExpr || Visit(LE);
16607     return RelevantExpr || Visit(RE);
16608   }
16609   bool VisitCXXThisExpr(CXXThisExpr *CTE) {
16610     assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
16611     RelevantExpr = CTE;
16612     Components.emplace_back(CTE, nullptr);
16613     return true;
16614   }
16615   bool VisitStmt(Stmt *) {
16616     emitErrorMsg();
16617     return false;
16618   }
16619   const Expr *getFoundBase() const {
16620     return RelevantExpr;
16621   }
16622   explicit MapBaseChecker(
16623       Sema &SemaRef, OpenMPClauseKind CKind,
16624       OMPClauseMappableExprCommon::MappableExprComponentList &Components,
16625       bool NoDiagnose, SourceLocation &ELoc, SourceRange &ERange)
16626       : SemaRef(SemaRef), CKind(CKind), Components(Components),
16627         NoDiagnose(NoDiagnose), ELoc(ELoc), ERange(ERange) {}
16628 };
16629 } // namespace
16630 
16631 /// Return the expression of the base of the mappable expression or null if it
16632 /// cannot be determined and do all the necessary checks to see if the expression
16633 /// is valid as a standalone mappable expression. In the process, record all the
16634 /// components of the expression.
16635 static const Expr *checkMapClauseExpressionBase(
16636     Sema &SemaRef, Expr *E,
16637     OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
16638     OpenMPClauseKind CKind, bool NoDiagnose) {
16639   SourceLocation ELoc = E->getExprLoc();
16640   SourceRange ERange = E->getSourceRange();
16641   MapBaseChecker Checker(SemaRef, CKind, CurComponents, NoDiagnose, ELoc,
16642                          ERange);
16643   if (Checker.Visit(E->IgnoreParens()))
16644     return Checker.getFoundBase();
16645   return nullptr;
16646 }
16647 
16648 // Return true if expression E associated with value VD has conflicts with other
16649 // map information.
16650 static bool checkMapConflicts(
16651     Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
16652     bool CurrentRegionOnly,
16653     OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
16654     OpenMPClauseKind CKind) {
16655   assert(VD && E);
16656   SourceLocation ELoc = E->getExprLoc();
16657   SourceRange ERange = E->getSourceRange();
16658 
16659   // In order to easily check the conflicts we need to match each component of
16660   // the expression under test with the components of the expressions that are
16661   // already in the stack.
16662 
16663   assert(!CurComponents.empty() && "Map clause expression with no components!");
16664   assert(CurComponents.back().getAssociatedDeclaration() == VD &&
16665          "Map clause expression with unexpected base!");
16666 
16667   // Variables to help detecting enclosing problems in data environment nests.
16668   bool IsEnclosedByDataEnvironmentExpr = false;
16669   const Expr *EnclosingExpr = nullptr;
16670 
16671   bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
16672       VD, CurrentRegionOnly,
16673       [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
16674        ERange, CKind, &EnclosingExpr,
16675        CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
16676                           StackComponents,
16677                       OpenMPClauseKind) {
16678         assert(!StackComponents.empty() &&
16679                "Map clause expression with no components!");
16680         assert(StackComponents.back().getAssociatedDeclaration() == VD &&
16681                "Map clause expression with unexpected base!");
16682         (void)VD;
16683 
16684         // The whole expression in the stack.
16685         const Expr *RE = StackComponents.front().getAssociatedExpression();
16686 
16687         // Expressions must start from the same base. Here we detect at which
16688         // point both expressions diverge from each other and see if we can
16689         // detect if the memory referred to both expressions is contiguous and
16690         // do not overlap.
16691         auto CI = CurComponents.rbegin();
16692         auto CE = CurComponents.rend();
16693         auto SI = StackComponents.rbegin();
16694         auto SE = StackComponents.rend();
16695         for (; CI != CE && SI != SE; ++CI, ++SI) {
16696 
16697           // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
16698           //  At most one list item can be an array item derived from a given
16699           //  variable in map clauses of the same construct.
16700           if (CurrentRegionOnly &&
16701               (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
16702                isa<OMPArraySectionExpr>(CI->getAssociatedExpression()) ||
16703                isa<OMPArrayShapingExpr>(CI->getAssociatedExpression())) &&
16704               (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
16705                isa<OMPArraySectionExpr>(SI->getAssociatedExpression()) ||
16706                isa<OMPArrayShapingExpr>(SI->getAssociatedExpression()))) {
16707             SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
16708                          diag::err_omp_multiple_array_items_in_map_clause)
16709                 << CI->getAssociatedExpression()->getSourceRange();
16710             SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
16711                          diag::note_used_here)
16712                 << SI->getAssociatedExpression()->getSourceRange();
16713             return true;
16714           }
16715 
16716           // Do both expressions have the same kind?
16717           if (CI->getAssociatedExpression()->getStmtClass() !=
16718               SI->getAssociatedExpression()->getStmtClass())
16719             break;
16720 
16721           // Are we dealing with different variables/fields?
16722           if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
16723             break;
16724         }
16725         // Check if the extra components of the expressions in the enclosing
16726         // data environment are redundant for the current base declaration.
16727         // If they are, the maps completely overlap, which is legal.
16728         for (; SI != SE; ++SI) {
16729           QualType Type;
16730           if (const auto *ASE =
16731                   dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
16732             Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
16733           } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(
16734                          SI->getAssociatedExpression())) {
16735             const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
16736             Type =
16737                 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
16738           } else if (const auto *OASE = dyn_cast<OMPArrayShapingExpr>(
16739                          SI->getAssociatedExpression())) {
16740             Type = OASE->getBase()->getType()->getPointeeType();
16741           }
16742           if (Type.isNull() || Type->isAnyPointerType() ||
16743               checkArrayExpressionDoesNotReferToWholeSize(
16744                   SemaRef, SI->getAssociatedExpression(), Type))
16745             break;
16746         }
16747 
16748         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
16749         //  List items of map clauses in the same construct must not share
16750         //  original storage.
16751         //
16752         // If the expressions are exactly the same or one is a subset of the
16753         // other, it means they are sharing storage.
16754         if (CI == CE && SI == SE) {
16755           if (CurrentRegionOnly) {
16756             if (CKind == OMPC_map) {
16757               SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
16758             } else {
16759               assert(CKind == OMPC_to || CKind == OMPC_from);
16760               SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
16761                   << ERange;
16762             }
16763             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
16764                 << RE->getSourceRange();
16765             return true;
16766           }
16767           // If we find the same expression in the enclosing data environment,
16768           // that is legal.
16769           IsEnclosedByDataEnvironmentExpr = true;
16770           return false;
16771         }
16772 
16773         QualType DerivedType =
16774             std::prev(CI)->getAssociatedDeclaration()->getType();
16775         SourceLocation DerivedLoc =
16776             std::prev(CI)->getAssociatedExpression()->getExprLoc();
16777 
16778         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
16779         //  If the type of a list item is a reference to a type T then the type
16780         //  will be considered to be T for all purposes of this clause.
16781         DerivedType = DerivedType.getNonReferenceType();
16782 
16783         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
16784         //  A variable for which the type is pointer and an array section
16785         //  derived from that variable must not appear as list items of map
16786         //  clauses of the same construct.
16787         //
16788         // Also, cover one of the cases in:
16789         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
16790         //  If any part of the original storage of a list item has corresponding
16791         //  storage in the device data environment, all of the original storage
16792         //  must have corresponding storage in the device data environment.
16793         //
16794         if (DerivedType->isAnyPointerType()) {
16795           if (CI == CE || SI == SE) {
16796             SemaRef.Diag(
16797                 DerivedLoc,
16798                 diag::err_omp_pointer_mapped_along_with_derived_section)
16799                 << DerivedLoc;
16800             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
16801                 << RE->getSourceRange();
16802             return true;
16803           }
16804           if (CI->getAssociatedExpression()->getStmtClass() !=
16805                          SI->getAssociatedExpression()->getStmtClass() ||
16806                      CI->getAssociatedDeclaration()->getCanonicalDecl() ==
16807                          SI->getAssociatedDeclaration()->getCanonicalDecl()) {
16808             assert(CI != CE && SI != SE);
16809             SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced)
16810                 << DerivedLoc;
16811             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
16812                 << RE->getSourceRange();
16813             return true;
16814           }
16815         }
16816 
16817         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
16818         //  List items of map clauses in the same construct must not share
16819         //  original storage.
16820         //
16821         // An expression is a subset of the other.
16822         if (CurrentRegionOnly && (CI == CE || SI == SE)) {
16823           if (CKind == OMPC_map) {
16824             if (CI != CE || SI != SE) {
16825               // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is
16826               // a pointer.
16827               auto Begin =
16828                   CI != CE ? CurComponents.begin() : StackComponents.begin();
16829               auto End = CI != CE ? CurComponents.end() : StackComponents.end();
16830               auto It = Begin;
16831               while (It != End && !It->getAssociatedDeclaration())
16832                 std::advance(It, 1);
16833               assert(It != End &&
16834                      "Expected at least one component with the declaration.");
16835               if (It != Begin && It->getAssociatedDeclaration()
16836                                      ->getType()
16837                                      .getCanonicalType()
16838                                      ->isAnyPointerType()) {
16839                 IsEnclosedByDataEnvironmentExpr = false;
16840                 EnclosingExpr = nullptr;
16841                 return false;
16842               }
16843             }
16844             SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
16845           } else {
16846             assert(CKind == OMPC_to || CKind == OMPC_from);
16847             SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
16848                 << ERange;
16849           }
16850           SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
16851               << RE->getSourceRange();
16852           return true;
16853         }
16854 
16855         // The current expression uses the same base as other expression in the
16856         // data environment but does not contain it completely.
16857         if (!CurrentRegionOnly && SI != SE)
16858           EnclosingExpr = RE;
16859 
16860         // The current expression is a subset of the expression in the data
16861         // environment.
16862         IsEnclosedByDataEnvironmentExpr |=
16863             (!CurrentRegionOnly && CI != CE && SI == SE);
16864 
16865         return false;
16866       });
16867 
16868   if (CurrentRegionOnly)
16869     return FoundError;
16870 
16871   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
16872   //  If any part of the original storage of a list item has corresponding
16873   //  storage in the device data environment, all of the original storage must
16874   //  have corresponding storage in the device data environment.
16875   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
16876   //  If a list item is an element of a structure, and a different element of
16877   //  the structure has a corresponding list item in the device data environment
16878   //  prior to a task encountering the construct associated with the map clause,
16879   //  then the list item must also have a corresponding list item in the device
16880   //  data environment prior to the task encountering the construct.
16881   //
16882   if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
16883     SemaRef.Diag(ELoc,
16884                  diag::err_omp_original_storage_is_shared_and_does_not_contain)
16885         << ERange;
16886     SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
16887         << EnclosingExpr->getSourceRange();
16888     return true;
16889   }
16890 
16891   return FoundError;
16892 }
16893 
16894 // Look up the user-defined mapper given the mapper name and mapped type, and
16895 // build a reference to it.
16896 static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S,
16897                                             CXXScopeSpec &MapperIdScopeSpec,
16898                                             const DeclarationNameInfo &MapperId,
16899                                             QualType Type,
16900                                             Expr *UnresolvedMapper) {
16901   if (MapperIdScopeSpec.isInvalid())
16902     return ExprError();
16903   // Get the actual type for the array type.
16904   if (Type->isArrayType()) {
16905     assert(Type->getAsArrayTypeUnsafe() && "Expect to get a valid array type");
16906     Type = Type->getAsArrayTypeUnsafe()->getElementType().getCanonicalType();
16907   }
16908   // Find all user-defined mappers with the given MapperId.
16909   SmallVector<UnresolvedSet<8>, 4> Lookups;
16910   LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName);
16911   Lookup.suppressDiagnostics();
16912   if (S) {
16913     while (S && SemaRef.LookupParsedName(Lookup, S, &MapperIdScopeSpec)) {
16914       NamedDecl *D = Lookup.getRepresentativeDecl();
16915       while (S && !S->isDeclScope(D))
16916         S = S->getParent();
16917       if (S)
16918         S = S->getParent();
16919       Lookups.emplace_back();
16920       Lookups.back().append(Lookup.begin(), Lookup.end());
16921       Lookup.clear();
16922     }
16923   } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(UnresolvedMapper)) {
16924     // Extract the user-defined mappers with the given MapperId.
16925     Lookups.push_back(UnresolvedSet<8>());
16926     for (NamedDecl *D : ULE->decls()) {
16927       auto *DMD = cast<OMPDeclareMapperDecl>(D);
16928       assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation.");
16929       Lookups.back().addDecl(DMD);
16930     }
16931   }
16932   // Defer the lookup for dependent types. The results will be passed through
16933   // UnresolvedMapper on instantiation.
16934   if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() ||
16935       Type->isInstantiationDependentType() ||
16936       Type->containsUnexpandedParameterPack() ||
16937       filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) {
16938         return !D->isInvalidDecl() &&
16939                (D->getType()->isDependentType() ||
16940                 D->getType()->isInstantiationDependentType() ||
16941                 D->getType()->containsUnexpandedParameterPack());
16942       })) {
16943     UnresolvedSet<8> URS;
16944     for (const UnresolvedSet<8> &Set : Lookups) {
16945       if (Set.empty())
16946         continue;
16947       URS.append(Set.begin(), Set.end());
16948     }
16949     return UnresolvedLookupExpr::Create(
16950         SemaRef.Context, /*NamingClass=*/nullptr,
16951         MapperIdScopeSpec.getWithLocInContext(SemaRef.Context), MapperId,
16952         /*ADL=*/false, /*Overloaded=*/true, URS.begin(), URS.end());
16953   }
16954   SourceLocation Loc = MapperId.getLoc();
16955   // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
16956   //  The type must be of struct, union or class type in C and C++
16957   if (!Type->isStructureOrClassType() && !Type->isUnionType() &&
16958       (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default")) {
16959     SemaRef.Diag(Loc, diag::err_omp_mapper_wrong_type);
16960     return ExprError();
16961   }
16962   // Perform argument dependent lookup.
16963   if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet())
16964     argumentDependentLookup(SemaRef, MapperId, Loc, Type, Lookups);
16965   // Return the first user-defined mapper with the desired type.
16966   if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
16967           Lookups, [&SemaRef, Type](ValueDecl *D) -> ValueDecl * {
16968             if (!D->isInvalidDecl() &&
16969                 SemaRef.Context.hasSameType(D->getType(), Type))
16970               return D;
16971             return nullptr;
16972           }))
16973     return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc);
16974   // Find the first user-defined mapper with a type derived from the desired
16975   // type.
16976   if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
16977           Lookups, [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * {
16978             if (!D->isInvalidDecl() &&
16979                 SemaRef.IsDerivedFrom(Loc, Type, D->getType()) &&
16980                 !Type.isMoreQualifiedThan(D->getType()))
16981               return D;
16982             return nullptr;
16983           })) {
16984     CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
16985                        /*DetectVirtual=*/false);
16986     if (SemaRef.IsDerivedFrom(Loc, Type, VD->getType(), Paths)) {
16987       if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
16988               VD->getType().getUnqualifiedType()))) {
16989         if (SemaRef.CheckBaseClassAccess(
16990                 Loc, VD->getType(), Type, Paths.front(),
16991                 /*DiagID=*/0) != Sema::AR_inaccessible) {
16992           return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc);
16993         }
16994       }
16995     }
16996   }
16997   // Report error if a mapper is specified, but cannot be found.
16998   if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") {
16999     SemaRef.Diag(Loc, diag::err_omp_invalid_mapper)
17000         << Type << MapperId.getName();
17001     return ExprError();
17002   }
17003   return ExprEmpty();
17004 }
17005 
17006 namespace {
17007 // Utility struct that gathers all the related lists associated with a mappable
17008 // expression.
17009 struct MappableVarListInfo {
17010   // The list of expressions.
17011   ArrayRef<Expr *> VarList;
17012   // The list of processed expressions.
17013   SmallVector<Expr *, 16> ProcessedVarList;
17014   // The mappble components for each expression.
17015   OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
17016   // The base declaration of the variable.
17017   SmallVector<ValueDecl *, 16> VarBaseDeclarations;
17018   // The reference to the user-defined mapper associated with every expression.
17019   SmallVector<Expr *, 16> UDMapperList;
17020 
17021   MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
17022     // We have a list of components and base declarations for each entry in the
17023     // variable list.
17024     VarComponents.reserve(VarList.size());
17025     VarBaseDeclarations.reserve(VarList.size());
17026   }
17027 };
17028 }
17029 
17030 // Check the validity of the provided variable list for the provided clause kind
17031 // \a CKind. In the check process the valid expressions, mappable expression
17032 // components, variables, and user-defined mappers are extracted and used to
17033 // fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a
17034 // UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec,
17035 // and \a MapperId are expected to be valid if the clause kind is 'map'.
17036 static void checkMappableExpressionList(
17037     Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind,
17038     MappableVarListInfo &MVLI, SourceLocation StartLoc,
17039     CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId,
17040     ArrayRef<Expr *> UnresolvedMappers,
17041     OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
17042     bool IsMapTypeImplicit = false) {
17043   // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
17044   assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
17045          "Unexpected clause kind with mappable expressions!");
17046 
17047   // If the identifier of user-defined mapper is not specified, it is "default".
17048   // We do not change the actual name in this clause to distinguish whether a
17049   // mapper is specified explicitly, i.e., it is not explicitly specified when
17050   // MapperId.getName() is empty.
17051   if (!MapperId.getName() || MapperId.getName().isEmpty()) {
17052     auto &DeclNames = SemaRef.getASTContext().DeclarationNames;
17053     MapperId.setName(DeclNames.getIdentifier(
17054         &SemaRef.getASTContext().Idents.get("default")));
17055   }
17056 
17057   // Iterators to find the current unresolved mapper expression.
17058   auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end();
17059   bool UpdateUMIt = false;
17060   Expr *UnresolvedMapper = nullptr;
17061 
17062   // Keep track of the mappable components and base declarations in this clause.
17063   // Each entry in the list is going to have a list of components associated. We
17064   // record each set of the components so that we can build the clause later on.
17065   // In the end we should have the same amount of declarations and component
17066   // lists.
17067 
17068   for (Expr *RE : MVLI.VarList) {
17069     assert(RE && "Null expr in omp to/from/map clause");
17070     SourceLocation ELoc = RE->getExprLoc();
17071 
17072     // Find the current unresolved mapper expression.
17073     if (UpdateUMIt && UMIt != UMEnd) {
17074       UMIt++;
17075       assert(
17076           UMIt != UMEnd &&
17077           "Expect the size of UnresolvedMappers to match with that of VarList");
17078     }
17079     UpdateUMIt = true;
17080     if (UMIt != UMEnd)
17081       UnresolvedMapper = *UMIt;
17082 
17083     const Expr *VE = RE->IgnoreParenLValueCasts();
17084 
17085     if (VE->isValueDependent() || VE->isTypeDependent() ||
17086         VE->isInstantiationDependent() ||
17087         VE->containsUnexpandedParameterPack()) {
17088       // Try to find the associated user-defined mapper.
17089       ExprResult ER = buildUserDefinedMapperRef(
17090           SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
17091           VE->getType().getCanonicalType(), UnresolvedMapper);
17092       if (ER.isInvalid())
17093         continue;
17094       MVLI.UDMapperList.push_back(ER.get());
17095       // We can only analyze this information once the missing information is
17096       // resolved.
17097       MVLI.ProcessedVarList.push_back(RE);
17098       continue;
17099     }
17100 
17101     Expr *SimpleExpr = RE->IgnoreParenCasts();
17102 
17103     if (!RE->isLValue()) {
17104       if (SemaRef.getLangOpts().OpenMP < 50) {
17105         SemaRef.Diag(
17106             ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
17107             << RE->getSourceRange();
17108       } else {
17109         SemaRef.Diag(ELoc, diag::err_omp_non_lvalue_in_map_or_motion_clauses)
17110             << getOpenMPClauseName(CKind) << RE->getSourceRange();
17111       }
17112       continue;
17113     }
17114 
17115     OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
17116     ValueDecl *CurDeclaration = nullptr;
17117 
17118     // Obtain the array or member expression bases if required. Also, fill the
17119     // components array with all the components identified in the process.
17120     const Expr *BE = checkMapClauseExpressionBase(
17121         SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false);
17122     if (!BE)
17123       continue;
17124 
17125     assert(!CurComponents.empty() &&
17126            "Invalid mappable expression information.");
17127 
17128     if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) {
17129       // Add store "this" pointer to class in DSAStackTy for future checking
17130       DSAS->addMappedClassesQualTypes(TE->getType());
17131       // Try to find the associated user-defined mapper.
17132       ExprResult ER = buildUserDefinedMapperRef(
17133           SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
17134           VE->getType().getCanonicalType(), UnresolvedMapper);
17135       if (ER.isInvalid())
17136         continue;
17137       MVLI.UDMapperList.push_back(ER.get());
17138       // Skip restriction checking for variable or field declarations
17139       MVLI.ProcessedVarList.push_back(RE);
17140       MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
17141       MVLI.VarComponents.back().append(CurComponents.begin(),
17142                                        CurComponents.end());
17143       MVLI.VarBaseDeclarations.push_back(nullptr);
17144       continue;
17145     }
17146 
17147     // For the following checks, we rely on the base declaration which is
17148     // expected to be associated with the last component. The declaration is
17149     // expected to be a variable or a field (if 'this' is being mapped).
17150     CurDeclaration = CurComponents.back().getAssociatedDeclaration();
17151     assert(CurDeclaration && "Null decl on map clause.");
17152     assert(
17153         CurDeclaration->isCanonicalDecl() &&
17154         "Expecting components to have associated only canonical declarations.");
17155 
17156     auto *VD = dyn_cast<VarDecl>(CurDeclaration);
17157     const auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
17158 
17159     assert((VD || FD) && "Only variables or fields are expected here!");
17160     (void)FD;
17161 
17162     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
17163     // threadprivate variables cannot appear in a map clause.
17164     // OpenMP 4.5 [2.10.5, target update Construct]
17165     // threadprivate variables cannot appear in a from clause.
17166     if (VD && DSAS->isThreadPrivate(VD)) {
17167       DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
17168       SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
17169           << getOpenMPClauseName(CKind);
17170       reportOriginalDsa(SemaRef, DSAS, VD, DVar);
17171       continue;
17172     }
17173 
17174     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
17175     //  A list item cannot appear in both a map clause and a data-sharing
17176     //  attribute clause on the same construct.
17177 
17178     // Check conflicts with other map clause expressions. We check the conflicts
17179     // with the current construct separately from the enclosing data
17180     // environment, because the restrictions are different. We only have to
17181     // check conflicts across regions for the map clauses.
17182     if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
17183                           /*CurrentRegionOnly=*/true, CurComponents, CKind))
17184       break;
17185     if (CKind == OMPC_map &&
17186         checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
17187                           /*CurrentRegionOnly=*/false, CurComponents, CKind))
17188       break;
17189 
17190     // OpenMP 4.5 [2.10.5, target update Construct]
17191     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
17192     //  If the type of a list item is a reference to a type T then the type will
17193     //  be considered to be T for all purposes of this clause.
17194     auto I = llvm::find_if(
17195         CurComponents,
17196         [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
17197           return MC.getAssociatedDeclaration();
17198         });
17199     assert(I != CurComponents.end() && "Null decl on map clause.");
17200     QualType Type;
17201     auto *ASE = dyn_cast<ArraySubscriptExpr>(VE->IgnoreParens());
17202     auto *OASE = dyn_cast<OMPArraySectionExpr>(VE->IgnoreParens());
17203     auto *OAShE = dyn_cast<OMPArrayShapingExpr>(VE->IgnoreParens());
17204     if (ASE) {
17205       Type = ASE->getType().getNonReferenceType();
17206     } else if (OASE) {
17207       QualType BaseType =
17208           OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
17209       if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
17210         Type = ATy->getElementType();
17211       else
17212         Type = BaseType->getPointeeType();
17213       Type = Type.getNonReferenceType();
17214     } else if (OAShE) {
17215       Type = OAShE->getBase()->getType()->getPointeeType();
17216     } else {
17217       Type = VE->getType();
17218     }
17219 
17220     // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
17221     // A list item in a to or from clause must have a mappable type.
17222     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
17223     //  A list item must have a mappable type.
17224     if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
17225                            DSAS, Type))
17226       continue;
17227 
17228     Type = I->getAssociatedDeclaration()->getType().getNonReferenceType();
17229 
17230     if (CKind == OMPC_map) {
17231       // target enter data
17232       // OpenMP [2.10.2, Restrictions, p. 99]
17233       // A map-type must be specified in all map clauses and must be either
17234       // to or alloc.
17235       OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
17236       if (DKind == OMPD_target_enter_data &&
17237           !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
17238         SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
17239             << (IsMapTypeImplicit ? 1 : 0)
17240             << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
17241             << getOpenMPDirectiveName(DKind);
17242         continue;
17243       }
17244 
17245       // target exit_data
17246       // OpenMP [2.10.3, Restrictions, p. 102]
17247       // A map-type must be specified in all map clauses and must be either
17248       // from, release, or delete.
17249       if (DKind == OMPD_target_exit_data &&
17250           !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
17251             MapType == OMPC_MAP_delete)) {
17252         SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
17253             << (IsMapTypeImplicit ? 1 : 0)
17254             << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
17255             << getOpenMPDirectiveName(DKind);
17256         continue;
17257       }
17258 
17259       // target, target data
17260       // OpenMP 5.0 [2.12.2, Restrictions, p. 163]
17261       // OpenMP 5.0 [2.12.5, Restrictions, p. 174]
17262       // A map-type in a map clause must be to, from, tofrom or alloc
17263       if ((DKind == OMPD_target_data ||
17264            isOpenMPTargetExecutionDirective(DKind)) &&
17265           !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_from ||
17266             MapType == OMPC_MAP_tofrom || MapType == OMPC_MAP_alloc)) {
17267         SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
17268             << (IsMapTypeImplicit ? 1 : 0)
17269             << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
17270             << getOpenMPDirectiveName(DKind);
17271         continue;
17272       }
17273 
17274       // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
17275       // A list item cannot appear in both a map clause and a data-sharing
17276       // attribute clause on the same construct
17277       //
17278       // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
17279       // A list item cannot appear in both a map clause and a data-sharing
17280       // attribute clause on the same construct unless the construct is a
17281       // combined construct.
17282       if (VD && ((SemaRef.LangOpts.OpenMP <= 45 &&
17283                   isOpenMPTargetExecutionDirective(DKind)) ||
17284                  DKind == OMPD_target)) {
17285         DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
17286         if (isOpenMPPrivate(DVar.CKind)) {
17287           SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
17288               << getOpenMPClauseName(DVar.CKind)
17289               << getOpenMPClauseName(OMPC_map)
17290               << getOpenMPDirectiveName(DSAS->getCurrentDirective());
17291           reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar);
17292           continue;
17293         }
17294       }
17295     }
17296 
17297     // Try to find the associated user-defined mapper.
17298     ExprResult ER = buildUserDefinedMapperRef(
17299         SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
17300         Type.getCanonicalType(), UnresolvedMapper);
17301     if (ER.isInvalid())
17302       continue;
17303     MVLI.UDMapperList.push_back(ER.get());
17304 
17305     // Save the current expression.
17306     MVLI.ProcessedVarList.push_back(RE);
17307 
17308     // Store the components in the stack so that they can be used to check
17309     // against other clauses later on.
17310     DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
17311                                           /*WhereFoundClauseKind=*/OMPC_map);
17312 
17313     // Save the components and declaration to create the clause. For purposes of
17314     // the clause creation, any component list that has has base 'this' uses
17315     // null as base declaration.
17316     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
17317     MVLI.VarComponents.back().append(CurComponents.begin(),
17318                                      CurComponents.end());
17319     MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
17320                                                            : CurDeclaration);
17321   }
17322 }
17323 
17324 OMPClause *Sema::ActOnOpenMPMapClause(
17325     ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
17326     ArrayRef<SourceLocation> MapTypeModifiersLoc,
17327     CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId,
17328     OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc,
17329     SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
17330     const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) {
17331   OpenMPMapModifierKind Modifiers[] = {OMPC_MAP_MODIFIER_unknown,
17332                                        OMPC_MAP_MODIFIER_unknown,
17333                                        OMPC_MAP_MODIFIER_unknown};
17334   SourceLocation ModifiersLoc[NumberOfOMPMapClauseModifiers];
17335 
17336   // Process map-type-modifiers, flag errors for duplicate modifiers.
17337   unsigned Count = 0;
17338   for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) {
17339     if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown &&
17340         llvm::find(Modifiers, MapTypeModifiers[I]) != std::end(Modifiers)) {
17341       Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier);
17342       continue;
17343     }
17344     assert(Count < NumberOfOMPMapClauseModifiers &&
17345            "Modifiers exceed the allowed number of map type modifiers");
17346     Modifiers[Count] = MapTypeModifiers[I];
17347     ModifiersLoc[Count] = MapTypeModifiersLoc[I];
17348     ++Count;
17349   }
17350 
17351   MappableVarListInfo MVLI(VarList);
17352   checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, Locs.StartLoc,
17353                               MapperIdScopeSpec, MapperId, UnresolvedMappers,
17354                               MapType, IsMapTypeImplicit);
17355 
17356   // We need to produce a map clause even if we don't have variables so that
17357   // other diagnostics related with non-existing map clauses are accurate.
17358   return OMPMapClause::Create(Context, Locs, MVLI.ProcessedVarList,
17359                               MVLI.VarBaseDeclarations, MVLI.VarComponents,
17360                               MVLI.UDMapperList, Modifiers, ModifiersLoc,
17361                               MapperIdScopeSpec.getWithLocInContext(Context),
17362                               MapperId, MapType, IsMapTypeImplicit, MapLoc);
17363 }
17364 
17365 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
17366                                                TypeResult ParsedType) {
17367   assert(ParsedType.isUsable());
17368 
17369   QualType ReductionType = GetTypeFromParser(ParsedType.get());
17370   if (ReductionType.isNull())
17371     return QualType();
17372 
17373   // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
17374   // A type name in a declare reduction directive cannot be a function type, an
17375   // array type, a reference type, or a type qualified with const, volatile or
17376   // restrict.
17377   if (ReductionType.hasQualifiers()) {
17378     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
17379     return QualType();
17380   }
17381 
17382   if (ReductionType->isFunctionType()) {
17383     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
17384     return QualType();
17385   }
17386   if (ReductionType->isReferenceType()) {
17387     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
17388     return QualType();
17389   }
17390   if (ReductionType->isArrayType()) {
17391     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
17392     return QualType();
17393   }
17394   return ReductionType;
17395 }
17396 
17397 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
17398     Scope *S, DeclContext *DC, DeclarationName Name,
17399     ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
17400     AccessSpecifier AS, Decl *PrevDeclInScope) {
17401   SmallVector<Decl *, 8> Decls;
17402   Decls.reserve(ReductionTypes.size());
17403 
17404   LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
17405                       forRedeclarationInCurContext());
17406   // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
17407   // A reduction-identifier may not be re-declared in the current scope for the
17408   // same type or for a type that is compatible according to the base language
17409   // rules.
17410   llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
17411   OMPDeclareReductionDecl *PrevDRD = nullptr;
17412   bool InCompoundScope = true;
17413   if (S != nullptr) {
17414     // Find previous declaration with the same name not referenced in other
17415     // declarations.
17416     FunctionScopeInfo *ParentFn = getEnclosingFunction();
17417     InCompoundScope =
17418         (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
17419     LookupName(Lookup, S);
17420     FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
17421                          /*AllowInlineNamespace=*/false);
17422     llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
17423     LookupResult::Filter Filter = Lookup.makeFilter();
17424     while (Filter.hasNext()) {
17425       auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
17426       if (InCompoundScope) {
17427         auto I = UsedAsPrevious.find(PrevDecl);
17428         if (I == UsedAsPrevious.end())
17429           UsedAsPrevious[PrevDecl] = false;
17430         if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
17431           UsedAsPrevious[D] = true;
17432       }
17433       PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
17434           PrevDecl->getLocation();
17435     }
17436     Filter.done();
17437     if (InCompoundScope) {
17438       for (const auto &PrevData : UsedAsPrevious) {
17439         if (!PrevData.second) {
17440           PrevDRD = PrevData.first;
17441           break;
17442         }
17443       }
17444     }
17445   } else if (PrevDeclInScope != nullptr) {
17446     auto *PrevDRDInScope = PrevDRD =
17447         cast<OMPDeclareReductionDecl>(PrevDeclInScope);
17448     do {
17449       PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
17450           PrevDRDInScope->getLocation();
17451       PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
17452     } while (PrevDRDInScope != nullptr);
17453   }
17454   for (const auto &TyData : ReductionTypes) {
17455     const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
17456     bool Invalid = false;
17457     if (I != PreviousRedeclTypes.end()) {
17458       Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
17459           << TyData.first;
17460       Diag(I->second, diag::note_previous_definition);
17461       Invalid = true;
17462     }
17463     PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
17464     auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
17465                                                 Name, TyData.first, PrevDRD);
17466     DC->addDecl(DRD);
17467     DRD->setAccess(AS);
17468     Decls.push_back(DRD);
17469     if (Invalid)
17470       DRD->setInvalidDecl();
17471     else
17472       PrevDRD = DRD;
17473   }
17474 
17475   return DeclGroupPtrTy::make(
17476       DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
17477 }
17478 
17479 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
17480   auto *DRD = cast<OMPDeclareReductionDecl>(D);
17481 
17482   // Enter new function scope.
17483   PushFunctionScope();
17484   setFunctionHasBranchProtectedScope();
17485   getCurFunction()->setHasOMPDeclareReductionCombiner();
17486 
17487   if (S != nullptr)
17488     PushDeclContext(S, DRD);
17489   else
17490     CurContext = DRD;
17491 
17492   PushExpressionEvaluationContext(
17493       ExpressionEvaluationContext::PotentiallyEvaluated);
17494 
17495   QualType ReductionType = DRD->getType();
17496   // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
17497   // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
17498   // uses semantics of argument handles by value, but it should be passed by
17499   // reference. C lang does not support references, so pass all parameters as
17500   // pointers.
17501   // Create 'T omp_in;' variable.
17502   VarDecl *OmpInParm =
17503       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
17504   // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
17505   // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
17506   // uses semantics of argument handles by value, but it should be passed by
17507   // reference. C lang does not support references, so pass all parameters as
17508   // pointers.
17509   // Create 'T omp_out;' variable.
17510   VarDecl *OmpOutParm =
17511       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
17512   if (S != nullptr) {
17513     PushOnScopeChains(OmpInParm, S);
17514     PushOnScopeChains(OmpOutParm, S);
17515   } else {
17516     DRD->addDecl(OmpInParm);
17517     DRD->addDecl(OmpOutParm);
17518   }
17519   Expr *InE =
17520       ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation());
17521   Expr *OutE =
17522       ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation());
17523   DRD->setCombinerData(InE, OutE);
17524 }
17525 
17526 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
17527   auto *DRD = cast<OMPDeclareReductionDecl>(D);
17528   DiscardCleanupsInEvaluationContext();
17529   PopExpressionEvaluationContext();
17530 
17531   PopDeclContext();
17532   PopFunctionScopeInfo();
17533 
17534   if (Combiner != nullptr)
17535     DRD->setCombiner(Combiner);
17536   else
17537     DRD->setInvalidDecl();
17538 }
17539 
17540 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
17541   auto *DRD = cast<OMPDeclareReductionDecl>(D);
17542 
17543   // Enter new function scope.
17544   PushFunctionScope();
17545   setFunctionHasBranchProtectedScope();
17546 
17547   if (S != nullptr)
17548     PushDeclContext(S, DRD);
17549   else
17550     CurContext = DRD;
17551 
17552   PushExpressionEvaluationContext(
17553       ExpressionEvaluationContext::PotentiallyEvaluated);
17554 
17555   QualType ReductionType = DRD->getType();
17556   // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
17557   // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
17558   // uses semantics of argument handles by value, but it should be passed by
17559   // reference. C lang does not support references, so pass all parameters as
17560   // pointers.
17561   // Create 'T omp_priv;' variable.
17562   VarDecl *OmpPrivParm =
17563       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
17564   // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
17565   // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
17566   // uses semantics of argument handles by value, but it should be passed by
17567   // reference. C lang does not support references, so pass all parameters as
17568   // pointers.
17569   // Create 'T omp_orig;' variable.
17570   VarDecl *OmpOrigParm =
17571       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
17572   if (S != nullptr) {
17573     PushOnScopeChains(OmpPrivParm, S);
17574     PushOnScopeChains(OmpOrigParm, S);
17575   } else {
17576     DRD->addDecl(OmpPrivParm);
17577     DRD->addDecl(OmpOrigParm);
17578   }
17579   Expr *OrigE =
17580       ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation());
17581   Expr *PrivE =
17582       ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation());
17583   DRD->setInitializerData(OrigE, PrivE);
17584   return OmpPrivParm;
17585 }
17586 
17587 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
17588                                                      VarDecl *OmpPrivParm) {
17589   auto *DRD = cast<OMPDeclareReductionDecl>(D);
17590   DiscardCleanupsInEvaluationContext();
17591   PopExpressionEvaluationContext();
17592 
17593   PopDeclContext();
17594   PopFunctionScopeInfo();
17595 
17596   if (Initializer != nullptr) {
17597     DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
17598   } else if (OmpPrivParm->hasInit()) {
17599     DRD->setInitializer(OmpPrivParm->getInit(),
17600                         OmpPrivParm->isDirectInit()
17601                             ? OMPDeclareReductionDecl::DirectInit
17602                             : OMPDeclareReductionDecl::CopyInit);
17603   } else {
17604     DRD->setInvalidDecl();
17605   }
17606 }
17607 
17608 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
17609     Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
17610   for (Decl *D : DeclReductions.get()) {
17611     if (IsValid) {
17612       if (S)
17613         PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S,
17614                           /*AddToContext=*/false);
17615     } else {
17616       D->setInvalidDecl();
17617     }
17618   }
17619   return DeclReductions;
17620 }
17621 
17622 TypeResult Sema::ActOnOpenMPDeclareMapperVarDecl(Scope *S, Declarator &D) {
17623   TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
17624   QualType T = TInfo->getType();
17625   if (D.isInvalidType())
17626     return true;
17627 
17628   if (getLangOpts().CPlusPlus) {
17629     // Check that there are no default arguments (C++ only).
17630     CheckExtraCXXDefaultArguments(D);
17631   }
17632 
17633   return CreateParsedType(T, TInfo);
17634 }
17635 
17636 QualType Sema::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc,
17637                                             TypeResult ParsedType) {
17638   assert(ParsedType.isUsable() && "Expect usable parsed mapper type");
17639 
17640   QualType MapperType = GetTypeFromParser(ParsedType.get());
17641   assert(!MapperType.isNull() && "Expect valid mapper type");
17642 
17643   // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
17644   //  The type must be of struct, union or class type in C and C++
17645   if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) {
17646     Diag(TyLoc, diag::err_omp_mapper_wrong_type);
17647     return QualType();
17648   }
17649   return MapperType;
17650 }
17651 
17652 OMPDeclareMapperDecl *Sema::ActOnOpenMPDeclareMapperDirectiveStart(
17653     Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType,
17654     SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS,
17655     Decl *PrevDeclInScope) {
17656   LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPMapperName,
17657                       forRedeclarationInCurContext());
17658   // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
17659   //  A mapper-identifier may not be redeclared in the current scope for the
17660   //  same type or for a type that is compatible according to the base language
17661   //  rules.
17662   llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
17663   OMPDeclareMapperDecl *PrevDMD = nullptr;
17664   bool InCompoundScope = true;
17665   if (S != nullptr) {
17666     // Find previous declaration with the same name not referenced in other
17667     // declarations.
17668     FunctionScopeInfo *ParentFn = getEnclosingFunction();
17669     InCompoundScope =
17670         (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
17671     LookupName(Lookup, S);
17672     FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
17673                          /*AllowInlineNamespace=*/false);
17674     llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious;
17675     LookupResult::Filter Filter = Lookup.makeFilter();
17676     while (Filter.hasNext()) {
17677       auto *PrevDecl = cast<OMPDeclareMapperDecl>(Filter.next());
17678       if (InCompoundScope) {
17679         auto I = UsedAsPrevious.find(PrevDecl);
17680         if (I == UsedAsPrevious.end())
17681           UsedAsPrevious[PrevDecl] = false;
17682         if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope())
17683           UsedAsPrevious[D] = true;
17684       }
17685       PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
17686           PrevDecl->getLocation();
17687     }
17688     Filter.done();
17689     if (InCompoundScope) {
17690       for (const auto &PrevData : UsedAsPrevious) {
17691         if (!PrevData.second) {
17692           PrevDMD = PrevData.first;
17693           break;
17694         }
17695       }
17696     }
17697   } else if (PrevDeclInScope) {
17698     auto *PrevDMDInScope = PrevDMD =
17699         cast<OMPDeclareMapperDecl>(PrevDeclInScope);
17700     do {
17701       PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] =
17702           PrevDMDInScope->getLocation();
17703       PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope();
17704     } while (PrevDMDInScope != nullptr);
17705   }
17706   const auto I = PreviousRedeclTypes.find(MapperType.getCanonicalType());
17707   bool Invalid = false;
17708   if (I != PreviousRedeclTypes.end()) {
17709     Diag(StartLoc, diag::err_omp_declare_mapper_redefinition)
17710         << MapperType << Name;
17711     Diag(I->second, diag::note_previous_definition);
17712     Invalid = true;
17713   }
17714   auto *DMD = OMPDeclareMapperDecl::Create(Context, DC, StartLoc, Name,
17715                                            MapperType, VN, PrevDMD);
17716   DC->addDecl(DMD);
17717   DMD->setAccess(AS);
17718   if (Invalid)
17719     DMD->setInvalidDecl();
17720 
17721   // Enter new function scope.
17722   PushFunctionScope();
17723   setFunctionHasBranchProtectedScope();
17724 
17725   CurContext = DMD;
17726 
17727   return DMD;
17728 }
17729 
17730 void Sema::ActOnOpenMPDeclareMapperDirectiveVarDecl(OMPDeclareMapperDecl *DMD,
17731                                                     Scope *S,
17732                                                     QualType MapperType,
17733                                                     SourceLocation StartLoc,
17734                                                     DeclarationName VN) {
17735   VarDecl *VD = buildVarDecl(*this, StartLoc, MapperType, VN.getAsString());
17736   if (S)
17737     PushOnScopeChains(VD, S);
17738   else
17739     DMD->addDecl(VD);
17740   Expr *MapperVarRefExpr = buildDeclRefExpr(*this, VD, MapperType, StartLoc);
17741   DMD->setMapperVarRef(MapperVarRefExpr);
17742 }
17743 
17744 Sema::DeclGroupPtrTy
17745 Sema::ActOnOpenMPDeclareMapperDirectiveEnd(OMPDeclareMapperDecl *D, Scope *S,
17746                                            ArrayRef<OMPClause *> ClauseList) {
17747   PopDeclContext();
17748   PopFunctionScopeInfo();
17749 
17750   if (D) {
17751     if (S)
17752       PushOnScopeChains(D, S, /*AddToContext=*/false);
17753     D->CreateClauses(Context, ClauseList);
17754   }
17755 
17756   return DeclGroupPtrTy::make(DeclGroupRef(D));
17757 }
17758 
17759 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
17760                                            SourceLocation StartLoc,
17761                                            SourceLocation LParenLoc,
17762                                            SourceLocation EndLoc) {
17763   Expr *ValExpr = NumTeams;
17764   Stmt *HelperValStmt = nullptr;
17765 
17766   // OpenMP [teams Constrcut, Restrictions]
17767   // The num_teams expression must evaluate to a positive integer value.
17768   if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
17769                                  /*StrictlyPositive=*/true))
17770     return nullptr;
17771 
17772   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
17773   OpenMPDirectiveKind CaptureRegion =
17774       getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams, LangOpts.OpenMP);
17775   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
17776     ValExpr = MakeFullExpr(ValExpr).get();
17777     llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
17778     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
17779     HelperValStmt = buildPreInits(Context, Captures);
17780   }
17781 
17782   return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
17783                                          StartLoc, LParenLoc, EndLoc);
17784 }
17785 
17786 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
17787                                               SourceLocation StartLoc,
17788                                               SourceLocation LParenLoc,
17789                                               SourceLocation EndLoc) {
17790   Expr *ValExpr = ThreadLimit;
17791   Stmt *HelperValStmt = nullptr;
17792 
17793   // OpenMP [teams Constrcut, Restrictions]
17794   // The thread_limit expression must evaluate to a positive integer value.
17795   if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
17796                                  /*StrictlyPositive=*/true))
17797     return nullptr;
17798 
17799   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
17800   OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
17801       DKind, OMPC_thread_limit, LangOpts.OpenMP);
17802   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
17803     ValExpr = MakeFullExpr(ValExpr).get();
17804     llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
17805     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
17806     HelperValStmt = buildPreInits(Context, Captures);
17807   }
17808 
17809   return new (Context) OMPThreadLimitClause(
17810       ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
17811 }
17812 
17813 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
17814                                            SourceLocation StartLoc,
17815                                            SourceLocation LParenLoc,
17816                                            SourceLocation EndLoc) {
17817   Expr *ValExpr = Priority;
17818   Stmt *HelperValStmt = nullptr;
17819   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
17820 
17821   // OpenMP [2.9.1, task Constrcut]
17822   // The priority-value is a non-negative numerical scalar expression.
17823   if (!isNonNegativeIntegerValue(
17824           ValExpr, *this, OMPC_priority,
17825           /*StrictlyPositive=*/false, /*BuildCapture=*/true,
17826           DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt))
17827     return nullptr;
17828 
17829   return new (Context) OMPPriorityClause(ValExpr, HelperValStmt, CaptureRegion,
17830                                          StartLoc, LParenLoc, EndLoc);
17831 }
17832 
17833 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
17834                                             SourceLocation StartLoc,
17835                                             SourceLocation LParenLoc,
17836                                             SourceLocation EndLoc) {
17837   Expr *ValExpr = Grainsize;
17838   Stmt *HelperValStmt = nullptr;
17839   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
17840 
17841   // OpenMP [2.9.2, taskloop Constrcut]
17842   // The parameter of the grainsize clause must be a positive integer
17843   // expression.
17844   if (!isNonNegativeIntegerValue(
17845           ValExpr, *this, OMPC_grainsize,
17846           /*StrictlyPositive=*/true, /*BuildCapture=*/true,
17847           DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt))
17848     return nullptr;
17849 
17850   return new (Context) OMPGrainsizeClause(ValExpr, HelperValStmt, CaptureRegion,
17851                                           StartLoc, LParenLoc, EndLoc);
17852 }
17853 
17854 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
17855                                            SourceLocation StartLoc,
17856                                            SourceLocation LParenLoc,
17857                                            SourceLocation EndLoc) {
17858   Expr *ValExpr = NumTasks;
17859   Stmt *HelperValStmt = nullptr;
17860   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
17861 
17862   // OpenMP [2.9.2, taskloop Constrcut]
17863   // The parameter of the num_tasks clause must be a positive integer
17864   // expression.
17865   if (!isNonNegativeIntegerValue(
17866           ValExpr, *this, OMPC_num_tasks,
17867           /*StrictlyPositive=*/true, /*BuildCapture=*/true,
17868           DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt))
17869     return nullptr;
17870 
17871   return new (Context) OMPNumTasksClause(ValExpr, HelperValStmt, CaptureRegion,
17872                                          StartLoc, LParenLoc, EndLoc);
17873 }
17874 
17875 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
17876                                        SourceLocation LParenLoc,
17877                                        SourceLocation EndLoc) {
17878   // OpenMP [2.13.2, critical construct, Description]
17879   // ... where hint-expression is an integer constant expression that evaluates
17880   // to a valid lock hint.
17881   ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
17882   if (HintExpr.isInvalid())
17883     return nullptr;
17884   return new (Context)
17885       OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
17886 }
17887 
17888 /// Tries to find omp_event_handle_t type.
17889 static bool findOMPEventHandleT(Sema &S, SourceLocation Loc,
17890                                 DSAStackTy *Stack) {
17891   QualType OMPEventHandleT = Stack->getOMPEventHandleT();
17892   if (!OMPEventHandleT.isNull())
17893     return true;
17894   IdentifierInfo *II = &S.PP.getIdentifierTable().get("omp_event_handle_t");
17895   ParsedType PT = S.getTypeName(*II, Loc, S.getCurScope());
17896   if (!PT.getAsOpaquePtr() || PT.get().isNull()) {
17897     S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_event_handle_t";
17898     return false;
17899   }
17900   Stack->setOMPEventHandleT(PT.get());
17901   return true;
17902 }
17903 
17904 OMPClause *Sema::ActOnOpenMPDetachClause(Expr *Evt, SourceLocation StartLoc,
17905                                          SourceLocation LParenLoc,
17906                                          SourceLocation EndLoc) {
17907   if (!Evt->isValueDependent() && !Evt->isTypeDependent() &&
17908       !Evt->isInstantiationDependent() &&
17909       !Evt->containsUnexpandedParameterPack()) {
17910     if (!findOMPEventHandleT(*this, Evt->getExprLoc(), DSAStack))
17911       return nullptr;
17912     // OpenMP 5.0, 2.10.1 task Construct.
17913     // event-handle is a variable of the omp_event_handle_t type.
17914     auto *Ref = dyn_cast<DeclRefExpr>(Evt->IgnoreParenImpCasts());
17915     if (!Ref) {
17916       Diag(Evt->getExprLoc(), diag::err_omp_var_expected)
17917           << "omp_event_handle_t" << 0 << Evt->getSourceRange();
17918       return nullptr;
17919     }
17920     auto *VD = dyn_cast_or_null<VarDecl>(Ref->getDecl());
17921     if (!VD) {
17922       Diag(Evt->getExprLoc(), diag::err_omp_var_expected)
17923           << "omp_event_handle_t" << 0 << Evt->getSourceRange();
17924       return nullptr;
17925     }
17926     if (!Context.hasSameUnqualifiedType(DSAStack->getOMPEventHandleT(),
17927                                         VD->getType()) ||
17928         VD->getType().isConstant(Context)) {
17929       Diag(Evt->getExprLoc(), diag::err_omp_var_expected)
17930           << "omp_event_handle_t" << 1 << VD->getType()
17931           << Evt->getSourceRange();
17932       return nullptr;
17933     }
17934     // OpenMP 5.0, 2.10.1 task Construct
17935     // [detach clause]... The event-handle will be considered as if it was
17936     // specified on a firstprivate clause.
17937     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, /*FromParent=*/false);
17938     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
17939         DVar.RefExpr) {
17940       Diag(Evt->getExprLoc(), diag::err_omp_wrong_dsa)
17941           << getOpenMPClauseName(DVar.CKind)
17942           << getOpenMPClauseName(OMPC_firstprivate);
17943       reportOriginalDsa(*this, DSAStack, VD, DVar);
17944       return nullptr;
17945     }
17946   }
17947 
17948   return new (Context) OMPDetachClause(Evt, StartLoc, LParenLoc, EndLoc);
17949 }
17950 
17951 OMPClause *Sema::ActOnOpenMPDistScheduleClause(
17952     OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
17953     SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
17954     SourceLocation EndLoc) {
17955   if (Kind == OMPC_DIST_SCHEDULE_unknown) {
17956     std::string Values;
17957     Values += "'";
17958     Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
17959     Values += "'";
17960     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
17961         << Values << getOpenMPClauseName(OMPC_dist_schedule);
17962     return nullptr;
17963   }
17964   Expr *ValExpr = ChunkSize;
17965   Stmt *HelperValStmt = nullptr;
17966   if (ChunkSize) {
17967     if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
17968         !ChunkSize->isInstantiationDependent() &&
17969         !ChunkSize->containsUnexpandedParameterPack()) {
17970       SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
17971       ExprResult Val =
17972           PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
17973       if (Val.isInvalid())
17974         return nullptr;
17975 
17976       ValExpr = Val.get();
17977 
17978       // OpenMP [2.7.1, Restrictions]
17979       //  chunk_size must be a loop invariant integer expression with a positive
17980       //  value.
17981       llvm::APSInt Result;
17982       if (ValExpr->isIntegerConstantExpr(Result, Context)) {
17983         if (Result.isSigned() && !Result.isStrictlyPositive()) {
17984           Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
17985               << "dist_schedule" << ChunkSize->getSourceRange();
17986           return nullptr;
17987         }
17988       } else if (getOpenMPCaptureRegionForClause(
17989                      DSAStack->getCurrentDirective(), OMPC_dist_schedule,
17990                      LangOpts.OpenMP) != OMPD_unknown &&
17991                  !CurContext->isDependentContext()) {
17992         ValExpr = MakeFullExpr(ValExpr).get();
17993         llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
17994         ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
17995         HelperValStmt = buildPreInits(Context, Captures);
17996       }
17997     }
17998   }
17999 
18000   return new (Context)
18001       OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
18002                             Kind, ValExpr, HelperValStmt);
18003 }
18004 
18005 OMPClause *Sema::ActOnOpenMPDefaultmapClause(
18006     OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
18007     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
18008     SourceLocation KindLoc, SourceLocation EndLoc) {
18009   if (getLangOpts().OpenMP < 50) {
18010     if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
18011         Kind != OMPC_DEFAULTMAP_scalar) {
18012       std::string Value;
18013       SourceLocation Loc;
18014       Value += "'";
18015       if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
18016         Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
18017                                                OMPC_DEFAULTMAP_MODIFIER_tofrom);
18018         Loc = MLoc;
18019       } else {
18020         Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
18021                                                OMPC_DEFAULTMAP_scalar);
18022         Loc = KindLoc;
18023       }
18024       Value += "'";
18025       Diag(Loc, diag::err_omp_unexpected_clause_value)
18026           << Value << getOpenMPClauseName(OMPC_defaultmap);
18027       return nullptr;
18028     }
18029   } else {
18030     bool isDefaultmapModifier = (M != OMPC_DEFAULTMAP_MODIFIER_unknown);
18031     bool isDefaultmapKind = (Kind != OMPC_DEFAULTMAP_unknown) ||
18032                             (LangOpts.OpenMP >= 50 && KindLoc.isInvalid());
18033     if (!isDefaultmapKind || !isDefaultmapModifier) {
18034       std::string ModifierValue = "'alloc', 'from', 'to', 'tofrom', "
18035                                   "'firstprivate', 'none', 'default'";
18036       std::string KindValue = "'scalar', 'aggregate', 'pointer'";
18037       if (!isDefaultmapKind && isDefaultmapModifier) {
18038         Diag(KindLoc, diag::err_omp_unexpected_clause_value)
18039             << KindValue << getOpenMPClauseName(OMPC_defaultmap);
18040       } else if (isDefaultmapKind && !isDefaultmapModifier) {
18041         Diag(MLoc, diag::err_omp_unexpected_clause_value)
18042             << ModifierValue << getOpenMPClauseName(OMPC_defaultmap);
18043       } else {
18044         Diag(MLoc, diag::err_omp_unexpected_clause_value)
18045             << ModifierValue << getOpenMPClauseName(OMPC_defaultmap);
18046         Diag(KindLoc, diag::err_omp_unexpected_clause_value)
18047             << KindValue << getOpenMPClauseName(OMPC_defaultmap);
18048       }
18049       return nullptr;
18050     }
18051 
18052     // OpenMP [5.0, 2.12.5, Restrictions, p. 174]
18053     //  At most one defaultmap clause for each category can appear on the
18054     //  directive.
18055     if (DSAStack->checkDefaultmapCategory(Kind)) {
18056       Diag(StartLoc, diag::err_omp_one_defaultmap_each_category);
18057       return nullptr;
18058     }
18059   }
18060   if (Kind == OMPC_DEFAULTMAP_unknown) {
18061     // Variable category is not specified - mark all categories.
18062     DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_aggregate, StartLoc);
18063     DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_scalar, StartLoc);
18064     DSAStack->setDefaultDMAAttr(M, OMPC_DEFAULTMAP_pointer, StartLoc);
18065   } else {
18066     DSAStack->setDefaultDMAAttr(M, Kind, StartLoc);
18067   }
18068 
18069   return new (Context)
18070       OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
18071 }
18072 
18073 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
18074   DeclContext *CurLexicalContext = getCurLexicalContext();
18075   if (!CurLexicalContext->isFileContext() &&
18076       !CurLexicalContext->isExternCContext() &&
18077       !CurLexicalContext->isExternCXXContext() &&
18078       !isa<CXXRecordDecl>(CurLexicalContext) &&
18079       !isa<ClassTemplateDecl>(CurLexicalContext) &&
18080       !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
18081       !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
18082     Diag(Loc, diag::err_omp_region_not_file_context);
18083     return false;
18084   }
18085   ++DeclareTargetNestingLevel;
18086   return true;
18087 }
18088 
18089 void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
18090   assert(DeclareTargetNestingLevel > 0 &&
18091          "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
18092   --DeclareTargetNestingLevel;
18093 }
18094 
18095 NamedDecl *
18096 Sema::lookupOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec,
18097                                     const DeclarationNameInfo &Id,
18098                                     NamedDeclSetType &SameDirectiveDecls) {
18099   LookupResult Lookup(*this, Id, LookupOrdinaryName);
18100   LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
18101 
18102   if (Lookup.isAmbiguous())
18103     return nullptr;
18104   Lookup.suppressDiagnostics();
18105 
18106   if (!Lookup.isSingleResult()) {
18107     VarOrFuncDeclFilterCCC CCC(*this);
18108     if (TypoCorrection Corrected =
18109             CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC,
18110                         CTK_ErrorRecovery)) {
18111       diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
18112                                   << Id.getName());
18113       checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
18114       return nullptr;
18115     }
18116 
18117     Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
18118     return nullptr;
18119   }
18120 
18121   NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
18122   if (!isa<VarDecl>(ND) && !isa<FunctionDecl>(ND) &&
18123       !isa<FunctionTemplateDecl>(ND)) {
18124     Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
18125     return nullptr;
18126   }
18127   if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
18128     Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
18129   return ND;
18130 }
18131 
18132 void Sema::ActOnOpenMPDeclareTargetName(
18133     NamedDecl *ND, SourceLocation Loc, OMPDeclareTargetDeclAttr::MapTypeTy MT,
18134     OMPDeclareTargetDeclAttr::DevTypeTy DT) {
18135   assert((isa<VarDecl>(ND) || isa<FunctionDecl>(ND) ||
18136           isa<FunctionTemplateDecl>(ND)) &&
18137          "Expected variable, function or function template.");
18138 
18139   // Diagnose marking after use as it may lead to incorrect diagnosis and
18140   // codegen.
18141   if (LangOpts.OpenMP >= 50 &&
18142       (ND->isUsed(/*CheckUsedAttr=*/false) || ND->isReferenced()))
18143     Diag(Loc, diag::warn_omp_declare_target_after_first_use);
18144 
18145   Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
18146       OMPDeclareTargetDeclAttr::getDeviceType(cast<ValueDecl>(ND));
18147   if (DevTy.hasValue() && *DevTy != DT) {
18148     Diag(Loc, diag::err_omp_device_type_mismatch)
18149         << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(DT)
18150         << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(*DevTy);
18151     return;
18152   }
18153   Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
18154       OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(cast<ValueDecl>(ND));
18155   if (!Res) {
18156     auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT, DT,
18157                                                        SourceRange(Loc, Loc));
18158     ND->addAttr(A);
18159     if (ASTMutationListener *ML = Context.getASTMutationListener())
18160       ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
18161     checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Loc);
18162   } else if (*Res != MT) {
18163     Diag(Loc, diag::err_omp_declare_target_to_and_link) << ND;
18164   }
18165 }
18166 
18167 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
18168                                      Sema &SemaRef, Decl *D) {
18169   if (!D || !isa<VarDecl>(D))
18170     return;
18171   auto *VD = cast<VarDecl>(D);
18172   Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy =
18173       OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
18174   if (SemaRef.LangOpts.OpenMP >= 50 &&
18175       (SemaRef.getCurLambda(/*IgnoreNonLambdaCapturingScope=*/true) ||
18176        SemaRef.getCurBlock() || SemaRef.getCurCapturedRegion()) &&
18177       VD->hasGlobalStorage()) {
18178     llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy =
18179         OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
18180     if (!MapTy || *MapTy != OMPDeclareTargetDeclAttr::MT_To) {
18181       // OpenMP 5.0, 2.12.7 declare target Directive, Restrictions
18182       // If a lambda declaration and definition appears between a
18183       // declare target directive and the matching end declare target
18184       // directive, all variables that are captured by the lambda
18185       // expression must also appear in a to clause.
18186       SemaRef.Diag(VD->getLocation(),
18187                    diag::err_omp_lambda_capture_in_declare_target_not_to);
18188       SemaRef.Diag(SL, diag::note_var_explicitly_captured_here)
18189           << VD << 0 << SR;
18190       return;
18191     }
18192   }
18193   if (MapTy.hasValue())
18194     return;
18195   SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context);
18196   SemaRef.Diag(SL, diag::note_used_here) << SR;
18197 }
18198 
18199 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
18200                                    Sema &SemaRef, DSAStackTy *Stack,
18201                                    ValueDecl *VD) {
18202   return OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) ||
18203          checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(),
18204                            /*FullCheck=*/false);
18205 }
18206 
18207 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
18208                                             SourceLocation IdLoc) {
18209   if (!D || D->isInvalidDecl())
18210     return;
18211   SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
18212   SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
18213   if (auto *VD = dyn_cast<VarDecl>(D)) {
18214     // Only global variables can be marked as declare target.
18215     if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
18216         !VD->isStaticDataMember())
18217       return;
18218     // 2.10.6: threadprivate variable cannot appear in a declare target
18219     // directive.
18220     if (DSAStack->isThreadPrivate(VD)) {
18221       Diag(SL, diag::err_omp_threadprivate_in_target);
18222       reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
18223       return;
18224     }
18225   }
18226   if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
18227     D = FTD->getTemplatedDecl();
18228   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
18229     llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
18230         OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD);
18231     if (IdLoc.isValid() && Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) {
18232       Diag(IdLoc, diag::err_omp_function_in_link_clause);
18233       Diag(FD->getLocation(), diag::note_defined_here) << FD;
18234       return;
18235     }
18236   }
18237   if (auto *VD = dyn_cast<ValueDecl>(D)) {
18238     // Problem if any with var declared with incomplete type will be reported
18239     // as normal, so no need to check it here.
18240     if ((E || !VD->getType()->isIncompleteType()) &&
18241         !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD))
18242       return;
18243     if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
18244       // Checking declaration inside declare target region.
18245       if (isa<VarDecl>(D) || isa<FunctionDecl>(D) ||
18246           isa<FunctionTemplateDecl>(D)) {
18247         auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
18248             Context, OMPDeclareTargetDeclAttr::MT_To,
18249             OMPDeclareTargetDeclAttr::DT_Any, SourceRange(IdLoc, IdLoc));
18250         D->addAttr(A);
18251         if (ASTMutationListener *ML = Context.getASTMutationListener())
18252           ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
18253       }
18254       return;
18255     }
18256   }
18257   if (!E)
18258     return;
18259   checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
18260 }
18261 
18262 OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
18263                                      CXXScopeSpec &MapperIdScopeSpec,
18264                                      DeclarationNameInfo &MapperId,
18265                                      const OMPVarListLocTy &Locs,
18266                                      ArrayRef<Expr *> UnresolvedMappers) {
18267   MappableVarListInfo MVLI(VarList);
18268   checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, Locs.StartLoc,
18269                               MapperIdScopeSpec, MapperId, UnresolvedMappers);
18270   if (MVLI.ProcessedVarList.empty())
18271     return nullptr;
18272 
18273   return OMPToClause::Create(
18274       Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
18275       MVLI.VarComponents, MVLI.UDMapperList,
18276       MapperIdScopeSpec.getWithLocInContext(Context), MapperId);
18277 }
18278 
18279 OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
18280                                        CXXScopeSpec &MapperIdScopeSpec,
18281                                        DeclarationNameInfo &MapperId,
18282                                        const OMPVarListLocTy &Locs,
18283                                        ArrayRef<Expr *> UnresolvedMappers) {
18284   MappableVarListInfo MVLI(VarList);
18285   checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, Locs.StartLoc,
18286                               MapperIdScopeSpec, MapperId, UnresolvedMappers);
18287   if (MVLI.ProcessedVarList.empty())
18288     return nullptr;
18289 
18290   return OMPFromClause::Create(
18291       Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
18292       MVLI.VarComponents, MVLI.UDMapperList,
18293       MapperIdScopeSpec.getWithLocInContext(Context), MapperId);
18294 }
18295 
18296 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
18297                                                const OMPVarListLocTy &Locs) {
18298   MappableVarListInfo MVLI(VarList);
18299   SmallVector<Expr *, 8> PrivateCopies;
18300   SmallVector<Expr *, 8> Inits;
18301 
18302   for (Expr *RefExpr : VarList) {
18303     assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
18304     SourceLocation ELoc;
18305     SourceRange ERange;
18306     Expr *SimpleRefExpr = RefExpr;
18307     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
18308     if (Res.second) {
18309       // It will be analyzed later.
18310       MVLI.ProcessedVarList.push_back(RefExpr);
18311       PrivateCopies.push_back(nullptr);
18312       Inits.push_back(nullptr);
18313     }
18314     ValueDecl *D = Res.first;
18315     if (!D)
18316       continue;
18317 
18318     QualType Type = D->getType();
18319     Type = Type.getNonReferenceType().getUnqualifiedType();
18320 
18321     auto *VD = dyn_cast<VarDecl>(D);
18322 
18323     // Item should be a pointer or reference to pointer.
18324     if (!Type->isPointerType()) {
18325       Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
18326           << 0 << RefExpr->getSourceRange();
18327       continue;
18328     }
18329 
18330     // Build the private variable and the expression that refers to it.
18331     auto VDPrivate =
18332         buildVarDecl(*this, ELoc, Type, D->getName(),
18333                      D->hasAttrs() ? &D->getAttrs() : nullptr,
18334                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
18335     if (VDPrivate->isInvalidDecl())
18336       continue;
18337 
18338     CurContext->addDecl(VDPrivate);
18339     DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
18340         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
18341 
18342     // Add temporary variable to initialize the private copy of the pointer.
18343     VarDecl *VDInit =
18344         buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
18345     DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
18346         *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc());
18347     AddInitializerToDecl(VDPrivate,
18348                          DefaultLvalueConversion(VDInitRefExpr).get(),
18349                          /*DirectInit=*/false);
18350 
18351     // If required, build a capture to implement the privatization initialized
18352     // with the current list item value.
18353     DeclRefExpr *Ref = nullptr;
18354     if (!VD)
18355       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
18356     MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
18357     PrivateCopies.push_back(VDPrivateRefExpr);
18358     Inits.push_back(VDInitRefExpr);
18359 
18360     // We need to add a data sharing attribute for this variable to make sure it
18361     // is correctly captured. A variable that shows up in a use_device_ptr has
18362     // similar properties of a first private variable.
18363     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
18364 
18365     // Create a mappable component for the list item. List items in this clause
18366     // only need a component.
18367     MVLI.VarBaseDeclarations.push_back(D);
18368     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
18369     MVLI.VarComponents.back().push_back(
18370         OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
18371   }
18372 
18373   if (MVLI.ProcessedVarList.empty())
18374     return nullptr;
18375 
18376   return OMPUseDevicePtrClause::Create(
18377       Context, Locs, MVLI.ProcessedVarList, PrivateCopies, Inits,
18378       MVLI.VarBaseDeclarations, MVLI.VarComponents);
18379 }
18380 
18381 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
18382                                               const OMPVarListLocTy &Locs) {
18383   MappableVarListInfo MVLI(VarList);
18384   for (Expr *RefExpr : VarList) {
18385     assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
18386     SourceLocation ELoc;
18387     SourceRange ERange;
18388     Expr *SimpleRefExpr = RefExpr;
18389     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
18390     if (Res.second) {
18391       // It will be analyzed later.
18392       MVLI.ProcessedVarList.push_back(RefExpr);
18393     }
18394     ValueDecl *D = Res.first;
18395     if (!D)
18396       continue;
18397 
18398     QualType Type = D->getType();
18399     // item should be a pointer or array or reference to pointer or array
18400     if (!Type.getNonReferenceType()->isPointerType() &&
18401         !Type.getNonReferenceType()->isArrayType()) {
18402       Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
18403           << 0 << RefExpr->getSourceRange();
18404       continue;
18405     }
18406 
18407     // Check if the declaration in the clause does not show up in any data
18408     // sharing attribute.
18409     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
18410     if (isOpenMPPrivate(DVar.CKind)) {
18411       Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
18412           << getOpenMPClauseName(DVar.CKind)
18413           << getOpenMPClauseName(OMPC_is_device_ptr)
18414           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
18415       reportOriginalDsa(*this, DSAStack, D, DVar);
18416       continue;
18417     }
18418 
18419     const Expr *ConflictExpr;
18420     if (DSAStack->checkMappableExprComponentListsForDecl(
18421             D, /*CurrentRegionOnly=*/true,
18422             [&ConflictExpr](
18423                 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
18424                 OpenMPClauseKind) -> bool {
18425               ConflictExpr = R.front().getAssociatedExpression();
18426               return true;
18427             })) {
18428       Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
18429       Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
18430           << ConflictExpr->getSourceRange();
18431       continue;
18432     }
18433 
18434     // Store the components in the stack so that they can be used to check
18435     // against other clauses later on.
18436     OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
18437     DSAStack->addMappableExpressionComponents(
18438         D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
18439 
18440     // Record the expression we've just processed.
18441     MVLI.ProcessedVarList.push_back(SimpleRefExpr);
18442 
18443     // Create a mappable component for the list item. List items in this clause
18444     // only need a component. We use a null declaration to signal fields in
18445     // 'this'.
18446     assert((isa<DeclRefExpr>(SimpleRefExpr) ||
18447             isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
18448            "Unexpected device pointer expression!");
18449     MVLI.VarBaseDeclarations.push_back(
18450         isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
18451     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
18452     MVLI.VarComponents.back().push_back(MC);
18453   }
18454 
18455   if (MVLI.ProcessedVarList.empty())
18456     return nullptr;
18457 
18458   return OMPIsDevicePtrClause::Create(Context, Locs, MVLI.ProcessedVarList,
18459                                       MVLI.VarBaseDeclarations,
18460                                       MVLI.VarComponents);
18461 }
18462 
18463 OMPClause *Sema::ActOnOpenMPAllocateClause(
18464     Expr *Allocator, ArrayRef<Expr *> VarList, SourceLocation StartLoc,
18465     SourceLocation ColonLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
18466   if (Allocator) {
18467     // OpenMP [2.11.4 allocate Clause, Description]
18468     // allocator is an expression of omp_allocator_handle_t type.
18469     if (!findOMPAllocatorHandleT(*this, Allocator->getExprLoc(), DSAStack))
18470       return nullptr;
18471 
18472     ExprResult AllocatorRes = DefaultLvalueConversion(Allocator);
18473     if (AllocatorRes.isInvalid())
18474       return nullptr;
18475     AllocatorRes = PerformImplicitConversion(AllocatorRes.get(),
18476                                              DSAStack->getOMPAllocatorHandleT(),
18477                                              Sema::AA_Initializing,
18478                                              /*AllowExplicit=*/true);
18479     if (AllocatorRes.isInvalid())
18480       return nullptr;
18481     Allocator = AllocatorRes.get();
18482   } else {
18483     // OpenMP 5.0, 2.11.4 allocate Clause, Restrictions.
18484     // allocate clauses that appear on a target construct or on constructs in a
18485     // target region must specify an allocator expression unless a requires
18486     // directive with the dynamic_allocators clause is present in the same
18487     // compilation unit.
18488     if (LangOpts.OpenMPIsDevice &&
18489         !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
18490       targetDiag(StartLoc, diag::err_expected_allocator_expression);
18491   }
18492   // Analyze and build list of variables.
18493   SmallVector<Expr *, 8> Vars;
18494   for (Expr *RefExpr : VarList) {
18495     assert(RefExpr && "NULL expr in OpenMP private clause.");
18496     SourceLocation ELoc;
18497     SourceRange ERange;
18498     Expr *SimpleRefExpr = RefExpr;
18499     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
18500     if (Res.second) {
18501       // It will be analyzed later.
18502       Vars.push_back(RefExpr);
18503     }
18504     ValueDecl *D = Res.first;
18505     if (!D)
18506       continue;
18507 
18508     auto *VD = dyn_cast<VarDecl>(D);
18509     DeclRefExpr *Ref = nullptr;
18510     if (!VD && !CurContext->isDependentContext())
18511       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
18512     Vars.push_back((VD || CurContext->isDependentContext())
18513                        ? RefExpr->IgnoreParens()
18514                        : Ref);
18515   }
18516 
18517   if (Vars.empty())
18518     return nullptr;
18519 
18520   if (Allocator)
18521     DSAStack->addInnerAllocatorExpr(Allocator);
18522   return OMPAllocateClause::Create(Context, StartLoc, LParenLoc, Allocator,
18523                                    ColonLoc, EndLoc, Vars);
18524 }
18525 
18526 OMPClause *Sema::ActOnOpenMPNontemporalClause(ArrayRef<Expr *> VarList,
18527                                               SourceLocation StartLoc,
18528                                               SourceLocation LParenLoc,
18529                                               SourceLocation EndLoc) {
18530   SmallVector<Expr *, 8> Vars;
18531   for (Expr *RefExpr : VarList) {
18532     assert(RefExpr && "NULL expr in OpenMP nontemporal clause.");
18533     SourceLocation ELoc;
18534     SourceRange ERange;
18535     Expr *SimpleRefExpr = RefExpr;
18536     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
18537     if (Res.second)
18538       // It will be analyzed later.
18539       Vars.push_back(RefExpr);
18540     ValueDecl *D = Res.first;
18541     if (!D)
18542       continue;
18543 
18544     // OpenMP 5.0, 2.9.3.1 simd Construct, Restrictions.
18545     // A list-item cannot appear in more than one nontemporal clause.
18546     if (const Expr *PrevRef =
18547             DSAStack->addUniqueNontemporal(D, SimpleRefExpr)) {
18548       Diag(ELoc, diag::err_omp_used_in_clause_twice)
18549           << 0 << getOpenMPClauseName(OMPC_nontemporal) << ERange;
18550       Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
18551           << getOpenMPClauseName(OMPC_nontemporal);
18552       continue;
18553     }
18554 
18555     Vars.push_back(RefExpr);
18556   }
18557 
18558   if (Vars.empty())
18559     return nullptr;
18560 
18561   return OMPNontemporalClause::Create(Context, StartLoc, LParenLoc, EndLoc,
18562                                       Vars);
18563 }
18564 
18565 OMPClause *Sema::ActOnOpenMPInclusiveClause(ArrayRef<Expr *> VarList,
18566                                             SourceLocation StartLoc,
18567                                             SourceLocation LParenLoc,
18568                                             SourceLocation EndLoc) {
18569   SmallVector<Expr *, 8> Vars;
18570   for (Expr *RefExpr : VarList) {
18571     assert(RefExpr && "NULL expr in OpenMP nontemporal clause.");
18572     SourceLocation ELoc;
18573     SourceRange ERange;
18574     Expr *SimpleRefExpr = RefExpr;
18575     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
18576                               /*AllowArraySection=*/true);
18577     if (Res.second)
18578       // It will be analyzed later.
18579       Vars.push_back(RefExpr);
18580     ValueDecl *D = Res.first;
18581     if (!D)
18582       continue;
18583 
18584     const DSAStackTy::DSAVarData DVar =
18585         DSAStack->getTopDSA(D, /*FromParent=*/true);
18586     // OpenMP 5.0, 2.9.6, scan Directive, Restrictions.
18587     // A list item that appears in the inclusive or exclusive clause must appear
18588     // in a reduction clause with the inscan modifier on the enclosing
18589     // worksharing-loop, worksharing-loop SIMD, or simd construct.
18590     if (DVar.CKind != OMPC_reduction ||
18591         DVar.Modifier != OMPC_REDUCTION_inscan)
18592       Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction)
18593           << RefExpr->getSourceRange();
18594 
18595     if (DSAStack->getParentDirective() != OMPD_unknown)
18596       DSAStack->markDeclAsUsedInScanDirective(D);
18597     Vars.push_back(RefExpr);
18598   }
18599 
18600   if (Vars.empty())
18601     return nullptr;
18602 
18603   return OMPInclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
18604 }
18605 
18606 OMPClause *Sema::ActOnOpenMPExclusiveClause(ArrayRef<Expr *> VarList,
18607                                             SourceLocation StartLoc,
18608                                             SourceLocation LParenLoc,
18609                                             SourceLocation EndLoc) {
18610   SmallVector<Expr *, 8> Vars;
18611   for (Expr *RefExpr : VarList) {
18612     assert(RefExpr && "NULL expr in OpenMP nontemporal clause.");
18613     SourceLocation ELoc;
18614     SourceRange ERange;
18615     Expr *SimpleRefExpr = RefExpr;
18616     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
18617                               /*AllowArraySection=*/true);
18618     if (Res.second)
18619       // It will be analyzed later.
18620       Vars.push_back(RefExpr);
18621     ValueDecl *D = Res.first;
18622     if (!D)
18623       continue;
18624 
18625     OpenMPDirectiveKind ParentDirective = DSAStack->getParentDirective();
18626     DSAStackTy::DSAVarData DVar;
18627     if (ParentDirective != OMPD_unknown)
18628       DVar = DSAStack->getTopDSA(D, /*FromParent=*/true);
18629     // OpenMP 5.0, 2.9.6, scan Directive, Restrictions.
18630     // A list item that appears in the inclusive or exclusive clause must appear
18631     // in a reduction clause with the inscan modifier on the enclosing
18632     // worksharing-loop, worksharing-loop SIMD, or simd construct.
18633     if (ParentDirective == OMPD_unknown || DVar.CKind != OMPC_reduction ||
18634         DVar.Modifier != OMPC_REDUCTION_inscan) {
18635       Diag(ELoc, diag::err_omp_inclusive_exclusive_not_reduction)
18636           << RefExpr->getSourceRange();
18637     } else {
18638       DSAStack->markDeclAsUsedInScanDirective(D);
18639     }
18640     Vars.push_back(RefExpr);
18641   }
18642 
18643   if (Vars.empty())
18644     return nullptr;
18645 
18646   return OMPExclusiveClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
18647 }
18648 
18649 /// Tries to find omp_alloctrait_t type.
18650 static bool findOMPAlloctraitT(Sema &S, SourceLocation Loc, DSAStackTy *Stack) {
18651   QualType OMPAlloctraitT = Stack->getOMPAlloctraitT();
18652   if (!OMPAlloctraitT.isNull())
18653     return true;
18654   IdentifierInfo &II = S.PP.getIdentifierTable().get("omp_alloctrait_t");
18655   ParsedType PT = S.getTypeName(II, Loc, S.getCurScope());
18656   if (!PT.getAsOpaquePtr() || PT.get().isNull()) {
18657     S.Diag(Loc, diag::err_omp_implied_type_not_found) << "omp_alloctrait_t";
18658     return false;
18659   }
18660   Stack->setOMPAlloctraitT(PT.get());
18661   return true;
18662 }
18663 
18664 OMPClause *Sema::ActOnOpenMPUsesAllocatorClause(
18665     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc,
18666     ArrayRef<UsesAllocatorsData> Data) {
18667   // OpenMP [2.12.5, target Construct]
18668   // allocator is an identifier of omp_allocator_handle_t type.
18669   if (!findOMPAllocatorHandleT(*this, StartLoc, DSAStack))
18670     return nullptr;
18671   // OpenMP [2.12.5, target Construct]
18672   // allocator-traits-array is an identifier of const omp_alloctrait_t * type.
18673   if (llvm::any_of(
18674           Data,
18675           [](const UsesAllocatorsData &D) { return D.AllocatorTraits; }) &&
18676       !findOMPAlloctraitT(*this, StartLoc, DSAStack))
18677     return nullptr;
18678   llvm::SmallSet<CanonicalDeclPtr<Decl>, 4> PredefinedAllocators;
18679   for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
18680     auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
18681     StringRef Allocator =
18682         OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(AllocatorKind);
18683     DeclarationName AllocatorName = &Context.Idents.get(Allocator);
18684     PredefinedAllocators.insert(LookupSingleName(
18685         TUScope, AllocatorName, StartLoc, Sema::LookupAnyName));
18686   }
18687 
18688   SmallVector<OMPUsesAllocatorsClause::Data, 4> NewData;
18689   for (const UsesAllocatorsData &D : Data) {
18690     Expr *AllocatorExpr = nullptr;
18691     // Check allocator expression.
18692     if (D.Allocator->isTypeDependent()) {
18693       AllocatorExpr = D.Allocator;
18694     } else {
18695       // Traits were specified - need to assign new allocator to the specified
18696       // allocator, so it must be an lvalue.
18697       AllocatorExpr = D.Allocator->IgnoreParenImpCasts();
18698       auto *DRE = dyn_cast<DeclRefExpr>(AllocatorExpr);
18699       bool IsPredefinedAllocator = false;
18700       if (DRE)
18701         IsPredefinedAllocator = PredefinedAllocators.count(DRE->getDecl());
18702       if (!DRE ||
18703           !(Context.hasSameUnqualifiedType(
18704                 AllocatorExpr->getType(), DSAStack->getOMPAllocatorHandleT()) ||
18705             Context.typesAreCompatible(AllocatorExpr->getType(),
18706                                        DSAStack->getOMPAllocatorHandleT(),
18707                                        /*CompareUnqualified=*/true)) ||
18708           (!IsPredefinedAllocator &&
18709            (AllocatorExpr->getType().isConstant(Context) ||
18710             !AllocatorExpr->isLValue()))) {
18711         Diag(D.Allocator->getExprLoc(), diag::err_omp_var_expected)
18712             << "omp_allocator_handle_t" << (DRE ? 1 : 0)
18713             << AllocatorExpr->getType() << D.Allocator->getSourceRange();
18714         continue;
18715       }
18716       // OpenMP [2.12.5, target Construct]
18717       // Predefined allocators appearing in a uses_allocators clause cannot have
18718       // traits specified.
18719       if (IsPredefinedAllocator && D.AllocatorTraits) {
18720         Diag(D.AllocatorTraits->getExprLoc(),
18721              diag::err_omp_predefined_allocator_with_traits)
18722             << D.AllocatorTraits->getSourceRange();
18723         Diag(D.Allocator->getExprLoc(), diag::note_omp_predefined_allocator)
18724             << cast<NamedDecl>(DRE->getDecl())->getName()
18725             << D.Allocator->getSourceRange();
18726         continue;
18727       }
18728       // OpenMP [2.12.5, target Construct]
18729       // Non-predefined allocators appearing in a uses_allocators clause must
18730       // have traits specified.
18731       if (!IsPredefinedAllocator && !D.AllocatorTraits) {
18732         Diag(D.Allocator->getExprLoc(),
18733              diag::err_omp_nonpredefined_allocator_without_traits);
18734         continue;
18735       }
18736       // No allocator traits - just convert it to rvalue.
18737       if (!D.AllocatorTraits)
18738         AllocatorExpr = DefaultLvalueConversion(AllocatorExpr).get();
18739       DSAStack->addUsesAllocatorsDecl(
18740           DRE->getDecl(),
18741           IsPredefinedAllocator
18742               ? DSAStackTy::UsesAllocatorsDeclKind::PredefinedAllocator
18743               : DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator);
18744     }
18745     Expr *AllocatorTraitsExpr = nullptr;
18746     if (D.AllocatorTraits) {
18747       if (D.AllocatorTraits->isTypeDependent()) {
18748         AllocatorTraitsExpr = D.AllocatorTraits;
18749       } else {
18750         // OpenMP [2.12.5, target Construct]
18751         // Arrays that contain allocator traits that appear in a uses_allocators
18752         // clause must be constant arrays, have constant values and be defined
18753         // in the same scope as the construct in which the clause appears.
18754         AllocatorTraitsExpr = D.AllocatorTraits->IgnoreParenImpCasts();
18755         // Check that traits expr is a constant array.
18756         QualType TraitTy;
18757         if (const ArrayType *Ty =
18758                 AllocatorTraitsExpr->getType()->getAsArrayTypeUnsafe())
18759           if (const auto *ConstArrayTy = dyn_cast<ConstantArrayType>(Ty))
18760             TraitTy = ConstArrayTy->getElementType();
18761         if (TraitTy.isNull() ||
18762             !(Context.hasSameUnqualifiedType(TraitTy,
18763                                              DSAStack->getOMPAlloctraitT()) ||
18764               Context.typesAreCompatible(TraitTy, DSAStack->getOMPAlloctraitT(),
18765                                          /*CompareUnqualified=*/true))) {
18766           Diag(D.AllocatorTraits->getExprLoc(),
18767                diag::err_omp_expected_array_alloctraits)
18768               << AllocatorTraitsExpr->getType();
18769           continue;
18770         }
18771         // Do not map by default allocator traits if it is a standalone
18772         // variable.
18773         if (auto *DRE = dyn_cast<DeclRefExpr>(AllocatorTraitsExpr))
18774           DSAStack->addUsesAllocatorsDecl(
18775               DRE->getDecl(),
18776               DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait);
18777       }
18778     }
18779     OMPUsesAllocatorsClause::Data &NewD = NewData.emplace_back();
18780     NewD.Allocator = AllocatorExpr;
18781     NewD.AllocatorTraits = AllocatorTraitsExpr;
18782     NewD.LParenLoc = D.LParenLoc;
18783     NewD.RParenLoc = D.RParenLoc;
18784   }
18785   return OMPUsesAllocatorsClause::Create(Context, StartLoc, LParenLoc, EndLoc,
18786                                          NewData);
18787 }
18788